Merge a1329c766df82793ebfc5b4878ceb37618355ae5 into e9531e5c7680d70a1e32756447535f7cfe1a6171

This commit is contained in:
Tyson Maly 2016-01-26 19:29:04 +00:00
commit c3b762fea7
8 changed files with 52 additions and 8 deletions

View File

@ -5,6 +5,7 @@
package gin package gin
import ( import (
"bytes"
"errors" "errors"
"io" "io"
"math" "math"
@ -395,6 +396,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. // XML serializes the given struct as XML into the response body.
// It also sets the Content-Type as "application/xml". // It also sets the Content-Type as "application/xml".
func (c *Context) XML(code int, obj interface{}) { func (c *Context) XML(code int, obj interface{}) {

View File

@ -153,7 +153,7 @@ func TestContextHandlerName(t *testing.T) {
c, _, _ := CreateTestContext() c, _, _ := CreateTestContext()
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest} 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) { func handlerNameTest(c *Context) {
@ -264,6 +264,21 @@ func TestContextRenderJSON(t *testing.T) {
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") 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 // Tests that the response is serialized as JSON
// we change the content-type before // we change the content-type before
func TestContextRenderAPIJSON(t *testing.T) { func TestContextRenderAPIJSON(t *testing.T) {

View File

@ -63,7 +63,7 @@ func TestDebugPrintRoutes(t *testing.T) {
defer teardown() defer teardown()
debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest}) 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) { func setup(w io.Writer) {

9
gin.go
View File

@ -9,6 +9,8 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"path"
"runtime"
"sync" "sync"
"github.com/gin-gonic/gin/render" "github.com/gin-gonic/gin/render"
@ -20,6 +22,13 @@ const Version = "v1.0rc2"
var default404Body = []byte("404 page not found") var default404Body = []byte("404 page not found")
var default405Body = []byte("405 method not allowed") 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 HandlerFunc func(*Context)
type HandlersChain []HandlerFunc type HandlersChain []HandlerFunc

View File

@ -217,27 +217,27 @@ func TestListOfRoutes(t *testing.T) {
assert.Contains(t, list, RouteInfo{ assert.Contains(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/favicon.ico", Path: "/favicon.ico",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: TestPath + "/gin.handler_test1",
}) })
assert.Contains(t, list, RouteInfo{ assert.Contains(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/", Path: "/",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: TestPath + "/gin.handler_test1",
}) })
assert.Contains(t, list, RouteInfo{ assert.Contains(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/users/", Path: "/users/",
Handler: "github.com/gin-gonic/gin.handler_test2", Handler: TestPath + "/gin.handler_test2",
}) })
assert.Contains(t, list, RouteInfo{ assert.Contains(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/users/:id", Path: "/users/:id",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: TestPath + "/gin.handler_test1",
}) })
assert.Contains(t, list, RouteInfo{ assert.Contains(t, list, RouteInfo{
Method: "POST", Method: "POST",
Path: "/users/:id", Path: "/users/:id",
Handler: "github.com/gin-gonic/gin.handler_test2", Handler: TestPath + "/gin.handler_test2",
}) })
} }

View File

@ -5,6 +5,7 @@
package render package render
import ( import (
"bytes"
"encoding/json" "encoding/json"
"net/http" "net/http"
) )
@ -17,6 +18,9 @@ type (
IndentedJSON struct { IndentedJSON struct {
Data interface{} Data interface{}
} }
RawJSON struct {
Data *bytes.Buffer
}
) )
var jsonContentType = []string{"application/json; charset=utf-8"} var jsonContentType = []string{"application/json; charset=utf-8"}
@ -39,3 +43,9 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error {
writeContentType(w, jsonContentType) writeContentType(w, jsonContentType)
return json.NewEncoder(w).Encode(obj) return json.NewEncoder(w).Encode(obj)
} }
func (r RawJSON) Render(w http.ResponseWriter) error {
writeContentType(w, jsonContentType)
w.Write(r.Data.Bytes())
return nil
}

View File

@ -13,6 +13,7 @@ type Render interface {
var ( var (
_ Render = JSON{} _ Render = JSON{}
_ Render = IndentedJSON{} _ Render = IndentedJSON{}
_ Render = RawJSON{}
_ Render = XML{} _ Render = XML{}
_ Render = String{} _ Render = String{}
_ Render = Redirect{} _ Render = Redirect{}

View File

@ -78,7 +78,7 @@ func TestFilterFlags(t *testing.T) {
} }
func TestFunctionName(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() { func somefunction() {