mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +08:00
added context tests
This commit is contained in:
parent
32304c721f
commit
0220b302d7
@ -691,7 +691,7 @@ func TestContextRenderProtoJSON(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
assert.Equal(t, http.StatusCreated, w.Code)
|
assert.Equal(t, http.StatusCreated, w.Code)
|
||||||
assert.Equal(t, "{\"label\":\"yes!\",\"optionalField\":\"ahah\"}", w.Body.String())
|
assert.Equal(t, "{\"label\":\"yes!\", \"optionalField\":\"ahah\"}", w.Body.String())
|
||||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1623,6 +1623,20 @@ func TestContextBindWithJSON(t *testing.T) {
|
|||||||
assert.Equal(t, 0, w.Body.Len())
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextBindWithProtoJSON(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"label\":\"bar\",\"optionalField\":\"foo\"}"))
|
||||||
|
c.Request.Header.Add("Content-Type", MIMEJSON)
|
||||||
|
|
||||||
|
var obj testdata.Test
|
||||||
|
assert.NoError(t, c.BindProtoJSON(&obj))
|
||||||
|
assert.Equal(t, "bar", obj.Label)
|
||||||
|
assert.Equal(t, "foo", *obj.OptionalField)
|
||||||
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextBindWithXML(t *testing.T) {
|
func TestContextBindWithXML(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
@ -1769,6 +1783,20 @@ func TestContextShouldBindWithJSON(t *testing.T) {
|
|||||||
assert.Equal(t, 0, w.Body.Len())
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextShouldBindWithProtoJSON(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"label\":\"bar\", \"optionalField\":\"foo\"}"))
|
||||||
|
c.Request.Header.Add("Content-Type", MIMEJSON) // set fake content-type
|
||||||
|
|
||||||
|
var obj testdata.Test
|
||||||
|
assert.NoError(t, c.ShouldBindProtoJSON(&obj))
|
||||||
|
assert.Equal(t, "bar", obj.Label)
|
||||||
|
assert.Equal(t, "foo", *obj.OptionalField)
|
||||||
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextShouldBindWithXML(t *testing.T) {
|
func TestContextShouldBindWithXML(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user