This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_handler_test.go
257 lines (224 loc) · 12.1 KB
/
action_handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package detour
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestModelBinderFixture(t *testing.T) {
gunit.Run(new(ModelBinderFixture), t)
}
type ModelBinderFixture struct {
*gunit.Fixture
controller *Controller
request *http.Request
response *httptest.ResponseRecorder
}
func (this *ModelBinderFixture) Setup() {
this.controller = &Controller{}
this.request = httptest.NewRequest("GET", "/?binding=BindingInputModel", nil)
this.response = httptest.NewRecorder()
}
func (this *ModelBinderFixture) TestFromFactory_IncorrectInputModelType__Panic() {
wrongInputModelType := func() interface{} { return "wrong type" }
action := func() { NewFromFactory(wrongInputModelType, this.controller.HandleBasicInputModel) }
this.So(action, should.PanicWith,
"Controller requires input model of type: [*detour.BlankBasicInputModel] "+
"Factory function provided input model of type: [string]")
}
func (this *ModelBinderFixture) TestFromFactory_ControllerWithNoInputModel__Panic() {
action := func() { NewFromFactory(NewBlankBasicInputModel, this.controller.HandleNoInputModel) }
this.So(action, should.Panic)
}
func (this *ModelBinderFixture) TestFromFactory_BasicInputModelProvidedToApplication__HTTP200() {
binder := NewFromFactory(NewBlankBasicInputModel, this.controller.HandleBasicInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusOK)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: BasicInputModel")
}
func (this *ModelBinderFixture) TestNoInputModelProvidedToApplication__HTTP200() {
binder := New(this.controller.HandleNoInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusOK)
}
func (this *ModelBinderFixture) TestRequestContextIsPopulatedWhenOnInputModel_HTTP200() {
binder := New(this.controller.HandleInputModelWithContextField)
binder.ServeHTTP(this.response, this.request.WithContext(context.WithValue(context.Background(), "Key", "ContextInputModel")))
this.So(this.response.Code, should.Equal, http.StatusOK)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: ContextInputModel")
}
func (this *ModelBinderFixture) TestBindsModelForApplication__HTTP200() {
binder := New(this.controller.HandleBindingInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusOK)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: BindingInputModel")
}
func (this *ModelBinderFixture) TestBindsFormParseFails__HTTP400() {
this.request = httptest.NewRequest("GET", "/?asdf=%%%%%", nil)
binder := New(this.controller.HandleBindingInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusBadRequest)
}
func (this *ModelBinderFixture) TestBindsModelAndHandlesError__HTTP400_JSONResponse() {
binder := New(this.controller.HandleBindingFailsInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 400)
this.So(this.response.Result().Header.Get(contentTypeHeader), should.Equal, jsonContentType)
this.So(this.response.Body.String(), should.EqualTrimSpace, `[{"Problem":"BindingFailsInputModel"}]`)
}
func (this *ModelBinderFixture) TestBindModelError__CustomStatusCode() {
binder := New(this.controller.HandleBindingFailsCustomStatusCodeInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusTeapot)
}
func (this *ModelBinderFixture) TestBindsModelAndHandlesNilErrors() {
binder := New(this.controller.HandleBindingEmptyErrorsInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
}
func (this *ModelBinderFixture) TestBindsModelEmptyValidationErrors__HTTP200() {
binder := New(this.controller.HandleBindingSucceedsInputModelWithEmptyDiagnosticErrors)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: BindingEmptyDiagnosticErrorsInputModel")
}
func (this *ModelBinderFixture) TestBindFromJSONRequiresInputModelToImplementJSONMarkerInterface() {
this.request = httptest.NewRequest("POST", "/", strings.NewReader(`{"content": "Hello, World!"}`))
this.request.Header.Set("Content-Type", "application/json")
binder := New(this.controller.HandleFailedBindingFromJSON)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.NotContainSubstring, "Hello, World!")
}
func (this *ModelBinderFixture) TestBindFromJSONPost() {
this.request = httptest.NewRequest("POST", "/", strings.NewReader(`{"content": "Hello, World!"}`))
this.request.Header.Set("Content-Type", "application/json")
this.request.Header.Set("binding", " (from the header)")
binder := New(this.controller.HandleBindingFromJSON)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.ContainSubstring, "Hello, World! (from the header)")
}
func (this *ModelBinderFixture) TestBindFromPost_UnsupportedMediaType() {
this.request = httptest.NewRequest("POST", "/", strings.NewReader(`{"content": "This will not be included."}`))
this.request.Header.Set("Content-Type", "application/xml")
this.request.Header.Set("binding", "(there should be nothing before this parenthetical header message)")
binder := New(this.controller.HandleBindingFromJSON)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 415)
this.So(this.response.Body.String(), should.Equal, "Unsupported Media Type")
}
func (this *ModelBinderFixture) TestBindFromJSONDisabled_JSONBodyIgnored() {
this.request = httptest.NewRequest("POST", "/", strings.NewReader(`{"content": "This will not be included."}`))
this.request.Header.Set("Content-Type", "application/json")
this.request.Header.Set("binding", "(there should be nothing before this parenthetical header message)")
binder := New(this.controller.HandleBindingFromJSONDisabled)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.Equal, "Just handled: (there should be nothing before this parenthetical header message)\n")
}
func (this *ModelBinderFixture) TestBindFromJSON_NotPUTorPOST_MethodNotAllowed() {
this.request = httptest.NewRequest("GET", "/", strings.NewReader(`{"content": "This will not be included."}`))
this.request.Header.Set("Content-Type", "application/json")
this.request.Header.Set("binding", "(there should be nothing before this parenthetical header message)")
binder := New(this.controller.HandleBindingFromJSON)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 405)
this.So(this.response.Body.String(), should.Equal, "Method Not Allowed")
}
func (this *ModelBinderFixture) TestBindFromJSONPut() {
this.request = httptest.NewRequest("PUT", "/", strings.NewReader(`{"content": "Hello, World!"}`))
this.request.Header.Set("Content-Type", "application/json")
binder := New(this.controller.HandleBindingFromJSON)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.ContainSubstring, "Hello, World!")
}
func (this *ModelBinderFixture) TestBindFromJSON_Malformed() {
this.request = httptest.NewRequest("PUT", "/", strings.NewReader(`{I can haz JSONs}`))
this.request.Header.Set("Content-Type", "application/json")
binder := New(this.controller.HandleBindingFromJSON)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 400)
this.So(this.response.Body.String(), should.ContainSubstring, "invalid character")
}
func (this *ModelBinderFixture) TestSanitizesModelIfAvailable() {
this.request.Header.Set("binding", "hello")
sanitizer := New(this.controller.HandleSanitizingInputModel)
sanitizer.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusOK)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: SANITIZINGINPUTMODEL")
}
func (this *ModelBinderFixture) TestValidatesModelForApplication__HTTP200() {
binder := New(this.controller.HandleValidatingInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusOK)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: ValidatingInputModel")
}
func (this *ModelBinderFixture) TestValidatesModelAndHandlesError__HTTP422() {
binder := New(this.controller.HandleValidatingFailsInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 422)
this.So(this.response.Result().Header.Get(contentTypeHeader), should.Equal, jsonContentType)
this.So(this.response.Body.String(), should.EqualTrimSpace, `[{"Problem":"ValidatingFailsInputModel"}]`)
}
func (this *ModelBinderFixture) TestValidatesModelEmptyValidationErrors__HTTP200() {
binder := New(this.controller.HandleValidatingEmptyErrors)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: ValidatingEmptyErrorsInputModel")
}
func (this *ModelBinderFixture) TestValidatesModelEmptyDiagnosticErrors__HTTP200() {
binder := New(this.controller.HandleValidatingEmptyDiagnosticErrors)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: ValidatingEmptyDiagnosticErrorsInputModel")
}
func (this *ModelBinderFixture) TestValidatesModelCustomStatusCodeError__HTTP418() {
binder := New(this.controller.HandleValidatingFailsWithCustomStatusCode)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusTeapot)
}
func (this *ModelBinderFixture) TestFinalErrorCondition__HTTP500() {
action := New(this.controller.HandleFinalError)
action.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 500)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Internal Server Error")
}
func (this *ModelBinderFixture) TestNoFinalErrorCondition__HTTP200() {
action := New(this.controller.HandleNoFinalError)
action.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
}
func (this *ModelBinderFixture) TestNilResponseFromApplication__HTTP200() {
binder := New(this.controller.HandleNilResponseInputModel)
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, http.StatusOK)
this.So(this.response.Body.String(), should.BeBlank)
}
////////////////////////////////////////////////////////////
func (this *ModelBinderFixture) TestModelParsingFromCallback() {
this.assertPanicWith(0, "The action provided is not a func.")
this.assertPanicWith(func(int) Renderer { return nil }, "The first argument to the controller callback must be a pointer type.")
this.assertPanicWith(func(*int, *int) Renderer { return nil }, "The callback provided must have no more than one argument.")
this.assertPanicWith(func(*BlankBasicInputModel) {}, "The return type must implement the detour.Renderer interface.")
this.assertDoesNOTPanic(func(*BlankBasicInputModel) Renderer { return nil })
this.assertDoesNOTPanic(func() Renderer { return nil })
}
func (this *ModelBinderFixture) assertPanicWith(callback interface{}, content string) {
this.So(func() { identifyInputModelArgumentType(callback) }, should.PanicWith, content)
}
func (this *ModelBinderFixture) assertDoesNOTPanic(callback interface{}) {
this.So(func() { identifyInputModelArgumentType(callback) }, should.NotPanic)
}
func (this *ModelBinderFixture) TestModelBinding() {
binder := New(func(input *BindingInputModel) Renderer {
return &ControllerResponse{Body: input.Content}
})
binder.ServeHTTP(this.response, this.request)
this.So(this.response.Code, should.Equal, 200)
this.So(this.response.Body.String(), should.EqualTrimSpace, "Just handled: BindingInputModel")
}