Merge branch 'danielalves-master' into develop

This commit is contained in:
Javier Provecho Fernandez 2015-10-02 10:26:23 +02:00
commit ee820830d5
2 changed files with 49 additions and 44 deletions

View File

@ -25,15 +25,6 @@ import (
// BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) { // BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {
// test that information is not leaked when reusing Contexts (using the Pool) // test that information is not leaked when reusing Contexts (using the Pool)
func createTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {
w = httptest.NewRecorder()
r = New()
c = r.allocateContext()
c.reset()
c.writermem.reset(w)
return
}
func createMultipartRequest() *http.Request { func createMultipartRequest() *http.Request {
boundary := "--testboundary" boundary := "--testboundary"
body := new(bytes.Buffer) body := new(bytes.Buffer)
@ -82,7 +73,7 @@ func TestContextReset(t *testing.T) {
} }
func TestContextHandlers(t *testing.T) { func TestContextHandlers(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
assert.Nil(t, c.handlers) assert.Nil(t, c.handlers)
assert.Nil(t, c.handlers.Last()) assert.Nil(t, c.handlers.Last())
@ -103,7 +94,7 @@ func TestContextHandlers(t *testing.T) {
// TestContextSetGet tests that a parameter is set correctly on the // TestContextSetGet tests that a parameter is set correctly on the
// current context and can be retrieved using Get. // current context and can be retrieved using Get.
func TestContextSetGet(t *testing.T) { func TestContextSetGet(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Set("foo", "bar") c.Set("foo", "bar")
value, err := c.Get("foo") value, err := c.Get("foo")
@ -119,7 +110,7 @@ func TestContextSetGet(t *testing.T) {
} }
func TestContextSetGetValues(t *testing.T) { func TestContextSetGetValues(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Set("string", "this is a string") c.Set("string", "this is a string")
c.Set("int32", int32(-42)) c.Set("int32", int32(-42))
c.Set("int64", int64(42424242424242)) c.Set("int64", int64(42424242424242))
@ -140,7 +131,7 @@ func TestContextSetGetValues(t *testing.T) {
} }
func TestContextCopy(t *testing.T) { func TestContextCopy(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.index = 2 c.index = 2
c.Request, _ = http.NewRequest("POST", "/hola", nil) c.Request, _ = http.NewRequest("POST", "/hola", nil)
c.handlers = HandlersChain{func(c *Context) {}} c.handlers = HandlersChain{func(c *Context) {}}
@ -159,7 +150,7 @@ func TestContextCopy(t *testing.T) {
} }
func TestContextHandlerName(t *testing.T) { 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(), "github.com/gin-gonic/gin.handlerNameTest")
@ -170,7 +161,7 @@ func handlerNameTest(c *Context) {
} }
func TestContextQuery(t *testing.T) { func TestContextQuery(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10", nil) c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10", nil)
assert.Equal(t, c.DefaultQuery("foo", "none"), "bar") assert.Equal(t, c.DefaultQuery("foo", "none"), "bar")
@ -187,7 +178,7 @@ func TestContextQuery(t *testing.T) {
} }
func TestContextQueryAndPostForm(t *testing.T) { func TestContextQueryAndPostForm(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
body := bytes.NewBufferString("foo=bar&page=11&both=POST&foo=second") body := bytes.NewBufferString("foo=bar&page=11&both=POST&foo=second")
c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body) c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body)
c.Request.Header.Add("Content-Type", MIMEPOSTForm) c.Request.Header.Add("Content-Type", MIMEPOSTForm)
@ -227,7 +218,7 @@ func TestContextQueryAndPostForm(t *testing.T) {
} }
func TestContextPostFormMultipart(t *testing.T) { func TestContextPostFormMultipart(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request = createMultipartRequest() c.Request = createMultipartRequest()
var obj struct { var obj struct {
@ -250,7 +241,7 @@ func TestContextPostFormMultipart(t *testing.T) {
// Tests that the response is serialized as JSON // Tests that the response is serialized as JSON
// and Content-Type is set to application/json // and Content-Type is set to application/json
func TestContextRenderJSON(t *testing.T) { func TestContextRenderJSON(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.JSON(201, H{"foo": "bar"}) c.JSON(201, H{"foo": "bar"})
assert.Equal(t, w.Code, 201) assert.Equal(t, w.Code, 201)
@ -261,7 +252,7 @@ func TestContextRenderJSON(t *testing.T) {
// 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) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Header("Content-Type", "application/vnd.api+json") c.Header("Content-Type", "application/vnd.api+json")
c.JSON(201, H{"foo": "bar"}) c.JSON(201, H{"foo": "bar"})
@ -273,7 +264,7 @@ func TestContextRenderAPIJSON(t *testing.T) {
// Tests that the response is serialized as JSON // Tests that the response is serialized as JSON
// and Content-Type is set to application/json // and Content-Type is set to application/json
func TestContextRenderIndentedJSON(t *testing.T) { func TestContextRenderIndentedJSON(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}}) c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})
assert.Equal(t, w.Code, 201) assert.Equal(t, w.Code, 201)
@ -284,7 +275,7 @@ func TestContextRenderIndentedJSON(t *testing.T) {
// Tests that the response executes the templates // Tests that the response executes the templates
// and responds with Content-Type set to text/html // and responds with Content-Type set to text/html
func TestContextRenderHTML(t *testing.T) { func TestContextRenderHTML(t *testing.T) {
c, w, router := createTestContext() c, w, router := CreateTestContext()
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`)) templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
router.SetHTMLTemplate(templ) router.SetHTMLTemplate(templ)
@ -298,7 +289,7 @@ func TestContextRenderHTML(t *testing.T) {
// TestContextXML tests that the response is serialized as XML // TestContextXML tests that the response is serialized as XML
// and Content-Type is set to application/xml // and Content-Type is set to application/xml
func TestContextRenderXML(t *testing.T) { func TestContextRenderXML(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.XML(201, H{"foo": "bar"}) c.XML(201, H{"foo": "bar"})
assert.Equal(t, w.Code, 201) assert.Equal(t, w.Code, 201)
@ -309,7 +300,7 @@ func TestContextRenderXML(t *testing.T) {
// TestContextString tests that the response is returned // TestContextString tests that the response is returned
// with Content-Type set to text/plain // with Content-Type set to text/plain
func TestContextRenderString(t *testing.T) { func TestContextRenderString(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.String(201, "test %s %d", "string", 2) c.String(201, "test %s %d", "string", 2)
assert.Equal(t, w.Code, 201) assert.Equal(t, w.Code, 201)
@ -320,7 +311,7 @@ func TestContextRenderString(t *testing.T) {
// TestContextString tests that the response is returned // TestContextString tests that the response is returned
// with Content-Type set to text/html // with Content-Type set to text/html
func TestContextRenderHTMLString(t *testing.T) { func TestContextRenderHTMLString(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Header("Content-Type", "text/html; charset=utf-8") c.Header("Content-Type", "text/html; charset=utf-8")
c.String(201, "<html>%s %d</html>", "string", 3) c.String(201, "<html>%s %d</html>", "string", 3)
@ -332,7 +323,7 @@ func TestContextRenderHTMLString(t *testing.T) {
// TestContextData tests that the response can be written from `bytesting` // TestContextData tests that the response can be written from `bytesting`
// with specified MIME type // with specified MIME type
func TestContextRenderData(t *testing.T) { func TestContextRenderData(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Data(201, "text/csv", []byte(`foo,bar`)) c.Data(201, "text/csv", []byte(`foo,bar`))
assert.Equal(t, w.Code, 201) assert.Equal(t, w.Code, 201)
@ -341,7 +332,7 @@ func TestContextRenderData(t *testing.T) {
} }
func TestContextRenderSSE(t *testing.T) { func TestContextRenderSSE(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.SSEvent("float", 1.5) c.SSEvent("float", 1.5)
c.Render(-1, sse.Event{ c.Render(-1, sse.Event{
Id: "123", Id: "123",
@ -356,7 +347,7 @@ func TestContextRenderSSE(t *testing.T) {
} }
func TestContextRenderFile(t *testing.T) { func TestContextRenderFile(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Request, _ = http.NewRequest("GET", "/", nil) c.Request, _ = http.NewRequest("GET", "/", nil)
c.File("./gin.go") c.File("./gin.go")
@ -366,7 +357,7 @@ func TestContextRenderFile(t *testing.T) {
} }
func TestContextHeaders(t *testing.T) { func TestContextHeaders(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Header("Content-Type", "text/plain") c.Header("Content-Type", "text/plain")
c.Header("X-Custom", "value") c.Header("X-Custom", "value")
@ -383,7 +374,7 @@ func TestContextHeaders(t *testing.T) {
// TODO // TODO
func TestContextRenderRedirectWithRelativePath(t *testing.T) { func TestContextRenderRedirectWithRelativePath(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "http://example.com", nil) c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
assert.Panics(t, func() { c.Redirect(299, "/new_path") }) assert.Panics(t, func() { c.Redirect(299, "/new_path") })
assert.Panics(t, func() { c.Redirect(309, "/new_path") }) assert.Panics(t, func() { c.Redirect(309, "/new_path") })
@ -395,7 +386,7 @@ func TestContextRenderRedirectWithRelativePath(t *testing.T) {
} }
func TestContextRenderRedirectWithAbsolutePath(t *testing.T) { func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "http://example.com", nil) c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
c.Redirect(302, "http://google.com") c.Redirect(302, "http://google.com")
c.Writer.WriteHeaderNow() c.Writer.WriteHeaderNow()
@ -405,7 +396,7 @@ func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
} }
func TestContextNegotiationFormat(t *testing.T) { func TestContextNegotiationFormat(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "", nil) c.Request, _ = http.NewRequest("POST", "", nil)
assert.Panics(t, func() { c.NegotiateFormat() }) assert.Panics(t, func() { c.NegotiateFormat() })
@ -414,7 +405,7 @@ func TestContextNegotiationFormat(t *testing.T) {
} }
func TestContextNegotiationFormatWithAccept(t *testing.T) { func TestContextNegotiationFormatWithAccept(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "/", nil) c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
@ -424,7 +415,7 @@ func TestContextNegotiationFormatWithAccept(t *testing.T) {
} }
func TestContextNegotiationFormatCustum(t *testing.T) { func TestContextNegotiationFormatCustum(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "/", nil) c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
@ -437,7 +428,7 @@ func TestContextNegotiationFormatCustum(t *testing.T) {
} }
func TestContextIsAborted(t *testing.T) { func TestContextIsAborted(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
assert.False(t, c.IsAborted()) assert.False(t, c.IsAborted())
c.Abort() c.Abort()
@ -453,7 +444,7 @@ func TestContextIsAborted(t *testing.T) {
// TestContextData tests that the response can be written from `bytesting` // TestContextData tests that the response can be written from `bytesting`
// with specified MIME type // with specified MIME type
func TestContextAbortWithStatus(t *testing.T) { func TestContextAbortWithStatus(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.index = 4 c.index = 4
c.AbortWithStatus(401) c.AbortWithStatus(401)
c.Writer.WriteHeaderNow() c.Writer.WriteHeaderNow()
@ -465,7 +456,7 @@ func TestContextAbortWithStatus(t *testing.T) {
} }
func TestContextError(t *testing.T) { func TestContextError(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
assert.Empty(t, c.Errors) assert.Empty(t, c.Errors)
c.Error(errors.New("first error")) c.Error(errors.New("first error"))
@ -491,7 +482,7 @@ func TestContextError(t *testing.T) {
} }
func TestContextTypedError(t *testing.T) { func TestContextTypedError(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Error(errors.New("externo 0")).SetType(ErrorTypePublic) c.Error(errors.New("externo 0")).SetType(ErrorTypePublic)
c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate) c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate)
@ -505,7 +496,7 @@ func TestContextTypedError(t *testing.T) {
} }
func TestContextAbortWithError(t *testing.T) { func TestContextAbortWithError(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.AbortWithError(401, errors.New("bad input")).SetMeta("some input") c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")
c.Writer.WriteHeaderNow() c.Writer.WriteHeaderNow()
@ -515,7 +506,7 @@ func TestContextAbortWithError(t *testing.T) {
} }
func TestContextClientIP(t *testing.T) { func TestContextClientIP(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "/", nil) c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ") c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ")
@ -535,7 +526,7 @@ func TestContextClientIP(t *testing.T) {
} }
func TestContextContentType(t *testing.T) { func TestContextContentType(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "/", nil) c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request.Header.Set("Content-Type", "application/json; charset=utf-8") c.Request.Header.Set("Content-Type", "application/json; charset=utf-8")
@ -543,7 +534,7 @@ func TestContextContentType(t *testing.T) {
} }
func TestContextAutoBindJSON(t *testing.T) { func TestContextAutoBindJSON(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}")) c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
c.Request.Header.Add("Content-Type", MIMEJSON) c.Request.Header.Add("Content-Type", MIMEJSON)
@ -558,7 +549,7 @@ func TestContextAutoBindJSON(t *testing.T) {
} }
func TestContextBindWithJSON(t *testing.T) { func TestContextBindWithJSON(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}")) c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
c.Request.Header.Add("Content-Type", MIMEXML) // set fake content-type c.Request.Header.Add("Content-Type", MIMEXML) // set fake content-type
@ -573,7 +564,7 @@ func TestContextBindWithJSON(t *testing.T) {
} }
func TestContextBadAutoBind(t *testing.T) { func TestContextBadAutoBind(t *testing.T) {
c, w, _ := createTestContext() c, w, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}")) c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}"))
c.Request.Header.Add("Content-Type", MIMEJSON) c.Request.Header.Add("Content-Type", MIMEJSON)
var obj struct { var obj struct {
@ -592,7 +583,7 @@ func TestContextBadAutoBind(t *testing.T) {
} }
func TestContextGolangContext(t *testing.T) { func TestContextGolangContext(t *testing.T) {
c, _, _ := createTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}")) c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
assert.NoError(t, c.Err()) assert.NoError(t, c.Err())
assert.Nil(t, c.Done()) assert.Nil(t, c.Done())

14
test_helpers.go Normal file
View File

@ -0,0 +1,14 @@
package gin
import (
"net/http/httptest"
)
func CreateTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {
w = httptest.NewRecorder()
r = New()
c = r.allocateContext()
c.reset()
c.writermem.reset(w)
return
}