mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 13:02:14 +08:00
Merge a1329c766df82793ebfc5b4878ceb37618355ae5 into e9531e5c7680d70a1e32756447535f7cfe1a6171
This commit is contained in:
commit
c3b762fea7
@ -5,6 +5,7 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"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.
|
||||
// It also sets the Content-Type as "application/xml".
|
||||
func (c *Context) XML(code int, obj interface{}) {
|
||||
|
@ -153,7 +153,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) {
|
||||
@ -264,6 +264,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) {
|
||||
|
@ -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) {
|
||||
|
9
gin.go
9
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
|
||||
|
||||
|
10
gin_test.go
10
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",
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ type Render interface {
|
||||
var (
|
||||
_ Render = JSON{}
|
||||
_ Render = IndentedJSON{}
|
||||
_ Render = RawJSON{}
|
||||
_ Render = XML{}
|
||||
_ Render = String{}
|
||||
_ Render = Redirect{}
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user