@@ -32,6 +32,7 @@ func TestContext(t *testing.T) {
32
32
userJSONIndent := "{\n _?\" id\" : \" 1\" ,\n _?\" name\" : \" Joe\" \n _}"
33
33
userXML := `<user><id>1</id><name>Joe</name></user>`
34
34
userXMLIndent := "_<user>\n _?<id>1</id>\n _?<name>Joe</name>\n _</user>"
35
+ incorrectContent := "this is incorrect content"
35
36
36
37
var nonMarshallableChannel chan bool
37
38
@@ -66,14 +67,18 @@ func TestContext(t *testing.T) {
66
67
//------
67
68
68
69
// JSON
69
- testBind (t , c , "application/json" )
70
+ testBindOk (t , c , ApplicationJSON )
71
+ c .X ().request = test .NewRequest (POST , "/" , strings .NewReader (incorrectContent ))
72
+ testBindError (t , c , ApplicationJSON )
70
73
71
74
// XML
72
75
c .X ().request = test .NewRequest (POST , "/" , strings .NewReader (userXML ))
73
- testBind (t , c , ApplicationXML )
76
+ testBindOk (t , c , ApplicationXML )
77
+ c .X ().request = test .NewRequest (POST , "/" , strings .NewReader (incorrectContent ))
78
+ testBindError (t , c , ApplicationXML )
74
79
75
80
// Unsupported
76
- testBind (t , c , "" )
81
+ testBindError (t , c , "" )
77
82
78
83
//--------
79
84
// Render
@@ -273,14 +278,30 @@ func TestContextNetContext(t *testing.T) {
273
278
// assert.Equal(t, "val", c.Value("key"))
274
279
}
275
280
276
- func testBind (t * testing.T , c Context , ct string ) {
281
+ func testBindOk (t * testing.T , c Context , ct string ) {
277
282
c .Request ().Header ().Set (ContentType , ct )
278
283
u := new (user )
279
284
err := c .Bind (u )
280
- if ct == "" {
281
- assert .Error (t , UnsupportedMediaType )
282
- } else if assert .NoError (t , err ) {
285
+ if assert .NoError (t , err ) {
283
286
assert .Equal (t , "1" , u .ID )
284
287
assert .Equal (t , "Joe" , u .Name )
285
288
}
286
289
}
290
+
291
+ func testBindError (t * testing.T , c Context , ct string ) {
292
+ c .Request ().Header ().Set (ContentType , ct )
293
+ u := new (user )
294
+ err := c .Bind (u )
295
+
296
+ switch ct {
297
+ case ApplicationJSON , ApplicationXML :
298
+ if assert .IsType (t , new (HTTPError ), err ) {
299
+ assert .Equal (t , http .StatusBadRequest , err .(* HTTPError ).code )
300
+ }
301
+ default :
302
+ if assert .IsType (t , new (HTTPError ), err ) {
303
+ assert .Equal (t , UnsupportedMediaType , err )
304
+ }
305
+
306
+ }
307
+ }
0 commit comments