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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestContextParamsGet tests that a parameter can be parsed from the URL.
|
|
|
|
func TestContextParamsByName(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test/alexandernyquist", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
name := ""
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
r.GET("/test/:name", func(c *Context) {
|
|
|
|
name = c.Params.ByName("name")
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if name != "alexandernyquist" {
|
|
|
|
t.Errorf("Url parameter was not correctly parsed. Should be alexandernyquist, was %s.", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestContextSetGet tests that a parameter is set correctly on the
|
|
|
|
// current context and can be retrieved using Get.
|
|
|
|
func TestContextSetGet(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
r.GET("/test", func(c *Context) {
|
|
|
|
// Key should be lazily created
|
|
|
|
if c.Keys != nil {
|
|
|
|
t.Error("Keys should be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set
|
|
|
|
c.Set("foo", "bar")
|
|
|
|
|
2015-03-31 21:12:10 +03:00
|
|
|
v, ok := c.Get("foo")
|
|
|
|
if !ok {
|
2014-08-08 15:48:15 +04:00
|
|
|
t.Errorf("Error on exist key")
|
|
|
|
}
|
|
|
|
if v != "bar" {
|
|
|
|
t.Errorf("Value should be bar, was %s", v)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestContextJSON tests that the response is serialized as JSON
|
|
|
|
// and Content-Type is set to application/json
|
|
|
|
func TestContextJSON(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
r.GET("/test", func(c *Context) {
|
|
|
|
c.JSON(200, H{"foo": "bar"})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Body.String() != "{\"foo\":\"bar\"}\n" {
|
|
|
|
t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-01-30 05:14:09 +03:00
|
|
|
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
2014-08-08 15:48:15 +04:00
|
|
|
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestContextHTML tests that the response executes the templates
|
|
|
|
// and responds with Content-Type set to text/html
|
|
|
|
func TestContextHTML(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
templ, _ := template.New("t").Parse(`Hello {{.Name}}`)
|
|
|
|
r.SetHTMLTemplate(templ)
|
|
|
|
|
|
|
|
type TestData struct{ Name string }
|
|
|
|
|
|
|
|
r.GET("/test", func(c *Context) {
|
|
|
|
c.HTML(200, "t", TestData{"alexandernyquist"})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Body.String() != "Hello alexandernyquist" {
|
|
|
|
t.Errorf("Response should be Hello alexandernyquist, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-01-30 05:14:09 +03:00
|
|
|
if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
|
2014-08-08 15:48:15 +04:00
|
|
|
t.Errorf("Content-Type should be text/html, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestContextString tests that the response is returned
|
|
|
|
// with Content-Type set to text/plain
|
|
|
|
func TestContextString(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
r.GET("/test", func(c *Context) {
|
|
|
|
c.String(200, "test")
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Body.String() != "test" {
|
|
|
|
t.Errorf("Response should be test, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-01-30 05:14:09 +03:00
|
|
|
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
|
2014-08-08 15:48:15 +04:00
|
|
|
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 17:31:01 +04:00
|
|
|
// TestContextXML tests that the response is serialized as XML
|
|
|
|
// and Content-Type is set to application/xml
|
|
|
|
func TestContextXML(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 17:31:01 +04:00
|
|
|
r.GET("/test", func(c *Context) {
|
|
|
|
c.XML(200, H{"foo": "bar"})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Body.String() != "<map><foo>bar</foo></map>" {
|
|
|
|
t.Errorf("Response should be <map><foo>bar</foo></map>, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-01-30 05:14:09 +03:00
|
|
|
if w.HeaderMap.Get("Content-Type") != "application/xml; charset=utf-8" {
|
2014-08-08 17:31:01 +04:00
|
|
|
t.Errorf("Content-Type should be application/xml, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestContextData tests that the response can be written from `bytesting`
|
|
|
|
// with specified MIME type
|
|
|
|
func TestContextData(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test/csv", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 17:31:01 +04:00
|
|
|
r.GET("/test/csv", func(c *Context) {
|
|
|
|
c.Data(200, "text/csv", []byte(`foo,bar`))
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Body.String() != "foo,bar" {
|
|
|
|
t.Errorf("Response should be foo&bar, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.HeaderMap.Get("Content-Type") != "text/csv" {
|
|
|
|
t.Errorf("Content-Type should be text/csv, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContextFile(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test/file", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 17:31:01 +04:00
|
|
|
r.GET("/test/file", func(c *Context) {
|
|
|
|
c.File("./gin.go")
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
bodyAsString := w.Body.String()
|
|
|
|
|
|
|
|
if len(bodyAsString) == 0 {
|
|
|
|
t.Errorf("Got empty body instead of file data")
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
|
|
|
|
t.Errorf("Content-Type should be text/plain; charset=utf-8, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 15:48:15 +04:00
|
|
|
// TestHandlerFunc - ensure that custom middleware works properly
|
|
|
|
func TestHandlerFunc(t *testing.T) {
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
var stepsPassed int = 0
|
|
|
|
|
|
|
|
r.Use(func(context *Context) {
|
|
|
|
stepsPassed += 1
|
|
|
|
context.Next()
|
|
|
|
stepsPassed += 1
|
|
|
|
})
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 404 {
|
2015-03-23 06:39:53 +03:00
|
|
|
t.Errorf("Response code should be Not found, was: %d", w.Code)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if stepsPassed != 2 {
|
2015-03-23 06:39:53 +03:00
|
|
|
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
|
|
|
|
func TestBadAbortHandlersChain(t *testing.T) {
|
2014-08-18 21:48:48 +04:00
|
|
|
// SETUP
|
2014-08-08 15:48:15 +04:00
|
|
|
var stepsPassed int = 0
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
|
|
|
r.Use(func(c *Context) {
|
2014-08-08 15:48:15 +04:00
|
|
|
stepsPassed += 1
|
2014-08-18 21:48:48 +04:00
|
|
|
c.Next()
|
2014-08-08 15:48:15 +04:00
|
|
|
stepsPassed += 1
|
|
|
|
// after check and abort
|
2014-10-08 23:37:26 +04:00
|
|
|
c.AbortWithStatus(409)
|
2014-08-18 21:48:48 +04:00
|
|
|
})
|
|
|
|
r.Use(func(c *Context) {
|
|
|
|
stepsPassed += 1
|
|
|
|
c.Next()
|
|
|
|
stepsPassed += 1
|
2014-10-08 23:37:26 +04:00
|
|
|
c.AbortWithStatus(403)
|
2014-08-18 21:48:48 +04:00
|
|
|
})
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "GET", "/")
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
// TEST
|
|
|
|
if w.Code != 409 {
|
|
|
|
t.Errorf("Response code should be Forbiden, was: %d", w.Code)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
if stepsPassed != 4 {
|
2014-08-18 21:48:48 +04:00
|
|
|
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
|
|
|
|
func TestAbortHandlersChain(t *testing.T) {
|
2014-08-18 21:48:48 +04:00
|
|
|
// SETUP
|
2014-08-08 15:48:15 +04:00
|
|
|
var stepsPassed int = 0
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
r.Use(func(context *Context) {
|
|
|
|
stepsPassed += 1
|
2014-10-08 23:37:26 +04:00
|
|
|
context.AbortWithStatus(409)
|
2014-08-18 21:48:48 +04:00
|
|
|
})
|
|
|
|
r.Use(func(context *Context) {
|
|
|
|
stepsPassed += 1
|
|
|
|
context.Next()
|
|
|
|
stepsPassed += 1
|
|
|
|
})
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "GET", "/")
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
// TEST
|
2014-08-08 15:48:15 +04:00
|
|
|
if w.Code != 409 {
|
2014-08-18 21:48:48 +04:00
|
|
|
t.Errorf("Response code should be Conflict, was: %d", w.Code)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
if stepsPassed != 1 {
|
2014-08-18 21:48:48 +04:00
|
|
|
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
|
|
|
|
// as well as Abort
|
|
|
|
func TestFailHandlersChain(t *testing.T) {
|
2014-08-18 21:48:48 +04:00
|
|
|
// SETUP
|
2014-08-08 15:48:15 +04:00
|
|
|
var stepsPassed int = 0
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-08 15:48:15 +04:00
|
|
|
r.Use(func(context *Context) {
|
|
|
|
stepsPassed += 1
|
|
|
|
context.Fail(500, errors.New("foo"))
|
2014-08-18 21:48:48 +04:00
|
|
|
})
|
|
|
|
r.Use(func(context *Context) {
|
|
|
|
stepsPassed += 1
|
|
|
|
context.Next()
|
|
|
|
stepsPassed += 1
|
|
|
|
})
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "GET", "/")
|
2014-08-08 15:48:15 +04:00
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
// TEST
|
2014-08-08 15:48:15 +04:00
|
|
|
if w.Code != 500 {
|
2014-08-18 21:48:48 +04:00
|
|
|
t.Errorf("Response code should be Server error, was: %d", w.Code)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
if stepsPassed != 1 {
|
2014-08-18 21:48:48 +04:00
|
|
|
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
2014-08-08 15:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 14:25:52 +04:00
|
|
|
|
|
|
|
func TestBindingJSON(t *testing.T) {
|
|
|
|
|
|
|
|
body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}"))
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-11 14:25:52 +04:00
|
|
|
r.POST("/binding/json", func(c *Context) {
|
|
|
|
var body struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
if c.Bind(&body) {
|
|
|
|
c.JSON(200, H{"parsed": body.Foo})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", "/binding/json", body)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 200 {
|
2015-03-23 06:39:53 +03:00
|
|
|
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if w.Body.String() != "{\"parsed\":\"bar\"}\n" {
|
|
|
|
t.Errorf("Response should be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-01-30 05:14:09 +03:00
|
|
|
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
2014-08-11 14:25:52 +04:00
|
|
|
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingJSONEncoding(t *testing.T) {
|
|
|
|
|
|
|
|
body := bytes.NewBuffer([]byte("{\"foo\":\"嘉\"}"))
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-11 14:25:52 +04:00
|
|
|
r.POST("/binding/json", func(c *Context) {
|
|
|
|
var body struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
if c.Bind(&body) {
|
|
|
|
c.JSON(200, H{"parsed": body.Foo})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", "/binding/json", body)
|
|
|
|
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 200 {
|
2015-03-23 06:39:53 +03:00
|
|
|
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if w.Body.String() != "{\"parsed\":\"嘉\"}\n" {
|
|
|
|
t.Errorf("Response should be {\"parsed\":\"嘉\"}, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2015-01-30 05:14:09 +03:00
|
|
|
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
2014-08-11 14:25:52 +04:00
|
|
|
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingJSONNoContentType(t *testing.T) {
|
|
|
|
|
|
|
|
body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}"))
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-11 14:25:52 +04:00
|
|
|
r.POST("/binding/json", func(c *Context) {
|
|
|
|
var body struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
if c.Bind(&body) {
|
|
|
|
c.JSON(200, H{"parsed": body.Foo})
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", "/binding/json", body)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 400 {
|
2015-03-23 06:39:53 +03:00
|
|
|
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
|
|
|
t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.HeaderMap.Get("Content-Type") == "application/json" {
|
|
|
|
t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingJSONMalformed(t *testing.T) {
|
|
|
|
|
|
|
|
body := bytes.NewBuffer([]byte("\"foo\":\"bar\"\n"))
|
|
|
|
|
2014-08-18 21:48:48 +04:00
|
|
|
r := New()
|
2014-08-11 14:25:52 +04:00
|
|
|
r.POST("/binding/json", func(c *Context) {
|
|
|
|
var body struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
if c.Bind(&body) {
|
|
|
|
c.JSON(200, H{"parsed": body.Foo})
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", "/binding/json", body)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 400 {
|
2015-03-23 06:39:53 +03:00
|
|
|
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
|
|
|
if w.Body.String() == "{\"parsed\":\"bar\"}\n" {
|
|
|
|
t.Errorf("Response should not be {\"parsed\":\"bar\"}, was: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.HeaderMap.Get("Content-Type") == "application/json" {
|
|
|
|
t.Errorf("Content-Type should not be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
2015-02-10 02:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingForm(t *testing.T) {
|
|
|
|
|
|
|
|
body := bytes.NewBuffer([]byte("foo=bar&num=123&unum=1234567890"))
|
|
|
|
|
|
|
|
r := New()
|
|
|
|
r.POST("/binding/form", func(c *Context) {
|
|
|
|
var body struct {
|
|
|
|
Foo string `form:"foo"`
|
|
|
|
Num int `form:"num"`
|
|
|
|
Unum uint `form:"unum"`
|
|
|
|
}
|
|
|
|
if c.Bind(&body) {
|
|
|
|
c.JSON(200, H{"foo": body.Foo, "num": body.Num, "unum": body.Unum})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", "/binding/form", body)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "{\"foo\":\"bar\",\"num\":123,\"unum\":1234567890}\n"
|
|
|
|
if w.Body.String() != expected {
|
|
|
|
t.Errorf("Response should be %s, was %s", expected, w.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
|
|
|
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
2014-08-11 14:25:52 +04:00
|
|
|
}
|
|
|
|
}
|
2014-12-21 15:42:48 +03:00
|
|
|
|
|
|
|
func TestClientIP(t *testing.T) {
|
|
|
|
r := New()
|
|
|
|
|
|
|
|
var clientIP string = ""
|
|
|
|
r.GET("/", func(c *Context) {
|
|
|
|
clientIP = c.ClientIP()
|
|
|
|
})
|
|
|
|
|
|
|
|
body := bytes.NewBuffer([]byte(""))
|
|
|
|
req, _ := http.NewRequest("GET", "/", body)
|
|
|
|
req.RemoteAddr = "clientip:1234"
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if clientIP != "clientip:1234" {
|
|
|
|
t.Errorf("ClientIP should not be %s, but clientip:1234", clientIP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientIPWithXForwardedForWithProxy(t *testing.T) {
|
|
|
|
r := New()
|
|
|
|
r.Use(ForwardedFor())
|
|
|
|
|
|
|
|
var clientIP string = ""
|
|
|
|
r.GET("/", func(c *Context) {
|
|
|
|
clientIP = c.ClientIP()
|
|
|
|
})
|
|
|
|
|
|
|
|
body := bytes.NewBuffer([]byte(""))
|
|
|
|
req, _ := http.NewRequest("GET", "/", body)
|
|
|
|
req.RemoteAddr = "172.16.8.3:1234"
|
|
|
|
req.Header.Set("X-Real-Ip", "realip")
|
|
|
|
req.Header.Set("X-Forwarded-For", "1.2.3.4, 10.10.0.4, 192.168.0.43, 172.16.8.4")
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if clientIP != "1.2.3.4:0" {
|
|
|
|
t.Errorf("ClientIP should not be %s, but 1.2.3.4:0", clientIP)
|
|
|
|
}
|
|
|
|
}
|