From 685d2c99cf473e152b9a6d3a02016f7f31997436 Mon Sep 17 00:00:00 2001 From: Sasha Myasoedov Date: Mon, 11 Aug 2014 13:25:52 +0300 Subject: [PATCH] Added tests for JSON binding. --- context_test.go | 135 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 9662bf5..9a2394a 100644 --- a/context_test.go +++ b/context_test.go @@ -1,6 +1,7 @@ package gin import ( + "bytes" "errors" "html/template" "net/http" @@ -169,7 +170,6 @@ func TestContextData(t *testing.T) { } } - func TestContextFile(t *testing.T) { req, _ := http.NewRequest("GET", "/test/file", nil) w := httptest.NewRecorder() @@ -317,3 +317,136 @@ func TestFailHandlersChain(t *testing.T) { } } + +func TestBindingJSON(t *testing.T) { + + body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}")) + + r := Default() + r.POST("/binding/json", func(c *Context) { + var body struct { + Foo string `json:"foo"` + } + if c.Bind(&body) { + c.JSON(200, H{"parsed": body.Foo}) + } + }) + + req, _ := http.NewRequest("POST", "/binding/json", body) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + r.ServeHTTP(w, req) + + if w.Code != 200 { + t.Errorf("Response code should be Ok, was: %s", w.Code) + } + + if w.Body.String() != "{\"parsed\":\"bar\"}\n" { + t.Errorf("Response should be {\"parsed\":\"bar\"}, was: %s", w.Body.String()) + } + + if w.HeaderMap.Get("Content-Type") != "application/json" { + t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type")) + } +} + +func TestBindingJSONEncoding(t *testing.T) { + + body := bytes.NewBuffer([]byte("{\"foo\":\"嘉\"}")) + + r := Default() + r.POST("/binding/json", func(c *Context) { + var body struct { + Foo string `json:"foo"` + } + if c.Bind(&body) { + c.JSON(200, H{"parsed": body.Foo}) + } + }) + + req, _ := http.NewRequest("POST", "/binding/json", body) + req.Header.Set("Content-Type", "application/json; charset=utf-8") + w := httptest.NewRecorder() + + r.ServeHTTP(w, req) + + if w.Code != 200 { + t.Errorf("Response code should be Ok, was: %s", w.Code) + } + + if w.Body.String() != "{\"parsed\":\"嘉\"}\n" { + t.Errorf("Response should be {\"parsed\":\"嘉\"}, was: %s", w.Body.String()) + } + + if w.HeaderMap.Get("Content-Type") != "application/json" { + t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type")) + } +} + +func TestBindingJSONNoContentType(t *testing.T) { + + body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}")) + + r := Default() + r.POST("/binding/json", func(c *Context) { + var body struct { + Foo string `json:"foo"` + } + if c.Bind(&body) { + c.JSON(200, H{"parsed": body.Foo}) + } + + }) + + req, _ := http.NewRequest("POST", "/binding/json", body) + w := httptest.NewRecorder() + + r.ServeHTTP(w, req) + + if w.Code != 400 { + t.Errorf("Response code should be Bad request, was: %s", w.Code) + } + + if w.Body.String() == "{\"parsed\":\"bar\"}\n" { + t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String()) + } + + if w.HeaderMap.Get("Content-Type") == "application/json" { + t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type")) + } +} + +func TestBindingJSONMalformed(t *testing.T) { + + body := bytes.NewBuffer([]byte("\"foo\":\"bar\"\n")) + + r := Default() + r.POST("/binding/json", func(c *Context) { + var body struct { + Foo string `json:"foo"` + } + if c.Bind(&body) { + c.JSON(200, H{"parsed": body.Foo}) + } + + }) + + req, _ := http.NewRequest("POST", "/binding/json", body) + req.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + r.ServeHTTP(w, req) + + if w.Code != 400 { + t.Errorf("Response code should be Bad request, was: %s", w.Code) + } + if w.Body.String() == "{\"parsed\":\"bar\"}\n" { + t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String()) + } + + if w.HeaderMap.Get("Content-Type") == "application/json" { + t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type")) + } +}