@@ -124,19 +124,113 @@ Available representations: text/plain, application/json`
124
124
}
125
125
}
126
126
127
- func TestUnsupportedMedia_Issue492 (t * testing.T ) {
127
+ func TestUnsupportedMedia_AcceptOnly (t * testing.T ) {
128
+ tearDown ()
129
+ Add (newPostTestService ())
130
+ for _ , method := range []string {"POST" , "PUT" , "PATCH" } {
131
+ httpRequest , _ := http .NewRequest (method , "http://here.com/test" , nil ) // no content
132
+ httpRequest .Header .Set ("Accept" , "application/json" )
133
+ httpWriter := httptest .NewRecorder ()
134
+ DefaultContainer .dispatch (httpWriter , httpRequest )
135
+ if http .StatusUnsupportedMediaType != httpWriter .Code {
136
+ t .Errorf ("[%s] 415 expected got %d" , method , httpWriter .Code )
137
+ }
138
+ }
139
+ }
140
+ func TestUnsupportedMedia_AcceptOnlyWithZeroContentLength (t * testing.T ) {
128
141
tearDown ()
129
142
Add (newPostTestService ())
130
143
for _ , method := range []string {"POST" , "PUT" , "PATCH" } {
131
144
httpRequest , _ := http .NewRequest (method , "http://here.com/test" , nil )
132
145
httpRequest .Header .Set ("Accept" , "application/json" )
146
+ httpRequest .Header .Set ("Content-length" , "0" )
133
147
httpWriter := httptest .NewRecorder ()
134
148
DefaultContainer .dispatch (httpWriter , httpRequest )
135
- if 415 != httpWriter .Code {
149
+ if http . StatusUnsupportedMediaType != httpWriter .Code {
136
150
t .Errorf ("[%s] 415 expected got %d" , method , httpWriter .Code )
137
151
}
138
152
}
139
153
}
154
+ func TestUnsupportedMedia_ContentTypeOnly (t * testing.T ) { // If Accept is not set then */* is used.
155
+ tearDown ()
156
+ Add (newPostTestService ())
157
+ for _ , method := range []string {"POST" , "PUT" , "PATCH" } {
158
+ httpRequest , _ := http .NewRequest (method , "http://here.com/test" , nil ) // no content
159
+ httpRequest .Header .Set ("Content-type" , "application/json" )
160
+ httpWriter := httptest .NewRecorder ()
161
+ DefaultContainer .dispatch (httpWriter , httpRequest )
162
+ if http .StatusOK != httpWriter .Code {
163
+ t .Errorf ("[%s] 200 expected got %d" , method , httpWriter .Code )
164
+ }
165
+ }
166
+ }
167
+
168
+ func TestGetWithNonMatchingContentType (t * testing.T ) { // If Accept is not set then */* is used.
169
+ tearDown ()
170
+ Add (newGetOnlyJsonOnlyService ())
171
+ httpRequest , _ := http .NewRequest ("GET" , "http://here.com/get" , nil ) // no content
172
+ httpRequest .Header .Set ("Content-type" , "application/yaml" )
173
+ httpWriter := httptest .NewRecorder ()
174
+ DefaultContainer .dispatch (httpWriter , httpRequest )
175
+ if httpWriter .Code != http .StatusUnsupportedMediaType {
176
+ t .Errorf ("[%s] 415 expected got %d" , "GET" , httpWriter .Code )
177
+ }
178
+ }
179
+
180
+ func TestPostWithNonMatchingContentType (t * testing.T ) { // If Accept is not set then */* is used.
181
+ tearDown ()
182
+ Add (newPostNoConsumesService ())
183
+ httpRequest , _ := http .NewRequest ("POST" , "http://here.com/post" , nil ) // no content
184
+ httpRequest .Header .Set ("Content-type" , "application/yaml" )
185
+ httpWriter := httptest .NewRecorder ()
186
+ DefaultContainer .dispatch (httpWriter , httpRequest )
187
+ if httpWriter .Code != http .StatusNoContent {
188
+ t .Errorf ("[%s] 204 expected got %d" , "POST" , httpWriter .Code )
189
+ }
190
+ }
191
+
192
+ func TestPostWithNonMatchingAccept (t * testing.T ) {
193
+ tearDown ()
194
+ // consumes and produces JSON on POST,PUT and PATCH
195
+ Add (newPostTestService ())
196
+ httpRequest , _ := http .NewRequest ("POST" , "http://here.com/test" , nil ) // no content
197
+ httpRequest .Header .Set ("Content-type" , "application/json" )
198
+ httpRequest .Header .Set ("Accept" , "application/yaml" )
199
+ httpWriter := httptest .NewRecorder ()
200
+ DefaultContainer .dispatch (httpWriter , httpRequest )
201
+ if httpWriter .Code != http .StatusNotAcceptable {
202
+ t .Errorf ("[%s] 406 expected got %d" , "POST" , httpWriter .Code )
203
+ }
204
+ }
205
+
206
+ func TestPostEmptyBody (t * testing.T ) {
207
+ tearDown ()
208
+ // consumes and produces JSON on POST,PUT and PATCH
209
+ Add (newPostTestService ())
210
+ httpRequest , _ := http .NewRequest ("POST" , "http://here.com/test" , nil ) // no content
211
+ httpRequest .Header .Set ("Content-type" , "application/json" )
212
+ httpRequest .Header .Set ("Accept" , "application/json" )
213
+ httpWriter := httptest .NewRecorder ()
214
+ DefaultContainer .dispatch (httpWriter , httpRequest )
215
+ if httpWriter .Code != http .StatusOK {
216
+ t .Errorf ("[%s] 200 expected got %d" , "POST" , httpWriter .Code )
217
+ }
218
+ }
219
+
220
+ func TestPostEmptyBodyZeroContentLength (t * testing.T ) {
221
+ tearDown ()
222
+ // consumes and produces JSON on POST,PUT and PATCH
223
+ Add (newPostTestService ())
224
+ httpRequest , _ := http .NewRequest ("POST" , "http://here.com/test" , nil ) // no content
225
+ httpRequest .Header .Set ("Content-type" , "application/json" )
226
+ httpRequest .Header .Set ("Content-length" , "0" )
227
+ httpRequest .Header .Set ("Accept" , "application/json" )
228
+ httpWriter := httptest .NewRecorder ()
229
+ DefaultContainer .dispatch (httpWriter , httpRequest )
230
+ if httpWriter .Code != http .StatusOK {
231
+ t .Errorf ("[%s] 200 expected got %d" , "POST" , httpWriter .Code )
232
+ }
233
+ }
140
234
141
235
func TestSelectedRoutePath_Issue100 (t * testing.T ) {
142
236
tearDown ()
@@ -390,6 +484,7 @@ func newPostNoConsumesService() *WebService {
390
484
return ws
391
485
}
392
486
487
+ // consumes and produces JSON on POST,PUT and PATCH
393
488
func newPostTestService () * WebService {
394
489
ws := new (WebService ).Path ("" )
395
490
ws .Consumes ("application/json" )
0 commit comments