mirror of https://github.com/gin-gonic/gin.git
Merge branch 'danielalves-master' into develop
This commit is contained in:
commit
ee820830d5
|
@ -25,15 +25,6 @@ import (
|
|||
// 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)
|
||||
|
||||
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 {
|
||||
boundary := "--testboundary"
|
||||
body := new(bytes.Buffer)
|
||||
|
@ -82,7 +73,7 @@ func TestContextReset(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextHandlers(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
assert.Nil(t, c.handlers)
|
||||
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
|
||||
// current context and can be retrieved using Get.
|
||||
func TestContextSetGet(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Set("foo", "bar")
|
||||
|
||||
value, err := c.Get("foo")
|
||||
|
@ -119,7 +110,7 @@ func TestContextSetGet(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextSetGetValues(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Set("string", "this is a string")
|
||||
c.Set("int32", int32(-42))
|
||||
c.Set("int64", int64(42424242424242))
|
||||
|
@ -140,7 +131,7 @@ func TestContextSetGetValues(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextCopy(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.index = 2
|
||||
c.Request, _ = http.NewRequest("POST", "/hola", nil)
|
||||
c.handlers = HandlersChain{func(c *Context) {}}
|
||||
|
@ -159,7 +150,7 @@ func TestContextCopy(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextHandlerName(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.handlers = HandlersChain{func(c *Context) {}, 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) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10", nil)
|
||||
|
||||
assert.Equal(t, c.DefaultQuery("foo", "none"), "bar")
|
||||
|
@ -187,7 +178,7 @@ func TestContextQuery(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextQueryAndPostForm(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
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.Header.Add("Content-Type", MIMEPOSTForm)
|
||||
|
@ -227,7 +218,7 @@ func TestContextQueryAndPostForm(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextPostFormMultipart(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request = createMultipartRequest()
|
||||
|
||||
var obj struct {
|
||||
|
@ -250,7 +241,7 @@ func TestContextPostFormMultipart(t *testing.T) {
|
|||
// Tests that the response is serialized as JSON
|
||||
// and Content-Type is set to application/json
|
||||
func TestContextRenderJSON(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.JSON(201, H{"foo": "bar"})
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
|
@ -261,7 +252,7 @@ func TestContextRenderJSON(t *testing.T) {
|
|||
// Tests that the response is serialized as JSON
|
||||
// we change the content-type before
|
||||
func TestContextRenderAPIJSON(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.Header("Content-Type", "application/vnd.api+json")
|
||||
c.JSON(201, H{"foo": "bar"})
|
||||
|
||||
|
@ -273,7 +264,7 @@ func TestContextRenderAPIJSON(t *testing.T) {
|
|||
// Tests that the response is serialized as JSON
|
||||
// and Content-Type is set to application/json
|
||||
func TestContextRenderIndentedJSON(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
|
@ -284,7 +275,7 @@ func TestContextRenderIndentedJSON(t *testing.T) {
|
|||
// Tests that the response executes the templates
|
||||
// and responds with Content-Type set to text/html
|
||||
func TestContextRenderHTML(t *testing.T) {
|
||||
c, w, router := createTestContext()
|
||||
c, w, router := CreateTestContext()
|
||||
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
|
||||
router.SetHTMLTemplate(templ)
|
||||
|
||||
|
@ -298,7 +289,7 @@ func TestContextRenderHTML(t *testing.T) {
|
|||
// TestContextXML tests that the response is serialized as XML
|
||||
// and Content-Type is set to application/xml
|
||||
func TestContextRenderXML(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.XML(201, H{"foo": "bar"})
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
|
@ -309,7 +300,7 @@ func TestContextRenderXML(t *testing.T) {
|
|||
// TestContextString tests that the response is returned
|
||||
// with Content-Type set to text/plain
|
||||
func TestContextRenderString(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.String(201, "test %s %d", "string", 2)
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
|
@ -320,7 +311,7 @@ func TestContextRenderString(t *testing.T) {
|
|||
// TestContextString tests that the response is returned
|
||||
// with Content-Type set to text/html
|
||||
func TestContextRenderHTMLString(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
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`
|
||||
// with specified MIME type
|
||||
func TestContextRenderData(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.Data(201, "text/csv", []byte(`foo,bar`))
|
||||
|
||||
assert.Equal(t, w.Code, 201)
|
||||
|
@ -341,7 +332,7 @@ func TestContextRenderData(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextRenderSSE(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.SSEvent("float", 1.5)
|
||||
c.Render(-1, sse.Event{
|
||||
Id: "123",
|
||||
|
@ -356,7 +347,7 @@ func TestContextRenderSSE(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextRenderFile(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("GET", "/", nil)
|
||||
c.File("./gin.go")
|
||||
|
||||
|
@ -366,7 +357,7 @@ func TestContextRenderFile(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextHeaders(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Header("Content-Type", "text/plain")
|
||||
c.Header("X-Custom", "value")
|
||||
|
||||
|
@ -383,7 +374,7 @@ func TestContextHeaders(t *testing.T) {
|
|||
|
||||
// TODO
|
||||
func TestContextRenderRedirectWithRelativePath(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
|
||||
assert.Panics(t, func() { c.Redirect(299, "/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) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
|
||||
c.Redirect(302, "http://google.com")
|
||||
c.Writer.WriteHeaderNow()
|
||||
|
@ -405,7 +396,7 @@ func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextNegotiationFormat(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "", nil)
|
||||
|
||||
assert.Panics(t, func() { c.NegotiateFormat() })
|
||||
|
@ -414,7 +405,7 @@ func TestContextNegotiationFormat(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextNegotiationFormatWithAccept(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
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) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
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) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
assert.False(t, c.IsAborted())
|
||||
|
||||
c.Abort()
|
||||
|
@ -453,7 +444,7 @@ func TestContextIsAborted(t *testing.T) {
|
|||
// TestContextData tests that the response can be written from `bytesting`
|
||||
// with specified MIME type
|
||||
func TestContextAbortWithStatus(t *testing.T) {
|
||||
c, w, _ := createTestContext()
|
||||
c, w, _ := CreateTestContext()
|
||||
c.index = 4
|
||||
c.AbortWithStatus(401)
|
||||
c.Writer.WriteHeaderNow()
|
||||
|
@ -465,7 +456,7 @@ func TestContextAbortWithStatus(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextError(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
assert.Empty(t, c.Errors)
|
||||
|
||||
c.Error(errors.New("first error"))
|
||||
|
@ -491,7 +482,7 @@ func TestContextError(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextTypedError(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Error(errors.New("externo 0")).SetType(ErrorTypePublic)
|
||||
c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate)
|
||||
|
||||
|
@ -505,7 +496,7 @@ func TestContextTypedError(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.Writer.WriteHeaderNow()
|
||||
|
||||
|
@ -515,7 +506,7 @@ func TestContextAbortWithError(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextClientIP(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
|
||||
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) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
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) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||
c.Request.Header.Add("Content-Type", MIMEJSON)
|
||||
|
||||
|
@ -558,7 +549,7 @@ func TestContextAutoBindJSON(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.Header.Add("Content-Type", MIMEXML) // set fake content-type
|
||||
|
||||
|
@ -573,7 +564,7 @@ func TestContextBindWithJSON(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.Header.Add("Content-Type", MIMEJSON)
|
||||
var obj struct {
|
||||
|
@ -592,7 +583,7 @@ func TestContextBadAutoBind(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestContextGolangContext(t *testing.T) {
|
||||
c, _, _ := createTestContext()
|
||||
c, _, _ := CreateTestContext()
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||
assert.NoError(t, c.Err())
|
||||
assert.Nil(t, c.Done())
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue