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"
|
2015-05-26 15:21:35 +03:00
|
|
|
"mime/multipart"
|
2014-08-08 15:48:15 +04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-09-25 13:04:55 +03:00
|
|
|
"strings"
|
2014-08-08 15:48:15 +04:00
|
|
|
"testing"
|
2015-05-19 03:29:32 +03:00
|
|
|
"time"
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2015-05-19 03:29:32 +03:00
|
|
|
"github.com/manucorporat/sse"
|
2015-04-08 03:58:35 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-10-19 17:13:38 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-04-08 03:58:35 +03:00
|
|
|
)
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2016-10-19 17:13:38 +03:00
|
|
|
var _ context.Context = &Context{}
|
|
|
|
|
2015-05-07 17:00:37 +03:00
|
|
|
// Unit tests TODO
|
2015-04-09 13:15:02 +03:00
|
|
|
// func (c *Context) File(filepath string) {
|
|
|
|
// func (c *Context) Negotiate(code int, config Negotiate) {
|
|
|
|
// BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {
|
2015-05-07 17:00:37 +03:00
|
|
|
// test that information is not leaked when reusing Contexts (using the Pool)
|
2015-04-09 13:15:02 +03:00
|
|
|
|
2015-05-26 18:22:39 +03:00
|
|
|
func createMultipartRequest() *http.Request {
|
2015-05-26 15:21:35 +03:00
|
|
|
boundary := "--testboundary"
|
2015-05-26 18:22:39 +03:00
|
|
|
body := new(bytes.Buffer)
|
2015-05-26 15:21:35 +03:00
|
|
|
mw := multipart.NewWriter(body)
|
|
|
|
defer mw.Close()
|
|
|
|
|
2015-05-26 18:22:39 +03:00
|
|
|
must(mw.SetBoundary(boundary))
|
|
|
|
must(mw.WriteField("foo", "bar"))
|
2016-01-29 04:07:44 +03:00
|
|
|
must(mw.WriteField("bar", "10"))
|
2015-07-08 05:26:37 +03:00
|
|
|
must(mw.WriteField("bar", "foo2"))
|
|
|
|
must(mw.WriteField("array", "first"))
|
|
|
|
must(mw.WriteField("array", "second"))
|
2016-01-29 04:07:44 +03:00
|
|
|
must(mw.WriteField("id", ""))
|
2015-05-26 18:22:39 +03:00
|
|
|
req, err := http.NewRequest("POST", "/", body)
|
|
|
|
must(err)
|
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func must(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2015-05-26 15:21:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestContextReset(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
c := router.allocateContext()
|
2015-05-22 03:24:13 +03:00
|
|
|
assert.Equal(t, c.engine, router)
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
c.index = 2
|
|
|
|
c.Writer = &responseWriter{ResponseWriter: httptest.NewRecorder()}
|
2015-04-08 15:24:49 +03:00
|
|
|
c.Params = Params{Param{}}
|
2015-05-22 04:25:21 +03:00
|
|
|
c.Error(errors.New("test"))
|
2015-04-08 03:58:35 +03:00
|
|
|
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)
|
2015-05-19 01:49:01 +03:00
|
|
|
assert.Empty(t, c.Errors.Errors())
|
|
|
|
assert.Empty(t, c.Errors.ByType(ErrorTypeAny))
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Len(t, c.Params, 0)
|
2015-05-31 23:35:49 +03:00
|
|
|
assert.EqualValues(t, c.index, -1)
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Equal(t, c.Writer.(*responseWriter), &c.writermem)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
|
2015-07-03 05:20:18 +03:00
|
|
|
func TestContextHandlers(t *testing.T) {
|
2015-10-02 11:26:02 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-07-03 05:20:18 +03:00
|
|
|
assert.Nil(t, c.handlers)
|
|
|
|
assert.Nil(t, c.handlers.Last())
|
|
|
|
|
|
|
|
c.handlers = HandlersChain{}
|
|
|
|
assert.NotNil(t, c.handlers)
|
|
|
|
assert.Nil(t, c.handlers.Last())
|
|
|
|
|
|
|
|
f := func(c *Context) {}
|
|
|
|
g := func(c *Context) {}
|
|
|
|
|
|
|
|
c.handlers = HandlersChain{f}
|
|
|
|
compareFunc(t, f, c.handlers.Last())
|
|
|
|
|
|
|
|
c.handlers = HandlersChain{f, g}
|
|
|
|
compareFunc(t, g, c.handlers.Last())
|
|
|
|
}
|
|
|
|
|
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-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
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-05-07 17:00:37 +03:00
|
|
|
func TestContextSetGetValues(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-07 17:00:37 +03:00
|
|
|
c.Set("string", "this is a string")
|
|
|
|
c.Set("int32", int32(-42))
|
|
|
|
c.Set("int64", int64(42424242424242))
|
|
|
|
c.Set("uint64", uint64(42))
|
|
|
|
c.Set("float32", float32(4.2))
|
|
|
|
c.Set("float64", 4.2)
|
2015-05-11 02:04:08 +03:00
|
|
|
var a interface{} = 1
|
|
|
|
c.Set("intInterface", a)
|
2015-05-07 17:00:37 +03:00
|
|
|
|
|
|
|
assert.Exactly(t, c.MustGet("string").(string), "this is a string")
|
|
|
|
assert.Exactly(t, c.MustGet("int32").(int32), int32(-42))
|
|
|
|
assert.Exactly(t, c.MustGet("int64").(int64), int64(42424242424242))
|
|
|
|
assert.Exactly(t, c.MustGet("uint64").(uint64), uint64(42))
|
|
|
|
assert.Exactly(t, c.MustGet("float32").(float32), float32(4.2))
|
|
|
|
assert.Exactly(t, c.MustGet("float64").(float64), 4.2)
|
2015-05-11 02:04:08 +03:00
|
|
|
assert.Exactly(t, c.MustGet("intInterface").(int), 1)
|
|
|
|
|
2015-05-07 17:00:37 +03:00
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
func TestContextCopy(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-04-09 13:15:02 +03:00
|
|
|
c.index = 2
|
|
|
|
c.Request, _ = http.NewRequest("POST", "/hola", nil)
|
2015-05-07 12:30:01 +03:00
|
|
|
c.handlers = HandlersChain{func(c *Context) {}}
|
2015-04-09 13:15:02 +03:00
|
|
|
c.Params = Params{Param{Key: "foo", Value: "bar"}}
|
|
|
|
c.Set("foo", "bar")
|
|
|
|
|
|
|
|
cp := c.Copy()
|
|
|
|
assert.Nil(t, cp.handlers)
|
2015-05-05 16:06:38 +03:00
|
|
|
assert.Nil(t, cp.writermem.ResponseWriter)
|
|
|
|
assert.Equal(t, &cp.writermem, cp.Writer.(*responseWriter))
|
2015-04-09 13:15:02 +03:00
|
|
|
assert.Equal(t, cp.Request, c.Request)
|
2015-07-02 21:24:54 +03:00
|
|
|
assert.Equal(t, cp.index, abortIndex)
|
2015-04-09 13:15:02 +03:00
|
|
|
assert.Equal(t, cp.Keys, c.Keys)
|
2015-05-22 03:24:13 +03:00
|
|
|
assert.Equal(t, cp.engine, c.engine)
|
2015-04-09 13:15:02 +03:00
|
|
|
assert.Equal(t, cp.Params, c.Params)
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:44:52 +03:00
|
|
|
func TestContextHandlerName(t *testing.T) {
|
2015-10-02 11:26:02 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-06-25 20:44:52 +03:00
|
|
|
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}
|
|
|
|
|
2015-08-21 21:57:53 +03:00
|
|
|
assert.Regexp(t, "^(.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest$", c.HandlerName())
|
2015-06-25 20:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerNameTest(c *Context) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-26 18:22:39 +03:00
|
|
|
func TestContextQuery(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2016-01-29 04:07:44 +03:00
|
|
|
c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil)
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok := c.GetQuery("foo")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, value, "bar")
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.DefaultQuery("foo", "none"), "bar")
|
|
|
|
assert.Equal(t, c.Query("foo"), "bar")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok = c.GetQuery("page")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, value, "10")
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.DefaultQuery("page", "0"), "10")
|
|
|
|
assert.Equal(t, c.Query("page"), "10")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok = c.GetQuery("id")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Empty(t, value)
|
|
|
|
assert.Equal(t, c.DefaultQuery("id", "nada"), "")
|
|
|
|
assert.Empty(t, c.Query("id"))
|
|
|
|
|
|
|
|
value, ok = c.GetQuery("NoKey")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, value)
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.DefaultQuery("NoKey", "nada"), "nada")
|
|
|
|
assert.Empty(t, c.Query("NoKey"))
|
2016-01-29 04:07:44 +03:00
|
|
|
|
|
|
|
// postform should not mess
|
|
|
|
value, ok = c.GetPostForm("page")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, value)
|
|
|
|
assert.Empty(t, c.PostForm("foo"))
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
|
|
|
|
2015-05-26 18:22:39 +03:00
|
|
|
func TestContextQueryAndPostForm(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2016-01-29 04:07:44 +03:00
|
|
|
body := bytes.NewBufferString("foo=bar&page=11&both=&foo=second")
|
2015-07-08 05:26:37 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body)
|
2015-05-05 16:06:38 +03:00
|
|
|
c.Request.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.DefaultPostForm("foo", "none"), "bar")
|
|
|
|
assert.Equal(t, c.PostForm("foo"), "bar")
|
|
|
|
assert.Empty(t, c.Query("foo"))
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok := c.GetPostForm("page")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, value, "11")
|
2015-05-26 13:08:33 +03:00
|
|
|
assert.Equal(t, c.DefaultPostForm("page", "0"), "11")
|
|
|
|
assert.Equal(t, c.PostForm("page"), "11")
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.Query("page"), "")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok = c.GetPostForm("both")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Empty(t, value)
|
|
|
|
assert.Empty(t, c.PostForm("both"))
|
|
|
|
assert.Equal(t, c.DefaultPostForm("both", "nothing"), "")
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.Query("both"), "GET")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok = c.GetQuery("id")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, value, "main")
|
2015-05-26 18:22:39 +03:00
|
|
|
assert.Equal(t, c.DefaultPostForm("id", "000"), "000")
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.Query("id"), "main")
|
|
|
|
assert.Empty(t, c.PostForm("id"))
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok = c.GetQuery("NoKey")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, value)
|
|
|
|
value, ok = c.GetPostForm("NoKey")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, value)
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, c.DefaultPostForm("NoKey", "nada"), "nada")
|
2016-01-29 04:07:44 +03:00
|
|
|
assert.Equal(t, c.DefaultQuery("NoKey", "nothing"), "nothing")
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Empty(t, c.PostForm("NoKey"))
|
|
|
|
assert.Empty(t, c.Query("NoKey"))
|
2015-05-26 13:08:33 +03:00
|
|
|
|
2015-05-26 18:22:39 +03:00
|
|
|
var obj struct {
|
2015-07-08 05:26:37 +03:00
|
|
|
Foo string `form:"foo"`
|
|
|
|
ID string `form:"id"`
|
2016-01-29 04:07:44 +03:00
|
|
|
Page int `form:"page"`
|
2015-07-08 05:26:37 +03:00
|
|
|
Both string `form:"both"`
|
|
|
|
Array []string `form:"array[]"`
|
2015-05-26 18:22:39 +03:00
|
|
|
}
|
|
|
|
assert.NoError(t, c.Bind(&obj))
|
|
|
|
assert.Equal(t, obj.Foo, "bar")
|
2015-07-03 05:20:18 +03:00
|
|
|
assert.Equal(t, obj.ID, "main")
|
2016-01-29 04:07:44 +03:00
|
|
|
assert.Equal(t, obj.Page, 11)
|
|
|
|
assert.Equal(t, obj.Both, "")
|
2015-07-08 05:26:37 +03:00
|
|
|
assert.Equal(t, obj.Array, []string{"first", "second"})
|
2015-05-26 18:22:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextPostFormMultipart(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-26 18:22:39 +03:00
|
|
|
c.Request = createMultipartRequest()
|
|
|
|
|
|
|
|
var obj struct {
|
2016-01-29 04:07:44 +03:00
|
|
|
Foo string `form:"foo"`
|
|
|
|
Bar string `form:"bar"`
|
|
|
|
BarAsInt int `form:"bar"`
|
|
|
|
Array []string `form:"array"`
|
2016-04-15 02:16:46 +03:00
|
|
|
ID string `form:"id"`
|
2015-05-26 18:22:39 +03:00
|
|
|
}
|
|
|
|
assert.NoError(t, c.Bind(&obj))
|
|
|
|
assert.Equal(t, obj.Foo, "bar")
|
2016-01-29 04:07:44 +03:00
|
|
|
assert.Equal(t, obj.Bar, "10")
|
|
|
|
assert.Equal(t, obj.BarAsInt, 10)
|
2015-07-08 05:26:37 +03:00
|
|
|
assert.Equal(t, obj.Array, []string{"first", "second"})
|
2016-04-15 02:16:46 +03:00
|
|
|
assert.Equal(t, obj.ID, "")
|
2015-05-26 13:08:33 +03:00
|
|
|
|
2016-01-29 04:07:44 +03:00
|
|
|
value, ok := c.GetQuery("foo")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, value)
|
2015-05-26 18:22:39 +03:00
|
|
|
assert.Empty(t, c.Query("bar"))
|
2016-01-29 04:07:44 +03:00
|
|
|
assert.Equal(t, c.DefaultQuery("id", "nothing"), "nothing")
|
|
|
|
|
|
|
|
value, ok = c.GetPostForm("foo")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, value, "bar")
|
2015-05-26 18:22:39 +03:00
|
|
|
assert.Equal(t, c.PostForm("foo"), "bar")
|
2016-01-29 04:07:44 +03:00
|
|
|
|
|
|
|
value, ok = c.GetPostForm("array")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, value, "first")
|
2015-07-08 05:26:37 +03:00
|
|
|
assert.Equal(t, c.PostForm("array"), "first")
|
2016-01-29 04:07:44 +03:00
|
|
|
|
|
|
|
assert.Equal(t, c.DefaultPostForm("bar", "nothing"), "10")
|
|
|
|
|
|
|
|
value, ok = c.GetPostForm("id")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Empty(t, value)
|
|
|
|
assert.Empty(t, c.PostForm("id"))
|
|
|
|
assert.Empty(t, c.DefaultPostForm("id", "nothing"))
|
|
|
|
|
|
|
|
value, ok = c.GetPostForm("nokey")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, value)
|
|
|
|
assert.Equal(t, c.DefaultPostForm("nokey", "nothing"), "nothing")
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
|
|
|
|
2015-08-27 11:04:50 +03:00
|
|
|
func TestContextSetCookie(t *testing.T) {
|
2015-10-02 13:39:25 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-08-27 11:04:50 +03:00
|
|
|
c.SetCookie("user", "gin", 1, "/", "localhost", true, true)
|
2015-10-02 13:37:51 +03:00
|
|
|
assert.Equal(t, c.Writer.Header().Get("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure")
|
2015-08-27 11:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextGetCookie(t *testing.T) {
|
2015-10-02 13:39:25 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-08-27 11:04:50 +03:00
|
|
|
c.Request, _ = http.NewRequest("GET", "/get", nil)
|
|
|
|
c.Request.Header.Set("Cookie", "user=gin")
|
2016-01-28 02:35:09 +03:00
|
|
|
cookie, _ := c.Cookie("user")
|
2015-10-02 13:37:51 +03:00
|
|
|
assert.Equal(t, cookie, "gin")
|
2015-08-27 11:04:50 +03: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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
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-06-13 05:29:10 +03:00
|
|
|
// Tests that the response is serialized as JSON
|
|
|
|
// we change the content-type before
|
|
|
|
func TestContextRenderAPIJSON(t *testing.T) {
|
2015-10-02 11:26:02 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-06-13 05:29:10 +03:00
|
|
|
c.Header("Content-Type", "application/vnd.api+json")
|
|
|
|
c.JSON(201, H{"foo": "bar"})
|
|
|
|
|
|
|
|
assert.Equal(t, w.Code, 201)
|
|
|
|
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
|
|
|
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/vnd.api+json")
|
|
|
|
}
|
|
|
|
|
2015-05-11 02:04:08 +03:00
|
|
|
// Tests that the response is serialized as JSON
|
|
|
|
// and Content-Type is set to application/json
|
|
|
|
func TestContextRenderIndentedJSON(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-05-11 02:04:08 +03:00
|
|
|
c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})
|
|
|
|
|
|
|
|
assert.Equal(t, w.Code, 201)
|
|
|
|
assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\",\n \"nested\": {\n \"foo\": \"bar\"\n }\n}")
|
|
|
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, router := CreateTestContext()
|
2015-04-09 13:15:02 +03:00
|
|
|
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
|
2015-04-08 03:58:35 +03:00
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-05-18 16:45:24 +03:00
|
|
|
c.Header("Content-Type", "text/html; charset=utf-8")
|
|
|
|
c.String(201, "<html>%s %d</html>", "string", 3)
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
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")
|
2014-08-08 17:31:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Data(201, "text/csv", []byte(`foo,bar`))
|
2014-08-08 17:31:01 +04:00
|
|
|
|
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")
|
2014-08-08 17:31:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:29:32 +03:00
|
|
|
func TestContextRenderSSE(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.SSEvent("float", 1.5)
|
|
|
|
c.Render(-1, sse.Event{
|
|
|
|
Id: "123",
|
|
|
|
Data: "text",
|
|
|
|
})
|
|
|
|
c.SSEvent("chat", H{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "foo",
|
|
|
|
})
|
|
|
|
|
2015-09-25 13:04:55 +03:00
|
|
|
assert.Equal(t, strings.Replace(w.Body.String(), " ", "", -1), strings.Replace("event:float\ndata:1.5\n\nid:123\ndata:text\n\nevent:chat\ndata:{\"bar\":\"foo\",\"foo\":\"bar\"}\n\n", " ", "", -1))
|
2015-05-19 03:29:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextRenderFile(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.Request, _ = http.NewRequest("GET", "/", nil)
|
|
|
|
c.File("./gin.go")
|
|
|
|
|
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Contains(t, w.Body.String(), "func New() *Engine {")
|
|
|
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
|
|
|
|
}
|
|
|
|
|
2016-04-15 00:47:49 +03:00
|
|
|
// TestContextRenderYAML tests that the response is serialized as YAML
|
|
|
|
// and Content-Type is set to application/x-yaml
|
|
|
|
func TestContextRenderYAML(t *testing.T) {
|
|
|
|
c, w, _ := CreateTestContext()
|
|
|
|
c.YAML(201, H{"foo": "bar"})
|
|
|
|
|
|
|
|
assert.Equal(t, w.Code, 201)
|
|
|
|
assert.Equal(t, w.Body.String(), "foo: bar\n")
|
|
|
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/x-yaml; charset=utf-8")
|
|
|
|
}
|
|
|
|
|
2015-05-18 16:45:24 +03:00
|
|
|
func TestContextHeaders(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-18 16:45:24 +03:00
|
|
|
c.Header("Content-Type", "text/plain")
|
|
|
|
c.Header("X-Custom", "value")
|
|
|
|
|
|
|
|
assert.Equal(t, c.Writer.Header().Get("Content-Type"), "text/plain")
|
|
|
|
assert.Equal(t, c.Writer.Header().Get("X-Custom"), "value")
|
|
|
|
|
|
|
|
c.Header("Content-Type", "text/html")
|
|
|
|
c.Header("X-Custom", "")
|
|
|
|
|
|
|
|
assert.Equal(t, c.Writer.Header().Get("Content-Type"), "text/html")
|
|
|
|
_, exist := c.Writer.Header()["X-Custom"]
|
|
|
|
assert.False(t, exist)
|
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
// TODO
|
|
|
|
func TestContextRenderRedirectWithRelativePath(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
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") })
|
|
|
|
|
2016-01-28 02:34:05 +03:00
|
|
|
c.Redirect(301, "/path")
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Writer.WriteHeaderNow()
|
2016-01-28 02:34:05 +03:00
|
|
|
assert.Equal(t, w.Code, 301)
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Equal(t, w.Header().Get("Location"), "/path")
|
2014-08-08 17:31:01 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-28 02:34:05 +03:00
|
|
|
func TestContextRenderRedirectWith201(t *testing.T) {
|
|
|
|
c, w, _ := CreateTestContext()
|
|
|
|
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
|
|
|
|
c.Redirect(201, "/resource")
|
|
|
|
c.Writer.WriteHeaderNow()
|
|
|
|
|
|
|
|
assert.Equal(t, w.Code, 201)
|
|
|
|
assert.Equal(t, w.Header().Get("Location"), "/resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextRenderRedirectAll(t *testing.T) {
|
|
|
|
c, _, _ := CreateTestContext()
|
|
|
|
c.Request, _ = http.NewRequest("POST", "http://example.com", nil)
|
|
|
|
assert.Panics(t, func() { c.Redirect(200, "/resource") })
|
|
|
|
assert.Panics(t, func() { c.Redirect(202, "/resource") })
|
|
|
|
assert.Panics(t, func() { c.Redirect(299, "/resource") })
|
|
|
|
assert.Panics(t, func() { c.Redirect(309, "/resource") })
|
|
|
|
assert.NotPanics(t, func() { c.Redirect(300, "/resource") })
|
|
|
|
assert.NotPanics(t, func() { c.Redirect(308, "/resource") })
|
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestContextNegotiationFormat(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "", nil)
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
assert.Panics(t, func() { c.NegotiateFormat() })
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
2015-04-08 03:58:35 +03:00
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
2015-04-08 03:58:35 +03:00
|
|
|
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-07-02 14:27:22 +03:00
|
|
|
func TestContextIsAborted(t *testing.T) {
|
2015-10-02 11:26:02 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-07-02 14:27:22 +03:00
|
|
|
assert.False(t, c.IsAborted())
|
|
|
|
|
|
|
|
c.Abort()
|
|
|
|
assert.True(t, c.IsAborted())
|
|
|
|
|
2015-07-02 17:37:35 +03:00
|
|
|
c.Next()
|
2015-07-02 19:45:09 +03:00
|
|
|
assert.True(t, c.IsAborted())
|
2015-07-02 14:27:22 +03:00
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
c.index++
|
2015-07-02 14:27:22 +03:00
|
|
|
assert.True(t, c.IsAborted())
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
c.index = 4
|
|
|
|
c.AbortWithStatus(401)
|
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
assert.Equal(t, c.index, abortIndex)
|
2015-04-08 03:58:35 +03:00
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-22 17:39:15 +03:00
|
|
|
assert.Empty(t, c.Errors)
|
2015-04-09 13:15:02 +03:00
|
|
|
|
2015-05-22 17:39:15 +03:00
|
|
|
c.Error(errors.New("first error"))
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Len(t, c.Errors, 1)
|
2015-05-22 17:39:15 +03:00
|
|
|
assert.Equal(t, c.Errors.String(), "Error #01: first error\n")
|
2014-08-11 14:25:52 +04:00
|
|
|
|
2015-05-22 17:39:15 +03:00
|
|
|
c.Error(&Error{
|
|
|
|
Err: errors.New("second error"),
|
|
|
|
Meta: "some data 2",
|
|
|
|
Type: ErrorTypePublic,
|
|
|
|
})
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Len(t, c.Errors, 2)
|
2014-08-11 14:25:52 +04:00
|
|
|
|
2015-05-22 04:43:39 +03:00
|
|
|
assert.Equal(t, c.Errors[0].Err, errors.New("first error"))
|
2015-05-22 17:39:15 +03:00
|
|
|
assert.Nil(t, c.Errors[0].Meta)
|
|
|
|
assert.Equal(t, c.Errors[0].Type, ErrorTypePrivate)
|
2014-08-11 14:25:52 +04:00
|
|
|
|
2015-05-22 04:43:39 +03:00
|
|
|
assert.Equal(t, c.Errors[1].Err, errors.New("second error"))
|
2015-05-22 17:39:15 +03:00
|
|
|
assert.Equal(t, c.Errors[1].Meta, "some data 2")
|
|
|
|
assert.Equal(t, c.Errors[1].Type, ErrorTypePublic)
|
|
|
|
|
|
|
|
assert.Equal(t, c.Errors.Last(), c.Errors[1])
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
2014-08-11 14:25:52 +04:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestContextTypedError(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-22 17:39:15 +03:00
|
|
|
c.Error(errors.New("externo 0")).SetType(ErrorTypePublic)
|
|
|
|
c.Error(errors.New("interno 0")).SetType(ErrorTypePrivate)
|
2015-05-22 04:25:21 +03:00
|
|
|
|
|
|
|
for _, err := range c.Errors.ByType(ErrorTypePublic) {
|
2015-05-22 17:39:15 +03:00
|
|
|
assert.Equal(t, err.Type, ErrorTypePublic)
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
2015-05-22 04:25:21 +03:00
|
|
|
for _, err := range c.Errors.ByType(ErrorTypePrivate) {
|
2015-05-22 17:39:15 +03:00
|
|
|
assert.Equal(t, err.Type, ErrorTypePrivate)
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
2015-05-22 17:39:15 +03:00
|
|
|
assert.Equal(t, c.Errors.Errors(), []string{"externo 0", "interno 0"})
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:39:15 +03:00
|
|
|
func TestContextAbortWithError(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-05-22 17:39:15 +03:00
|
|
|
c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")
|
2014-08-11 14:25:52 +04:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Equal(t, w.Code, 401)
|
2015-07-02 21:24:54 +03:00
|
|
|
assert.Equal(t, c.index, abortIndex)
|
2015-04-08 03:58:35 +03:00
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
2014-08-11 14:25:52 +04:00
|
|
|
|
2015-05-24 16:35:08 +03:00
|
|
|
c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ")
|
2015-06-04 14:15:22 +03:00
|
|
|
c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20, 30.30.30.30")
|
2015-07-01 21:48:21 +03:00
|
|
|
c.Request.RemoteAddr = " 40.40.40.40:42123 "
|
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")
|
2015-05-24 16:35:08 +03:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Request.Header.Del("X-Real-IP")
|
|
|
|
assert.Equal(t, c.ClientIP(), "20.20.20.20")
|
2015-05-24 16:35:08 +03:00
|
|
|
|
2015-06-04 14:15:22 +03:00
|
|
|
c.Request.Header.Set("X-Forwarded-For", "30.30.30.30 ")
|
2015-05-24 16:28:58 +03:00
|
|
|
assert.Equal(t, c.ClientIP(), "30.30.30.30")
|
2015-05-24 16:35:08 +03:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Request.Header.Del("X-Forwarded-For")
|
|
|
|
assert.Equal(t, c.ClientIP(), "40.40.40.40")
|
2015-02-10 02:13:05 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestContextContentType(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
2015-02-10 02:13:05 +03:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Equal(t, c.ContentType(), "application/json")
|
|
|
|
}
|
2015-02-10 02:13:05 +03:00
|
|
|
|
2015-05-26 18:22:39 +03:00
|
|
|
func TestContextAutoBindJSON(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Request.Header.Add("Content-Type", MIMEJSON)
|
2015-05-26 18:22:39 +03:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
var obj struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
Bar string `json:"bar"`
|
|
|
|
}
|
2015-05-22 04:25:21 +03:00
|
|
|
assert.NoError(t, c.Bind(&obj))
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Equal(t, obj.Bar, "foo")
|
|
|
|
assert.Equal(t, obj.Foo, "bar")
|
2015-05-26 18:22:39 +03:00
|
|
|
assert.Empty(t, c.Errors)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
2015-02-10 02:13:05 +03:00
|
|
|
|
2015-05-26 18:22:39 +03:00
|
|
|
func TestContextBindWithJSON(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-05-26 18:22:39 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
|
|
|
c.Request.Header.Add("Content-Type", MIMEXML) // set fake content-type
|
2015-05-26 15:21:35 +03:00
|
|
|
|
|
|
|
var obj struct {
|
2015-05-26 18:22:39 +03:00
|
|
|
Foo string `json:"foo"`
|
|
|
|
Bar string `json:"bar"`
|
2015-05-26 15:21:35 +03:00
|
|
|
}
|
2015-05-26 18:22:39 +03:00
|
|
|
assert.NoError(t, c.BindJSON(&obj))
|
2015-05-26 15:21:35 +03:00
|
|
|
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) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, w, _ := CreateTestContext()
|
2015-04-08 03:58:35 +03:00
|
|
|
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-02-10 02:13:05 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.False(t, c.IsAborted())
|
2015-05-22 04:25:21 +03:00
|
|
|
assert.Error(t, c.Bind(&obj))
|
2015-04-08 03:58:35 +03:00
|
|
|
c.Writer.WriteHeaderNow()
|
2015-02-10 02:13:05 +03:00
|
|
|
|
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
|
|
|
}
|
2014-12-21 15:42:48 +03:00
|
|
|
|
2015-05-19 03:29:32 +03:00
|
|
|
func TestContextGolangContext(t *testing.T) {
|
2015-06-02 23:16:51 +03:00
|
|
|
c, _, _ := CreateTestContext()
|
2015-05-19 03:29:32 +03:00
|
|
|
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
|
|
|
assert.NoError(t, c.Err())
|
|
|
|
assert.Nil(t, c.Done())
|
|
|
|
ti, ok := c.Deadline()
|
|
|
|
assert.Equal(t, ti, time.Time{})
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Equal(t, c.Value(0), c.Request)
|
|
|
|
assert.Nil(t, c.Value("foo"))
|
|
|
|
|
|
|
|
c.Set("foo", "bar")
|
|
|
|
assert.Equal(t, c.Value("foo"), "bar")
|
|
|
|
assert.Nil(t, c.Value(1))
|
|
|
|
}
|