gin/context_test.go

321 lines
9.8 KiB
Go
Raw Normal View History

2014-08-29 21:49:50 +04:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
2014-08-08 15:48:15 +04:00
package gin
import (
2014-08-11 14:25:52 +04:00
"bytes"
2014-08-08 15:48:15 +04:00
"errors"
"html/template"
"net/http"
"net/http/httptest"
"testing"
2015-04-08 03:58:35 +03:00
"github.com/gin-gonic/gin/binding"
"github.com/stretchr/testify/assert"
)
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
func createTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {
w = httptest.NewRecorder()
r = New()
c = r.allocateContext()
c.reset()
c.writermem.reset(w)
return
}
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
func TestContextReset(t *testing.T) {
router := New()
c := router.allocateContext()
assert.Equal(t, c.Engine, router)
c.index = 2
c.Writer = &responseWriter{ResponseWriter: httptest.NewRecorder()}
2015-04-08 15:24:49 +03:00
c.Params = Params{Param{}}
2015-04-08 03:58:35 +03:00
c.Error(errors.New("test"), nil)
c.Set("foo", "bar")
c.reset()
assert.False(t, c.IsAborted())
assert.Nil(t, c.Keys)
assert.Nil(t, c.Accepted)
assert.Len(t, c.Errors, 0)
assert.Len(t, c.Params, 0)
assert.Equal(t, c.index, -1)
assert.Equal(t, c.Writer.(*responseWriter), &c.writermem)
2014-08-08 15:48:15 +04:00
}
// TestContextSetGet tests that a parameter is set correctly on the
// current context and can be retrieved using Get.
func TestContextSetGet(t *testing.T) {
2015-04-08 03:58:35 +03:00
c, _, _ := createTestContext()
c.Set("foo", "bar")
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
value, err := c.Get("foo")
assert.Equal(t, value, "bar")
assert.True(t, err)
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
value, err = c.Get("foo2")
assert.Nil(t, value)
assert.False(t, err)
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.MustGet("foo"), "bar")
assert.Panics(t, func() { c.MustGet("no_exist") })
}
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
// 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.JSON(201, H{"foo": "bar"})
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
2014-08-08 15:48:15 +04:00
}
2015-04-08 03:58:35 +03:00
// Tests that the response executes the templates
2014-08-08 15:48:15 +04:00
// and responds with Content-Type set to text/html
2015-04-08 03:58:35 +03:00
func TestContextRenderHTML(t *testing.T) {
c, w, router := createTestContext()
templ, _ := template.New("t").Parse(`Hello {{.name}}`)
router.SetHTMLTemplate(templ)
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
c.HTML(201, "t", H{"name": "alexandernyquist"})
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
}
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
// 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.XML(201, H{"foo": "bar"})
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/xml; charset=utf-8")
2014-08-08 15:48:15 +04:00
}
// TestContextString tests that the response is returned
// with Content-Type set to text/plain
2015-04-08 03:58:35 +03:00
func TestContextRenderString(t *testing.T) {
c, w, _ := createTestContext()
c.String(201, "test %s %d", "string", 2)
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "test string 2")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
2014-08-08 15:48:15 +04:00
}
2015-04-08 03:58:35 +03:00
// TestContextString tests that the response is returned
// with Content-Type set to text/html
func TestContextRenderHTMLString(t *testing.T) {
c, w, _ := createTestContext()
c.HTMLString(201, "<html>%s %d</html>", "string", 3)
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "<html>string 3</html>")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
}
// TestContextData tests that the response can be written from `bytesting`
// with specified MIME type
2015-04-08 03:58:35 +03:00
func TestContextRenderData(t *testing.T) {
c, w, _ := createTestContext()
c.Data(201, "text/csv", []byte(`foo,bar`))
2015-04-08 03:58:35 +03:00
assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "foo,bar")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/csv")
}
2015-04-08 03:58:35 +03:00
// TODO
func TestContextRenderRedirectWithRelativePath(t *testing.T) {
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") })
c.Redirect(302, "/path")
c.Writer.WriteHeaderNow()
assert.Equal(t, w.Code, 302)
assert.Equal(t, w.Header().Get("Location"), "/path")
}
2015-04-08 03:58:35 +03:00
func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
c, w, _ := createTestContext()
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
c.Redirect(302, "http://google.com")
c.Writer.WriteHeaderNow()
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, w.Code, 302)
assert.Equal(t, w.Header().Get("Location"), "http://google.com")
2014-08-08 15:48:15 +04:00
}
2015-04-08 03:58:35 +03:00
func TestContextNegotiationFormat(t *testing.T) {
c, _, _ := createTestContext()
c.Request, _ = http.NewRequest("POST", "", nil)
2014-08-08 15:48:15 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON)
assert.Equal(t, c.NegotiateFormat(MIMEHTML, MIMEJSON), MIMEHTML)
2014-08-08 15:48:15 +04:00
}
2015-04-08 03:58:35 +03:00
func TestContextNegotiationFormatWithAccept(t *testing.T) {
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")
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEXML)
assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEHTML)
assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")
}
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
func TestContextNegotiationFormatCustum(t *testing.T) {
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")
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
c.Accepted = nil
c.SetAccepted(MIMEJSON, MIMEXML)
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON)
assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEXML)
assert.Equal(t, c.NegotiateFormat(MIMEJSON), MIMEJSON)
2014-08-11 14:25:52 +04:00
}
2015-04-08 03:58:35 +03:00
// TestContextData tests that the response can be written from `bytesting`
// with specified MIME type
func TestContextAbortWithStatus(t *testing.T) {
c, w, _ := createTestContext()
c.index = 4
c.AbortWithStatus(401)
c.Writer.WriteHeaderNow()
assert.Equal(t, c.index, AbortIndex)
assert.Equal(t, c.Writer.Status(), 401)
assert.Equal(t, w.Code, 401)
assert.True(t, c.IsAborted())
}
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
func TestContextError(t *testing.T) {
c, _, _ := createTestContext()
c.Error(errors.New("first error"), "some data")
assert.Equal(t, c.LastError().Error(), "first error")
assert.Len(t, c.Errors, 1)
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
c.Error(errors.New("second error"), "some data 2")
assert.Equal(t, c.LastError().Error(), "second error")
assert.Len(t, c.Errors, 2)
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.Errors[0].Err, "first error")
assert.Equal(t, c.Errors[0].Meta, "some data")
assert.Equal(t, c.Errors[0].Type, ErrorTypeExternal)
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.Errors[1].Err, "second error")
assert.Equal(t, c.Errors[1].Meta, "some data 2")
assert.Equal(t, c.Errors[1].Type, ErrorTypeExternal)
}
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
func TestContextTypedError(t *testing.T) {
c, _, _ := createTestContext()
c.ErrorTyped(errors.New("externo 0"), ErrorTypeExternal, nil)
c.ErrorTyped(errors.New("externo 1"), ErrorTypeExternal, nil)
c.ErrorTyped(errors.New("interno 0"), ErrorTypeInternal, nil)
c.ErrorTyped(errors.New("externo 2"), ErrorTypeExternal, nil)
c.ErrorTyped(errors.New("interno 1"), ErrorTypeInternal, nil)
c.ErrorTyped(errors.New("interno 2"), ErrorTypeInternal, nil)
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
for _, err := range c.Errors.ByType(ErrorTypeExternal) {
assert.Equal(t, err.Type, ErrorTypeExternal)
2014-08-11 14:25:52 +04:00
}
2015-04-08 03:58:35 +03:00
for _, err := range c.Errors.ByType(ErrorTypeInternal) {
assert.Equal(t, err.Type, ErrorTypeInternal)
2014-08-11 14:25:52 +04:00
}
}
2015-04-08 03:58:35 +03:00
func TestContextFail(t *testing.T) {
c, w, _ := createTestContext()
c.Fail(401, errors.New("bad input"))
c.Writer.WriteHeaderNow()
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, w.Code, 401)
assert.Equal(t, c.LastError().Error(), "bad input")
assert.Equal(t, c.index, AbortIndex)
assert.True(t, c.IsAborted())
}
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
func TestContextClientIP(t *testing.T) {
c, _, _ := createTestContext()
c.Request, _ = http.NewRequest("POST", "", nil)
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
c.Request.Header.Set("X-Real-IP", "10.10.10.10")
c.Request.Header.Set("X-Forwarded-For", "20.20.20.20 , 30.30.30.30")
c.Request.RemoteAddr = "40.40.40.40"
2014-08-11 14:25:52 +04:00
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.ClientIP(), "10.10.10.10")
c.Request.Header.Del("X-Real-IP")
assert.Equal(t, c.ClientIP(), "20.20.20.20")
c.Request.Header.Del("X-Forwarded-For")
assert.Equal(t, c.ClientIP(), "40.40.40.40")
}
2015-04-08 03:58:35 +03:00
func TestContextContentType(t *testing.T) {
c, _, _ := createTestContext()
c.Request, _ = http.NewRequest("POST", "", nil)
c.Request.Header.Set("Content-Type", "application/json; charset=utf-8")
2015-04-08 03:58:35 +03:00
assert.Equal(t, c.ContentType(), "application/json")
}
2015-04-08 03:58:35 +03:00
func TestContextAutoBind(t *testing.T) {
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 {
Foo string `json:"foo"`
Bar string `json:"bar"`
}
assert.True(t, c.Bind(&obj))
assert.Equal(t, obj.Bar, "foo")
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, w.Body.Len(), 0)
}
2015-04-08 03:58:35 +03:00
func TestContextBadAutoBind(t *testing.T) {
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 {
Foo string `json:"foo"`
Bar string `json:"bar"`
}
2015-04-08 03:58:35 +03:00
assert.False(t, c.IsAborted())
assert.False(t, c.Bind(&obj))
c.Writer.WriteHeaderNow()
2015-04-08 03:58:35 +03:00
assert.Empty(t, obj.Bar)
assert.Empty(t, obj.Foo)
assert.Equal(t, w.Code, 400)
assert.True(t, c.IsAborted())
2014-08-11 14:25:52 +04:00
}
2015-04-08 03:58:35 +03:00
func TestContextBindWith(t *testing.T) {
c, w, _ := createTestContext()
c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
c.Request.Header.Add("Content-Type", MIMEXML)
var obj struct {
Foo string `json:"foo"`
Bar string `json:"bar"`
}
assert.True(t, c.BindWith(&obj, binding.JSON))
assert.Equal(t, obj.Bar, "foo")
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, w.Body.Len(), 0)
}