Add gin.CreateContext(w, req) as a Test Helper

This commit is contained in:
Wael M. Nasreddine 2014-08-24 14:54:01 -07:00
parent ae4800e50f
commit 5417a27f9e
2 changed files with 29 additions and 0 deletions

View File

@ -65,6 +65,15 @@ type Context struct {
index int8
}
/************************************/
/*********** TEST HELPERS ***********/
/************************************/
func CreateContext(w http.ResponseWriter, req *http.Request) *Context {
engine := New()
return engine.createContext(w, req, nil, nil)
}
/************************************/
/********** ROUTES GROUPING *********/
/************************************/

View File

@ -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"))
}
}
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())
}
}