mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Add gin.CreateContext(w, req) as a Test Helper
This commit is contained in:
parent
ae4800e50f
commit
5417a27f9e
@ -65,6 +65,15 @@ type Context struct {
|
|||||||
index int8
|
index int8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/*********** TEST HELPERS ***********/
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
func CreateContext(w http.ResponseWriter, req *http.Request) *Context {
|
||||||
|
engine := New()
|
||||||
|
return engine.createContext(w, req, nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
/********** ROUTES GROUPING *********/
|
/********** ROUTES GROUPING *********/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
@ -436,3 +436,23 @@ func TestBindingJSONMalformed(t *testing.T) {
|
|||||||
t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateContext(t *testing.T) {
|
||||||
|
req, _ := http.NewRequest("GET", "/", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
c := CreateContext(w, req)
|
||||||
|
c.JSON(200, H{"foo": "bar"})
|
||||||
|
|
||||||
|
if w.Code != 200 {
|
||||||
|
t.Errorf("Response code should be Ok, was: %s", w.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
if w.HeaderMap.Get("Content-Type") != "application/json" {
|
||||||
|
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if w.Body.String() != "{\"foo\":\"bar\"}\n" {
|
||||||
|
t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", w.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user