From febfd57e0cba430079cc9df0dbf0fa2a1cd05585 Mon Sep 17 00:00:00 2001 From: Tyson Maly Date: Mon, 21 Sep 2015 08:03:29 -0400 Subject: [PATCH 1/2] replaced hard coded test paths to make it easier for contributors to run tests --- context_test.go | 2 +- debug_test.go | 2 +- gin.go | 9 +++++++++ gin_test.go | 10 +++++----- utils_test.go | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/context_test.go b/context_test.go index efdba7b2..14b93208 100644 --- a/context_test.go +++ b/context_test.go @@ -161,7 +161,7 @@ func TestContextHandlerName(t *testing.T) { c, _, _ := createTestContext() c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest} - assert.Equal(t, c.HandlerName(), "github.com/gin-gonic/gin.handlerNameTest") + assert.Equal(t, c.HandlerName(), TestPath+"/gin.handlerNameTest") } func handlerNameTest(c *Context) { diff --git a/debug_test.go b/debug_test.go index 7a352e6e..5aa2e843 100644 --- a/debug_test.go +++ b/debug_test.go @@ -63,7 +63,7 @@ func TestDebugPrintRoutes(t *testing.T) { defer teardown() debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest}) - assert.Equal(t, w.String(), "[GIN-debug] GET /path/to/route/:param --> github.com/gin-gonic/gin.handlerNameTest (2 handlers)\n") + assert.Equal(t, w.String(), "[GIN-debug] GET /path/to/route/:param --> "+TestPath+"/gin.handlerNameTest (2 handlers)\n") } func setup(w io.Writer) { diff --git a/gin.go b/gin.go index 3834d67e..3f017b37 100644 --- a/gin.go +++ b/gin.go @@ -9,6 +9,8 @@ import ( "net" "net/http" "os" + "path" + "runtime" "sync" "github.com/gin-gonic/gin/render" @@ -20,6 +22,13 @@ const Version = "v1.0rc2" var default404Body = []byte("404 page not found") var default405Body = []byte("405 method not allowed") +// replace hardcoded paths in assertions within tests to +// make it easier for contributors to run tests +var _, CurrentFile, _, _ = runtime.Caller(1) +var TestPath = path.Dir(path.Dir(path.Dir(CurrentFile))) + +//TestPath,_ := os.Open(path.Join(path.Dir(TestFile), "gin.go")) + type HandlerFunc func(*Context) type HandlersChain []HandlerFunc diff --git a/gin_test.go b/gin_test.go index b3b0eb6b..165eb22a 100644 --- a/gin_test.go +++ b/gin_test.go @@ -217,27 +217,27 @@ func TestListOfRoutes(t *testing.T) { assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/favicon.ico", - Handler: "github.com/gin-gonic/gin.handler_test1", + Handler: TestPath + "/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/", - Handler: "github.com/gin-gonic/gin.handler_test1", + Handler: TestPath + "/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/users/", - Handler: "github.com/gin-gonic/gin.handler_test2", + Handler: TestPath + "/gin.handler_test2", }) assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/users/:id", - Handler: "github.com/gin-gonic/gin.handler_test1", + Handler: TestPath + "/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ Method: "POST", Path: "/users/:id", - Handler: "github.com/gin-gonic/gin.handler_test2", + Handler: TestPath + "/gin.handler_test2", }) } diff --git a/utils_test.go b/utils_test.go index 11a5b684..624ccf4b 100644 --- a/utils_test.go +++ b/utils_test.go @@ -78,7 +78,7 @@ func TestFilterFlags(t *testing.T) { } func TestFunctionName(t *testing.T) { - assert.Equal(t, nameOfFunction(somefunction), "github.com/gin-gonic/gin.somefunction") + assert.Equal(t, nameOfFunction(somefunction), TestPath+"/gin.somefunction") } func somefunction() { From a1329c766df82793ebfc5b4878ceb37618355ae5 Mon Sep 17 00:00:00 2001 From: Tyson Maly Date: Mon, 21 Sep 2015 08:10:01 -0400 Subject: [PATCH 2/2] RawJSON added to allow writing JSON from bytes --- context.go | 9 +++++++++ context_test.go | 15 +++++++++++++++ render/json.go | 10 ++++++++++ render/render.go | 1 + 4 files changed, 35 insertions(+) diff --git a/context.go b/context.go index b784c14b..56b2bace 100644 --- a/context.go +++ b/context.go @@ -5,6 +5,7 @@ package gin import ( + "bytes" "errors" "io" "math" @@ -358,6 +359,14 @@ func (c *Context) JSON(code int, obj interface{}) { } } +// RawJSON give a buffer of bytes representing JSON just write it out +// this habdles cases where you get back JSON as bytes from redis or ffjson +// and you do not want the extra marshall/demarshal overhead +// It also sets the Content-Type as "application/json". +func (c *Context) RawJSON(code int, obj interface{}) { + c.Render(code, render.RawJSON{Data: obj.(*bytes.Buffer)}) +} + // XML serializes the given struct as XML into the response body. // It also sets the Content-Type as "application/xml". func (c *Context) XML(code int, obj interface{}) { diff --git a/context_test.go b/context_test.go index 14b93208..9c2e8e62 100644 --- a/context_test.go +++ b/context_test.go @@ -257,6 +257,21 @@ func TestContextRenderJSON(t *testing.T) { assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") } +// Tests that the response is JSON writen out from bytes +// and Content-Type is set to application/json +func TestContextRenderRawJSON(t *testing.T) { + c, w, _ := createTestContext() + + var buffer bytes.Buffer + buffer.WriteString("{\"foo\": \"bar\"}") + + c.RawJSON(201, &buffer) + + assert.Equal(t, w.Code, 201) + assert.Equal(t, w.Body.String(), "{\"foo\": \"bar\"}") + assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") +} + // Tests that the response is serialized as JSON // we change the content-type before func TestContextRenderAPIJSON(t *testing.T) { diff --git a/render/json.go b/render/json.go index 32e6058d..3e82292a 100644 --- a/render/json.go +++ b/render/json.go @@ -5,6 +5,7 @@ package render import ( + "bytes" "encoding/json" "net/http" ) @@ -17,6 +18,9 @@ type ( IndentedJSON struct { Data interface{} } + RawJSON struct { + Data *bytes.Buffer + } ) var jsonContentType = []string{"application/json; charset=utf-8"} @@ -39,3 +43,9 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error { writeContentType(w, jsonContentType) return json.NewEncoder(w).Encode(obj) } + +func (r RawJSON) Render(w http.ResponseWriter) error { + writeContentType(w, jsonContentType) + w.Write(r.Data.Bytes()) + return nil +} diff --git a/render/render.go b/render/render.go index 994fcd7c..af85130c 100644 --- a/render/render.go +++ b/render/render.go @@ -13,6 +13,7 @@ type Render interface { var ( _ Render = JSON{} _ Render = IndentedJSON{} + _ Render = RawJSON{} _ Render = XML{} _ Render = String{} _ Render = Redirect{}