mirror of https://github.com/gin-gonic/gin.git
Improves unit tests
This commit is contained in:
parent
67f8f6bb69
commit
ac0ad2fed8
5
auth.go
5
auth.go
|
@ -9,7 +9,6 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -61,12 +60,12 @@ func BasicAuth(accounts Accounts) HandlerFunc {
|
||||||
|
|
||||||
func processAccounts(accounts Accounts) authPairs {
|
func processAccounts(accounts Accounts) authPairs {
|
||||||
if len(accounts) == 0 {
|
if len(accounts) == 0 {
|
||||||
log.Panic("Empty list of authorized credentials")
|
panic("Empty list of authorized credentials")
|
||||||
}
|
}
|
||||||
pairs := make(authPairs, 0, len(accounts))
|
pairs := make(authPairs, 0, len(accounts))
|
||||||
for user, password := range accounts {
|
for user, password := range accounts {
|
||||||
if len(user) == 0 {
|
if len(user) == 0 {
|
||||||
log.Panic("User can not be empty")
|
panic("User can not be empty")
|
||||||
}
|
}
|
||||||
base := user + ":" + password
|
base := user + ":" + password
|
||||||
value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
|
value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
|
||||||
|
|
|
@ -6,7 +6,6 @@ package binding
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
@ -136,6 +135,6 @@ func setFloatField(val string, bitSize int, field reflect.Value) error {
|
||||||
// https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
|
// https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
|
||||||
func ensureNotPointer(obj interface{}) {
|
func ensureNotPointer(obj interface{}) {
|
||||||
if reflect.TypeOf(obj).Kind() == reflect.Ptr {
|
if reflect.TypeOf(obj).Kind() == reflect.Ptr {
|
||||||
log.Panic("Pointers are not accepted as binding models")
|
panic("Pointers are not accepted as binding models")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
context.go
19
context.go
|
@ -6,7 +6,7 @@ package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -81,6 +81,10 @@ func (c *Context) AbortWithStatus(code int) {
|
||||||
c.Abort()
|
c.Abort()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) IsAborted() bool {
|
||||||
|
return c.index == AbortIndex
|
||||||
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
/********* ERROR MANAGEMENT *********/
|
/********* ERROR MANAGEMENT *********/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
@ -96,7 +100,7 @@ func (c *Context) Fail(code int, err error) {
|
||||||
c.AbortWithStatus(code)
|
c.AbortWithStatus(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) ErrorTyped(err error, typ uint32, meta interface{}) {
|
func (c *Context) ErrorTyped(err error, typ int, meta interface{}) {
|
||||||
c.Errors = append(c.Errors, errorMsg{
|
c.Errors = append(c.Errors, errorMsg{
|
||||||
Err: err.Error(),
|
Err: err.Error(),
|
||||||
Type: typ,
|
Type: typ,
|
||||||
|
@ -146,9 +150,8 @@ func (c *Context) MustGet(key string) interface{} {
|
||||||
if value, exists := c.Get(key); exists {
|
if value, exists := c.Get(key); exists {
|
||||||
return value
|
return value
|
||||||
} else {
|
} else {
|
||||||
log.Panicf("Key %s does not exist", key)
|
panic("Key " + key + " does not exist")
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
|
@ -163,7 +166,7 @@ func (c *Context) ClientIP() string {
|
||||||
clientIP = c.Request.Header.Get("X-Forwarded-For")
|
clientIP = c.Request.Header.Get("X-Forwarded-For")
|
||||||
clientIP = strings.Split(clientIP, ",")[0]
|
clientIP = strings.Split(clientIP, ",")[0]
|
||||||
if len(clientIP) > 0 {
|
if len(clientIP) > 0 {
|
||||||
return clientIP
|
return strings.TrimSpace(clientIP)
|
||||||
}
|
}
|
||||||
return c.Request.RemoteAddr
|
return c.Request.RemoteAddr
|
||||||
}
|
}
|
||||||
|
@ -236,7 +239,7 @@ func (c *Context) Redirect(code int, location string) {
|
||||||
if code >= 300 && code <= 308 {
|
if code >= 300 && code <= 308 {
|
||||||
c.Render(code, render.Redirect, c.Request, location)
|
c.Render(code, render.Redirect, c.Request, location)
|
||||||
} else {
|
} else {
|
||||||
log.Panicf("Cannot redirect with status code %d", code)
|
panic(fmt.Sprintf("Cannot redirect with status code %d", code))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,7 +278,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
|
||||||
|
|
||||||
case binding.MIMEHTML:
|
case binding.MIMEHTML:
|
||||||
if len(config.HTMLPath) == 0 {
|
if len(config.HTMLPath) == 0 {
|
||||||
log.Panic("negotiate config is wrong. html path is needed")
|
panic("negotiate config is wrong. html path is needed")
|
||||||
}
|
}
|
||||||
data := chooseData(config.HTMLData, config.Data)
|
data := chooseData(config.HTMLData, config.Data)
|
||||||
c.HTML(code, config.HTMLPath, data)
|
c.HTML(code, config.HTMLPath, data)
|
||||||
|
@ -291,7 +294,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
|
||||||
|
|
||||||
func (c *Context) NegotiateFormat(offered ...string) string {
|
func (c *Context) NegotiateFormat(offered ...string) string {
|
||||||
if len(offered) == 0 {
|
if len(offered) == 0 {
|
||||||
log.Panic("you must provide at least one offer")
|
panic("you must provide at least one offer")
|
||||||
}
|
}
|
||||||
if c.Accepted == nil {
|
if c.Accepted == nil {
|
||||||
c.Accepted = parseAccept(c.Request.Header.Get("Accept"))
|
c.Accepted = parseAccept(c.Request.Header.Get("Accept"))
|
||||||
|
|
639
context_test.go
639
context_test.go
|
@ -11,454 +11,311 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestContextParamsGet tests that a parameter can be parsed from the URL.
|
func createTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) {
|
||||||
func TestContextParamsByName(t *testing.T) {
|
w = httptest.NewRecorder()
|
||||||
req, _ := http.NewRequest("GET", "/test/alexandernyquist", nil)
|
r = New()
|
||||||
w := httptest.NewRecorder()
|
c = r.allocateContext()
|
||||||
name := ""
|
c.reset()
|
||||||
|
c.writermem.reset(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
r := New()
|
func TestContextReset(t *testing.T) {
|
||||||
r.GET("/test/:name", func(c *Context) {
|
router := New()
|
||||||
name = c.Params.ByName("name")
|
c := router.allocateContext()
|
||||||
})
|
assert.Equal(t, c.Engine, router)
|
||||||
|
|
||||||
r.ServeHTTP(w, req)
|
c.index = 2
|
||||||
|
c.Writer = &responseWriter{ResponseWriter: httptest.NewRecorder()}
|
||||||
|
c.Params = httprouter.Params{httprouter.Param{}}
|
||||||
|
c.Error(errors.New("test"), nil)
|
||||||
|
c.Set("foo", "bar")
|
||||||
|
c.reset()
|
||||||
|
|
||||||
if name != "alexandernyquist" {
|
assert.False(t, c.IsAborted())
|
||||||
t.Errorf("Url parameter was not correctly parsed. Should be alexandernyquist, was %s.", name)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
c, _, _ := createTestContext()
|
||||||
w := httptest.NewRecorder()
|
c.Set("foo", "bar")
|
||||||
|
|
||||||
r := New()
|
value, err := c.Get("foo")
|
||||||
r.GET("/test", func(c *Context) {
|
assert.Equal(t, value, "bar")
|
||||||
// Key should be lazily created
|
assert.True(t, err)
|
||||||
if c.Keys != nil {
|
|
||||||
t.Error("Keys should be nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set
|
value, err = c.Get("foo2")
|
||||||
c.Set("foo", "bar")
|
assert.Nil(t, value)
|
||||||
|
assert.False(t, err)
|
||||||
|
|
||||||
v, ok := c.Get("foo")
|
assert.Equal(t, c.MustGet("foo"), "bar")
|
||||||
if !ok {
|
assert.Panics(t, func() { c.MustGet("no_exist") })
|
||||||
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
|
// 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 TestContextJSON(t *testing.T) {
|
func TestContextRenderJSON(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
c, w, _ := createTestContext()
|
||||||
w := httptest.NewRecorder()
|
c.JSON(201, H{"foo": "bar"})
|
||||||
|
|
||||||
r := New()
|
assert.Equal(t, w.Code, 201)
|
||||||
r.GET("/test", func(c *Context) {
|
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
|
||||||
c.JSON(200, H{"foo": "bar"})
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
|
||||||
})
|
|
||||||
|
|
||||||
r.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
if w.Body.String() != "{\"foo\":\"bar\"}\n" {
|
|
||||||
t.Errorf("Response should be {\"foo\":\"bar\"}, was: %s", 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"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestContextHTML 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 TestContextHTML(t *testing.T) {
|
func TestContextRenderHTML(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
c, w, router := createTestContext()
|
||||||
w := httptest.NewRecorder()
|
templ, _ := template.New("t").Parse(`Hello {{.name}}`)
|
||||||
|
router.SetHTMLTemplate(templ)
|
||||||
|
|
||||||
r := New()
|
c.HTML(201, "t", H{"name": "alexandernyquist"})
|
||||||
templ, _ := template.New("t").Parse(`Hello {{.Name}}`)
|
|
||||||
r.SetHTMLTemplate(templ)
|
|
||||||
|
|
||||||
type TestData struct{ Name string }
|
assert.Equal(t, w.Code, 201)
|
||||||
|
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
|
||||||
r.GET("/test", func(c *Context) {
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
|
|
||||||
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()
|
|
||||||
|
|
||||||
r := New()
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
|
|
||||||
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 TestContextXML(t *testing.T) {
|
func TestContextRenderXML(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
c, w, _ := createTestContext()
|
||||||
w := httptest.NewRecorder()
|
c.XML(201, H{"foo": "bar"})
|
||||||
|
|
||||||
r := New()
|
assert.Equal(t, w.Code, 201)
|
||||||
r.GET("/test", func(c *Context) {
|
assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")
|
||||||
c.XML(200, H{"foo": "bar"})
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/xml; charset=utf-8")
|
||||||
})
|
}
|
||||||
|
|
||||||
r.ServeHTTP(w, req)
|
// TestContextString tests that the response is returned
|
||||||
|
// with Content-Type set to text/plain
|
||||||
|
func TestContextRenderString(t *testing.T) {
|
||||||
|
c, w, _ := createTestContext()
|
||||||
|
c.String(201, "test %s %d", "string", 2)
|
||||||
|
|
||||||
if w.Body.String() != "<map><foo>bar</foo></map>" {
|
assert.Equal(t, w.Code, 201)
|
||||||
t.Errorf("Response should be <map><foo>bar</foo></map>, was: %s", w.Body.String())
|
assert.Equal(t, w.Body.String(), "test string 2")
|
||||||
}
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
|
||||||
|
}
|
||||||
|
|
||||||
if w.HeaderMap.Get("Content-Type") != "application/xml; charset=utf-8" {
|
// TestContextString tests that the response is returned
|
||||||
t.Errorf("Content-Type should be application/xml, was %s", w.HeaderMap.Get("Content-Type"))
|
// 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`
|
// TestContextData tests that the response can be written from `bytesting`
|
||||||
// with specified MIME type
|
// with specified MIME type
|
||||||
func TestContextData(t *testing.T) {
|
func TestContextRenderData(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", "/test/csv", nil)
|
c, w, _ := createTestContext()
|
||||||
w := httptest.NewRecorder()
|
c.Data(201, "text/csv", []byte(`foo,bar`))
|
||||||
|
|
||||||
r := New()
|
assert.Equal(t, w.Code, 201)
|
||||||
r.GET("/test/csv", func(c *Context) {
|
assert.Equal(t, w.Body.String(), "foo,bar")
|
||||||
c.Data(200, "text/csv", []byte(`foo,bar`))
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/csv")
|
||||||
})
|
}
|
||||||
|
|
||||||
r.ServeHTTP(w, req)
|
// 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") })
|
||||||
|
|
||||||
if w.Body.String() != "foo,bar" {
|
c.Redirect(302, "/path")
|
||||||
t.Errorf("Response should be foo&bar, was: %s", w.Body.String())
|
c.Writer.WriteHeaderNow()
|
||||||
|
assert.Equal(t, w.Code, 302)
|
||||||
|
assert.Equal(t, w.Header().Get("Location"), "/path")
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
assert.Equal(t, w.Code, 302)
|
||||||
|
assert.Equal(t, w.Header().Get("Location"), "http://google.com")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextNegotiationFormat(t *testing.T) {
|
||||||
|
c, _, _ := createTestContext()
|
||||||
|
c.Request, _ = http.NewRequest("POST", "", nil)
|
||||||
|
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON)
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEHTML, MIMEJSON), MIMEHTML)
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEXML)
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEHTML)
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
c.Accepted = nil
|
||||||
|
c.SetAccepted(MIMEJSON, MIMEXML)
|
||||||
|
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEJSON, MIMEXML), MIMEJSON)
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEXML, MIMEHTML), MIMEXML)
|
||||||
|
assert.Equal(t, c.NegotiateFormat(MIMEJSON), MIMEJSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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())
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
c.Error(errors.New("second error"), "some data 2")
|
||||||
|
assert.Equal(t, c.LastError().Error(), "second error")
|
||||||
|
assert.Len(t, c.Errors, 2)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
for _, err := range c.Errors.ByType(ErrorTypeExternal) {
|
||||||
|
assert.Equal(t, err.Type, ErrorTypeExternal)
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.HeaderMap.Get("Content-Type") != "text/csv" {
|
for _, err := range c.Errors.ByType(ErrorTypeInternal) {
|
||||||
t.Errorf("Content-Type should be text/csv, was %s", w.HeaderMap.Get("Content-Type"))
|
assert.Equal(t, err.Type, ErrorTypeInternal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextFile(t *testing.T) {
|
func TestContextFail(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", "/test/file", nil)
|
c, w, _ := createTestContext()
|
||||||
w := httptest.NewRecorder()
|
c.Fail(401, errors.New("bad input"))
|
||||||
|
c.Writer.WriteHeaderNow()
|
||||||
|
|
||||||
r := New()
|
assert.Equal(t, w.Code, 401)
|
||||||
r.GET("/test/file", func(c *Context) {
|
assert.Equal(t, c.LastError().Error(), "bad input")
|
||||||
c.File("./gin.go")
|
assert.Equal(t, c.index, AbortIndex)
|
||||||
})
|
assert.True(t, c.IsAborted())
|
||||||
|
|
||||||
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"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHandlerFunc - ensure that custom middleware works properly
|
func TestContextClientIP(t *testing.T) {
|
||||||
func TestHandlerFunc(t *testing.T) {
|
c, _, _ := createTestContext()
|
||||||
|
c.Request, _ = http.NewRequest("POST", "", nil)
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "/", nil)
|
c.Request.Header.Set("X-Real-IP", "10.10.10.10")
|
||||||
w := httptest.NewRecorder()
|
c.Request.Header.Set("X-Forwarded-For", "20.20.20.20 , 30.30.30.30")
|
||||||
|
c.Request.RemoteAddr = "40.40.40.40"
|
||||||
|
|
||||||
r := New()
|
assert.Equal(t, c.ClientIP(), "10.10.10.10")
|
||||||
var stepsPassed int = 0
|
c.Request.Header.Del("X-Real-IP")
|
||||||
|
assert.Equal(t, c.ClientIP(), "20.20.20.20")
|
||||||
r.Use(func(context *Context) {
|
c.Request.Header.Del("X-Forwarded-For")
|
||||||
stepsPassed += 1
|
assert.Equal(t, c.ClientIP(), "40.40.40.40")
|
||||||
context.Next()
|
|
||||||
stepsPassed += 1
|
|
||||||
})
|
|
||||||
|
|
||||||
r.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
if w.Code != 404 {
|
|
||||||
t.Errorf("Response code should be Not found, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
if stepsPassed != 2 {
|
|
||||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
|
func TestContextContentType(t *testing.T) {
|
||||||
func TestBadAbortHandlersChain(t *testing.T) {
|
c, _, _ := createTestContext()
|
||||||
// SETUP
|
c.Request, _ = http.NewRequest("POST", "", nil)
|
||||||
var stepsPassed int = 0
|
c.Request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||||
r := New()
|
|
||||||
r.Use(func(c *Context) {
|
|
||||||
stepsPassed += 1
|
|
||||||
c.Next()
|
|
||||||
stepsPassed += 1
|
|
||||||
// after check and abort
|
|
||||||
c.AbortWithStatus(409)
|
|
||||||
})
|
|
||||||
r.Use(func(c *Context) {
|
|
||||||
stepsPassed += 1
|
|
||||||
c.Next()
|
|
||||||
stepsPassed += 1
|
|
||||||
c.AbortWithStatus(403)
|
|
||||||
})
|
|
||||||
|
|
||||||
// RUN
|
assert.Equal(t, c.ContentType(), "application/json")
|
||||||
w := PerformRequest(r, "GET", "/")
|
|
||||||
|
|
||||||
// TEST
|
|
||||||
if w.Code != 409 {
|
|
||||||
t.Errorf("Response code should be Forbiden, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
if stepsPassed != 4 {
|
|
||||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
|
func TestContextAutoBind(t *testing.T) {
|
||||||
func TestAbortHandlersChain(t *testing.T) {
|
c, w, _ := createTestContext()
|
||||||
// SETUP
|
c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||||
var stepsPassed int = 0
|
c.Request.Header.Add("Content-Type", MIMEJSON)
|
||||||
r := New()
|
var obj struct {
|
||||||
r.Use(func(context *Context) {
|
Foo string `json:"foo"`
|
||||||
stepsPassed += 1
|
Bar string `json:"bar"`
|
||||||
context.AbortWithStatus(409)
|
|
||||||
})
|
|
||||||
r.Use(func(context *Context) {
|
|
||||||
stepsPassed += 1
|
|
||||||
context.Next()
|
|
||||||
stepsPassed += 1
|
|
||||||
})
|
|
||||||
|
|
||||||
// RUN
|
|
||||||
w := PerformRequest(r, "GET", "/")
|
|
||||||
|
|
||||||
// TEST
|
|
||||||
if w.Code != 409 {
|
|
||||||
t.Errorf("Response code should be Conflict, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
if stepsPassed != 1 {
|
|
||||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
|
func TestContextBadAutoBind(t *testing.T) {
|
||||||
// as well as Abort
|
c, w, _ := createTestContext()
|
||||||
func TestFailHandlersChain(t *testing.T) {
|
c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||||
// SETUP
|
c.Request.Header.Add("Content-Type", MIMEJSON)
|
||||||
var stepsPassed int = 0
|
var obj struct {
|
||||||
r := New()
|
Foo string `json:"foo"`
|
||||||
r.Use(func(context *Context) {
|
Bar string `json:"bar"`
|
||||||
stepsPassed += 1
|
|
||||||
context.Fail(500, errors.New("foo"))
|
|
||||||
})
|
|
||||||
r.Use(func(context *Context) {
|
|
||||||
stepsPassed += 1
|
|
||||||
context.Next()
|
|
||||||
stepsPassed += 1
|
|
||||||
})
|
|
||||||
|
|
||||||
// RUN
|
|
||||||
w := PerformRequest(r, "GET", "/")
|
|
||||||
|
|
||||||
// TEST
|
|
||||||
if w.Code != 500 {
|
|
||||||
t.Errorf("Response code should be Server error, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
if stepsPassed != 1 {
|
|
||||||
t.Errorf("Falied to switch context in handler function: %d", stepsPassed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert.False(t, c.IsAborted())
|
||||||
|
assert.False(t, c.Bind(&obj))
|
||||||
|
c.Writer.WriteHeaderNow()
|
||||||
|
|
||||||
|
assert.Empty(t, obj.Bar)
|
||||||
|
assert.Empty(t, obj.Foo)
|
||||||
|
assert.Equal(t, w.Code, 400)
|
||||||
|
assert.True(t, c.IsAborted())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBindingJSON(t *testing.T) {
|
func TestContextBindWith(t *testing.T) {
|
||||||
|
c, w, _ := createTestContext()
|
||||||
body := bytes.NewBuffer([]byte("{\"foo\":\"bar\"}"))
|
c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
|
||||||
|
c.Request.Header.Add("Content-Type", MIMEXML)
|
||||||
r := New()
|
var obj struct {
|
||||||
r.POST("/binding/json", func(c *Context) {
|
Foo string `json:"foo"`
|
||||||
var body struct {
|
Bar string `json:"bar"`
|
||||||
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 {
|
|
||||||
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.Body.String() != "{\"parsed\":\"bar\"}\n" {
|
|
||||||
t.Errorf("Response should be {\"parsed\":\"bar\"}, was: %s", 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"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBindingJSONEncoding(t *testing.T) {
|
|
||||||
|
|
||||||
body := bytes.NewBuffer([]byte("{\"foo\":\"嘉\"}"))
|
|
||||||
|
|
||||||
r := New()
|
|
||||||
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 {
|
|
||||||
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.Body.String() != "{\"parsed\":\"嘉\"}\n" {
|
|
||||||
t.Errorf("Response should be {\"parsed\":\"嘉\"}, was: %s", 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"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBindingJSONMalformed(t *testing.T) {
|
|
||||||
|
|
||||||
body := bytes.NewBuffer([]byte("\"foo\":\"bar\"\n"))
|
|
||||||
|
|
||||||
r := New()
|
|
||||||
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 {
|
|
||||||
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
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 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"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
9
debug.go
9
debug.go
|
@ -4,7 +4,12 @@
|
||||||
|
|
||||||
package gin
|
package gin
|
||||||
|
|
||||||
import "log"
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var debugLogger = log.New(os.Stdout, "[GIN-debug] ", 0)
|
||||||
|
|
||||||
func IsDebugging() bool {
|
func IsDebugging() bool {
|
||||||
return ginMode == debugCode
|
return ginMode == debugCode
|
||||||
|
@ -20,6 +25,6 @@ func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) {
|
||||||
|
|
||||||
func debugPrint(format string, values ...interface{}) {
|
func debugPrint(format string, values ...interface{}) {
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
log.Printf("[GIN-debug] "+format, values...)
|
debugLogger.Printf(format, values...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsDebugging(t *testing.T) {
|
||||||
|
SetMode(DebugMode)
|
||||||
|
assert.True(t, IsDebugging())
|
||||||
|
SetMode(ReleaseMode)
|
||||||
|
assert.False(t, IsDebugging())
|
||||||
|
SetMode(TestMode)
|
||||||
|
assert.False(t, IsDebugging())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// func TestDebugPrint(t *testing.T) {
|
||||||
|
// buffer := bytes.NewBufferString("")
|
||||||
|
// debugLogger.
|
||||||
|
// log.SetOutput(buffer)
|
||||||
|
|
||||||
|
// SetMode(ReleaseMode)
|
||||||
|
// debugPrint("This is a example")
|
||||||
|
// assert.Equal(t, buffer.Len(), 0)
|
||||||
|
|
||||||
|
// SetMode(DebugMode)
|
||||||
|
// debugPrint("This is %s", "a example")
|
||||||
|
// assert.Equal(t, buffer.String(), "[GIN-debug] This is a example")
|
||||||
|
|
||||||
|
// SetMode(TestMode)
|
||||||
|
// log.SetOutput(os.Stdout)
|
||||||
|
// }
|
|
@ -18,13 +18,13 @@ const (
|
||||||
// Used internally to collect errors that occurred during an http request.
|
// Used internally to collect errors that occurred during an http request.
|
||||||
type errorMsg struct {
|
type errorMsg struct {
|
||||||
Err string `json:"error"`
|
Err string `json:"error"`
|
||||||
Type uint32 `json:"-"`
|
Type int `json:"-"`
|
||||||
Meta interface{} `json:"meta"`
|
Meta interface{} `json:"meta"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type errorMsgs []errorMsg
|
type errorMsgs []errorMsg
|
||||||
|
|
||||||
func (a errorMsgs) ByType(typ uint32) errorMsgs {
|
func (a errorMsgs) ByType(typ int) errorMsgs {
|
||||||
if len(a) == 0 {
|
if len(a) == 0 {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,26 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/flosch/pongo2"
|
"github.com/flosch/pongo2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"github.com/gin-gonic/gin/render"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
router.HTMLRender = newPongoRender()
|
||||||
|
|
||||||
|
router.GET("/index", func(c *gin.Context) {
|
||||||
|
c.HTML(200, "index.html", gin.H{
|
||||||
|
"title": "Gin meets pongo2 !",
|
||||||
|
"name": c.Input.Get("name"),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
|
||||||
type pongoRender struct {
|
type pongoRender struct {
|
||||||
cache map[string]*pongo2.Template
|
cache map[string]*pongo2.Template
|
||||||
}
|
}
|
||||||
|
@ -14,13 +29,6 @@ func newPongoRender() *pongoRender {
|
||||||
return &pongoRender{map[string]*pongo2.Template{}}
|
return &pongoRender{map[string]*pongo2.Template{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
|
||||||
if code >= 0 {
|
|
||||||
w.Header().Set("Content-Type", contentType)
|
|
||||||
w.WriteHeader(code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||||
file := data[0].(string)
|
file := data[0].(string)
|
||||||
ctx := data[1].(pongo2.Context)
|
ctx := data[1].(pongo2.Context)
|
||||||
|
@ -36,23 +44,6 @@ func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{
|
||||||
p.cache[file] = tmpl
|
p.cache[file] = tmpl
|
||||||
t = tmpl
|
t = tmpl
|
||||||
}
|
}
|
||||||
writeHeader(w, code, "text/html")
|
render.WriteHeader(w, code, "text/html")
|
||||||
return t.ExecuteWriter(ctx, w)
|
return t.ExecuteWriter(ctx, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
r := gin.Default()
|
|
||||||
r.HTMLRender = newPongoRender()
|
|
||||||
|
|
||||||
r.GET("/index", func(c *gin.Context) {
|
|
||||||
name := c.Request.FormValue("name")
|
|
||||||
ctx := pongo2.Context{
|
|
||||||
"title": "Gin meets pongo2 !",
|
|
||||||
"name": name,
|
|
||||||
}
|
|
||||||
c.HTML(200, "index.html", ctx)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Listen and server on 0.0.0.0:8080
|
|
||||||
r.Run(":8080")
|
|
||||||
}
|
|
||||||
|
|
279
gin_test.go
279
gin_test.go
|
@ -5,202 +5,137 @@
|
||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
SetMode(TestMode)
|
SetMode(TestMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
func TestCreateEngine(t *testing.T) {
|
||||||
req, _ := http.NewRequest(method, path, nil)
|
router := New()
|
||||||
w := httptest.NewRecorder()
|
assert.Equal(t, "/", router.absolutePath)
|
||||||
r.ServeHTTP(w, req)
|
assert.Equal(t, router.engine, router)
|
||||||
return w
|
assert.Empty(t, router.Handlers)
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// assert.Equal(t, router.router.NotFound, router.handle404)
|
||||||
|
// assert.Equal(t, router.router.MethodNotAllowed, router.handle405)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
func TestCreateDefaultRouter(t *testing.T) {
|
||||||
func testRouteOK(method string, t *testing.T) {
|
router := Default()
|
||||||
// SETUP
|
assert.Len(t, router.Handlers, 2)
|
||||||
passed := false
|
|
||||||
r := New()
|
|
||||||
r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
|
|
||||||
passed = true
|
|
||||||
}})
|
|
||||||
|
|
||||||
// RUN
|
|
||||||
w := PerformRequest(r, method, "/test")
|
|
||||||
|
|
||||||
// TEST
|
|
||||||
if passed == false {
|
|
||||||
t.Errorf(method + " route handler was not invoked.")
|
|
||||||
}
|
|
||||||
if w.Code != http.StatusOK {
|
|
||||||
t.Errorf("Status code should be %v, was %d", http.StatusOK, w.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func TestRouterGroupRouteOK(t *testing.T) {
|
|
||||||
testRouteOK("POST", t)
|
|
||||||
testRouteOK("DELETE", t)
|
|
||||||
testRouteOK("PATCH", t)
|
|
||||||
testRouteOK("PUT", t)
|
|
||||||
testRouteOK("OPTIONS", t)
|
|
||||||
testRouteOK("HEAD", t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
func TestNoRouteWithoutGlobalHandlers(t *testing.T) {
|
||||||
func testRouteNotOK(method string, t *testing.T) {
|
middleware0 := func(c *Context) {}
|
||||||
// SETUP
|
middleware1 := func(c *Context) {}
|
||||||
passed := false
|
|
||||||
r := New()
|
|
||||||
r.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
|
|
||||||
passed = true
|
|
||||||
}})
|
|
||||||
|
|
||||||
// RUN
|
router := New()
|
||||||
w := PerformRequest(r, method, "/test")
|
|
||||||
|
|
||||||
// TEST
|
router.NoRoute(middleware0)
|
||||||
if passed == true {
|
assert.Nil(t, router.Handlers)
|
||||||
t.Errorf(method + " route handler was invoked, when it should not")
|
assert.Len(t, router.noRoute, 1)
|
||||||
}
|
assert.Len(t, router.allNoRoute, 1)
|
||||||
if w.Code != http.StatusNotFound {
|
assert.Equal(t, router.noRoute[0], middleware0)
|
||||||
// If this fails, it's because httprouter needs to be updated to at least f78f58a0db
|
assert.Equal(t, router.allNoRoute[0], middleware0)
|
||||||
t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusNotFound, w.Code, w.HeaderMap.Get("Location"))
|
|
||||||
}
|
router.NoRoute(middleware1, middleware0)
|
||||||
|
assert.Len(t, router.noRoute, 2)
|
||||||
|
assert.Len(t, router.allNoRoute, 2)
|
||||||
|
assert.Equal(t, router.noRoute[0], middleware1)
|
||||||
|
assert.Equal(t, router.allNoRoute[0], middleware1)
|
||||||
|
assert.Equal(t, router.noRoute[1], middleware0)
|
||||||
|
assert.Equal(t, router.allNoRoute[1], middleware0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
func TestNoRouteWithGlobalHandlers(t *testing.T) {
|
||||||
func TestRouteNotOK(t *testing.T) {
|
middleware0 := func(c *Context) {}
|
||||||
testRouteNotOK("POST", t)
|
middleware1 := func(c *Context) {}
|
||||||
testRouteNotOK("DELETE", t)
|
middleware2 := func(c *Context) {}
|
||||||
testRouteNotOK("PATCH", t)
|
|
||||||
testRouteNotOK("PUT", t)
|
router := New()
|
||||||
testRouteNotOK("OPTIONS", t)
|
router.Use(middleware2)
|
||||||
testRouteNotOK("HEAD", t)
|
|
||||||
|
router.NoRoute(middleware0)
|
||||||
|
assert.Len(t, router.allNoRoute, 2)
|
||||||
|
assert.Len(t, router.Handlers, 1)
|
||||||
|
assert.Len(t, router.noRoute, 1)
|
||||||
|
|
||||||
|
assert.Equal(t, router.Handlers[0], middleware2)
|
||||||
|
assert.Equal(t, router.noRoute[0], middleware0)
|
||||||
|
assert.Equal(t, router.allNoRoute[0], middleware2)
|
||||||
|
assert.Equal(t, router.allNoRoute[1], middleware0)
|
||||||
|
|
||||||
|
router.Use(middleware1)
|
||||||
|
assert.Len(t, router.allNoRoute, 3)
|
||||||
|
assert.Len(t, router.Handlers, 2)
|
||||||
|
assert.Len(t, router.noRoute, 1)
|
||||||
|
|
||||||
|
assert.Equal(t, router.Handlers[0], middleware2)
|
||||||
|
assert.Equal(t, router.Handlers[1], middleware1)
|
||||||
|
assert.Equal(t, router.noRoute[0], middleware0)
|
||||||
|
assert.Equal(t, router.allNoRoute[0], middleware2)
|
||||||
|
assert.Equal(t, router.allNoRoute[1], middleware1)
|
||||||
|
assert.Equal(t, router.allNoRoute[2], middleware0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
func TestNoMethodWithoutGlobalHandlers(t *testing.T) {
|
||||||
func testRouteNotOK2(method string, t *testing.T) {
|
middleware0 := func(c *Context) {}
|
||||||
// SETUP
|
middleware1 := func(c *Context) {}
|
||||||
passed := false
|
|
||||||
r := New()
|
|
||||||
var methodRoute string
|
|
||||||
if method == "POST" {
|
|
||||||
methodRoute = "GET"
|
|
||||||
} else {
|
|
||||||
methodRoute = "POST"
|
|
||||||
}
|
|
||||||
r.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
|
|
||||||
passed = true
|
|
||||||
}})
|
|
||||||
|
|
||||||
// RUN
|
router := New()
|
||||||
w := PerformRequest(r, method, "/test")
|
|
||||||
|
|
||||||
// TEST
|
router.NoMethod(middleware0)
|
||||||
if passed == true {
|
assert.Empty(t, router.Handlers)
|
||||||
t.Errorf(method + " route handler was invoked, when it should not")
|
assert.Len(t, router.noMethod, 1)
|
||||||
}
|
assert.Len(t, router.allNoMethod, 1)
|
||||||
if w.Code != http.StatusMethodNotAllowed {
|
assert.Equal(t, router.noMethod[0], middleware0)
|
||||||
t.Errorf("Status code should be %v, was %d. Location: %s", http.StatusMethodNotAllowed, w.Code, w.HeaderMap.Get("Location"))
|
assert.Equal(t, router.allNoMethod[0], middleware0)
|
||||||
}
|
|
||||||
|
router.NoMethod(middleware1, middleware0)
|
||||||
|
assert.Len(t, router.noMethod, 2)
|
||||||
|
assert.Len(t, router.allNoMethod, 2)
|
||||||
|
assert.Equal(t, router.noMethod[0], middleware1)
|
||||||
|
assert.Equal(t, router.allNoMethod[0], middleware1)
|
||||||
|
assert.Equal(t, router.noMethod[1], middleware0)
|
||||||
|
assert.Equal(t, router.allNoMethod[1], middleware0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSingleRouteOK tests that POST route is correctly invoked.
|
func TestRebuild404Handlers(t *testing.T) {
|
||||||
func TestRouteNotOK2(t *testing.T) {
|
|
||||||
testRouteNotOK2("POST", t)
|
|
||||||
testRouteNotOK2("DELETE", t)
|
|
||||||
testRouteNotOK2("PATCH", t)
|
|
||||||
testRouteNotOK2("PUT", t)
|
|
||||||
testRouteNotOK2("OPTIONS", t)
|
|
||||||
testRouteNotOK2("HEAD", t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHandleStaticFile - ensure the static file handles properly
|
func TestNoMethodWithGlobalHandlers(t *testing.T) {
|
||||||
func TestHandleStaticFile(t *testing.T) {
|
middleware0 := func(c *Context) {}
|
||||||
// SETUP file
|
middleware1 := func(c *Context) {}
|
||||||
testRoot, _ := os.Getwd()
|
middleware2 := func(c *Context) {}
|
||||||
f, err := ioutil.TempFile(testRoot, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer os.Remove(f.Name())
|
|
||||||
filePath := path.Join("/", path.Base(f.Name()))
|
|
||||||
f.WriteString("Gin Web Framework")
|
|
||||||
f.Close()
|
|
||||||
|
|
||||||
// SETUP gin
|
router := New()
|
||||||
r := New()
|
router.Use(middleware2)
|
||||||
r.Static("./", testRoot)
|
|
||||||
|
|
||||||
// RUN
|
router.NoMethod(middleware0)
|
||||||
w := PerformRequest(r, "GET", filePath)
|
assert.Len(t, router.allNoMethod, 2)
|
||||||
|
assert.Len(t, router.Handlers, 1)
|
||||||
|
assert.Len(t, router.noMethod, 1)
|
||||||
|
|
||||||
// TEST
|
assert.Equal(t, router.Handlers[0], middleware2)
|
||||||
if w.Code != 200 {
|
assert.Equal(t, router.noMethod[0], middleware0)
|
||||||
t.Errorf("Response code should be 200, was: %d", w.Code)
|
assert.Equal(t, router.allNoMethod[0], middleware2)
|
||||||
}
|
assert.Equal(t, router.allNoMethod[1], middleware0)
|
||||||
if w.Body.String() != "Gin Web Framework" {
|
|
||||||
t.Errorf("Response should be test, was: %s", w.Body.String())
|
router.Use(middleware1)
|
||||||
}
|
assert.Len(t, router.allNoMethod, 3)
|
||||||
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
|
assert.Len(t, router.Handlers, 2)
|
||||||
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
assert.Len(t, router.noMethod, 1)
|
||||||
}
|
|
||||||
}
|
assert.Equal(t, router.Handlers[0], middleware2)
|
||||||
|
assert.Equal(t, router.Handlers[1], middleware1)
|
||||||
// TestHandleStaticDir - ensure the root/sub dir handles properly
|
assert.Equal(t, router.noMethod[0], middleware0)
|
||||||
func TestHandleStaticDir(t *testing.T) {
|
assert.Equal(t, router.allNoMethod[0], middleware2)
|
||||||
// SETUP
|
assert.Equal(t, router.allNoMethod[1], middleware1)
|
||||||
r := New()
|
assert.Equal(t, router.allNoMethod[2], middleware0)
|
||||||
r.Static("/", "./")
|
|
||||||
|
|
||||||
// RUN
|
|
||||||
w := PerformRequest(r, "GET", "/")
|
|
||||||
|
|
||||||
// TEST
|
|
||||||
bodyAsString := w.Body.String()
|
|
||||||
if w.Code != 200 {
|
|
||||||
t.Errorf("Response code should be 200, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
if len(bodyAsString) == 0 {
|
|
||||||
t.Errorf("Got empty body instead of file tree")
|
|
||||||
}
|
|
||||||
if !strings.Contains(bodyAsString, "gin.go") {
|
|
||||||
t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
|
|
||||||
}
|
|
||||||
if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
|
|
||||||
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestHandleHeadToDir - ensure the root/sub dir handles properly
|
|
||||||
func TestHandleHeadToDir(t *testing.T) {
|
|
||||||
// SETUP
|
|
||||||
r := New()
|
|
||||||
r.Static("/", "./")
|
|
||||||
|
|
||||||
// RUN
|
|
||||||
w := PerformRequest(r, "HEAD", "/")
|
|
||||||
|
|
||||||
// TEST
|
|
||||||
bodyAsString := w.Body.String()
|
|
||||||
if w.Code != 200 {
|
|
||||||
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
if len(bodyAsString) == 0 {
|
|
||||||
t.Errorf("Got empty body instead of file tree")
|
|
||||||
}
|
|
||||||
if !strings.Contains(bodyAsString, "gin.go") {
|
|
||||||
t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
|
|
||||||
}
|
|
||||||
if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
|
|
||||||
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,14 +25,13 @@ func ErrorLogger() HandlerFunc {
|
||||||
return ErrorLoggerT(ErrorTypeAll)
|
return ErrorLoggerT(ErrorTypeAll)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ErrorLoggerT(typ uint32) HandlerFunc {
|
func ErrorLoggerT(typ int) HandlerFunc {
|
||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
if !c.Writer.Written() {
|
if !c.Writer.Written() {
|
||||||
errs := c.Errors.ByType(typ)
|
if errs := c.Errors.ByType(typ); len(errs) > 0 {
|
||||||
if len(errs) > 0 {
|
c.JSON(-1, errs)
|
||||||
c.JSON(-1, c.Errors)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
3
mode.go
3
mode.go
|
@ -5,7 +5,6 @@
|
||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/mattn/go-colorable"
|
"github.com/mattn/go-colorable"
|
||||||
|
@ -46,7 +45,7 @@ func SetMode(value string) {
|
||||||
case TestMode:
|
case TestMode:
|
||||||
ginMode = testCode
|
ginMode = testCode
|
||||||
default:
|
default:
|
||||||
log.Panic("gin mode unknown: " + value)
|
panic("gin mode unknown: " + value)
|
||||||
}
|
}
|
||||||
modeName = value
|
modeName = value
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,11 @@ func TestPanicInHandler(t *testing.T) {
|
||||||
r := New()
|
r := New()
|
||||||
r.Use(Recovery())
|
r.Use(Recovery())
|
||||||
r.GET("/recovery", func(_ *Context) {
|
r.GET("/recovery", func(_ *Context) {
|
||||||
log.Panic("Oupps, Houston, we have a problem")
|
panic("Oupps, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
|
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(r, "GET", "/recovery")
|
w := performRequest(r, "GET", "/recovery")
|
||||||
|
|
||||||
// restore logging
|
// restore logging
|
||||||
log.SetOutput(os.Stderr)
|
log.SetOutput(os.Stderr)
|
||||||
|
@ -40,11 +40,11 @@ func TestPanicWithAbort(t *testing.T) {
|
||||||
r.Use(Recovery())
|
r.Use(Recovery())
|
||||||
r.GET("/recovery", func(c *Context) {
|
r.GET("/recovery", func(c *Context) {
|
||||||
c.AbortWithStatus(400)
|
c.AbortWithStatus(400)
|
||||||
log.Panic("Oupps, Houston, we have a problem")
|
panic("Oupps, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
|
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(r, "GET", "/recovery")
|
w := performRequest(r, "GET", "/recovery")
|
||||||
|
|
||||||
// restore logging
|
// restore logging
|
||||||
log.SetOutput(os.Stderr)
|
log.SetOutput(os.Stderr)
|
||||||
|
|
|
@ -0,0 +1,332 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
||||||
|
req, _ := http.NewRequest(method, path, nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
func testRouteOK(method string, t *testing.T) {
|
||||||
|
// SETUP
|
||||||
|
passed := false
|
||||||
|
r := New()
|
||||||
|
r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
|
||||||
|
passed = true
|
||||||
|
}})
|
||||||
|
// RUN
|
||||||
|
w := performRequest(r, method, "/test")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.True(t, passed)
|
||||||
|
assert.Equal(t, w.Code, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
||||||
|
func testRouteNotOK(method string, t *testing.T) {
|
||||||
|
// SETUP
|
||||||
|
passed := false
|
||||||
|
router := New()
|
||||||
|
router.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
|
||||||
|
passed = true
|
||||||
|
}})
|
||||||
|
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, method, "/test")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.False(t, passed)
|
||||||
|
assert.Equal(t, w.Code, http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
||||||
|
func testRouteNotOK2(method string, t *testing.T) {
|
||||||
|
// SETUP
|
||||||
|
passed := false
|
||||||
|
router := New()
|
||||||
|
var methodRoute string
|
||||||
|
if method == "POST" {
|
||||||
|
methodRoute = "GET"
|
||||||
|
} else {
|
||||||
|
methodRoute = "POST"
|
||||||
|
}
|
||||||
|
router.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
|
||||||
|
passed = true
|
||||||
|
}})
|
||||||
|
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, method, "/test")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.False(t, passed)
|
||||||
|
assert.Equal(t, w.Code, http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouterGroupRouteOK(t *testing.T) {
|
||||||
|
testRouteOK("POST", t)
|
||||||
|
testRouteOK("DELETE", t)
|
||||||
|
testRouteOK("PATCH", t)
|
||||||
|
testRouteOK("PUT", t)
|
||||||
|
testRouteOK("OPTIONS", t)
|
||||||
|
testRouteOK("HEAD", t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
||||||
|
func TestRouteNotOK(t *testing.T) {
|
||||||
|
testRouteNotOK("POST", t)
|
||||||
|
testRouteNotOK("DELETE", t)
|
||||||
|
testRouteNotOK("PATCH", t)
|
||||||
|
testRouteNotOK("PUT", t)
|
||||||
|
testRouteNotOK("OPTIONS", t)
|
||||||
|
testRouteNotOK("HEAD", t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
||||||
|
func TestRouteNotOK2(t *testing.T) {
|
||||||
|
testRouteNotOK2("POST", t)
|
||||||
|
testRouteNotOK2("DELETE", t)
|
||||||
|
testRouteNotOK2("PATCH", t)
|
||||||
|
testRouteNotOK2("PUT", t)
|
||||||
|
testRouteNotOK2("OPTIONS", t)
|
||||||
|
testRouteNotOK2("HEAD", t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandleStaticFile - ensure the static file handles properly
|
||||||
|
func TestHandleStaticFile(t *testing.T) {
|
||||||
|
// SETUP file
|
||||||
|
testRoot, _ := os.Getwd()
|
||||||
|
f, err := ioutil.TempFile(testRoot, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
filePath := path.Join("/", path.Base(f.Name()))
|
||||||
|
f.WriteString("Gin Web Framework")
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
// SETUP gin
|
||||||
|
r := New()
|
||||||
|
r.Static("./", testRoot)
|
||||||
|
|
||||||
|
// RUN
|
||||||
|
w := performRequest(r, "GET", filePath)
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
if w.Code != 200 {
|
||||||
|
t.Errorf("Response code should be 200, was: %d", w.Code)
|
||||||
|
}
|
||||||
|
if w.Body.String() != "Gin Web Framework" {
|
||||||
|
t.Errorf("Response should be test, was: %s", w.Body.String())
|
||||||
|
}
|
||||||
|
if w.HeaderMap.Get("Content-Type") != "text/plain; charset=utf-8" {
|
||||||
|
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandleStaticDir - ensure the root/sub dir handles properly
|
||||||
|
func TestHandleStaticDir(t *testing.T) {
|
||||||
|
// SETUP
|
||||||
|
r := New()
|
||||||
|
r.Static("/", "./")
|
||||||
|
|
||||||
|
// RUN
|
||||||
|
w := performRequest(r, "GET", "/")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
bodyAsString := w.Body.String()
|
||||||
|
if w.Code != 200 {
|
||||||
|
t.Errorf("Response code should be 200, was: %d", w.Code)
|
||||||
|
}
|
||||||
|
if len(bodyAsString) == 0 {
|
||||||
|
t.Errorf("Got empty body instead of file tree")
|
||||||
|
}
|
||||||
|
if !strings.Contains(bodyAsString, "gin.go") {
|
||||||
|
t.Errorf("Can't find:`gin.go` in file tree: %s", bodyAsString)
|
||||||
|
}
|
||||||
|
if w.HeaderMap.Get("Content-Type") != "text/html; charset=utf-8" {
|
||||||
|
t.Errorf("Content-Type should be text/plain, was %s", w.HeaderMap.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHandleHeadToDir - ensure the root/sub dir handles properly
|
||||||
|
func TestHandleHeadToDir(t *testing.T) {
|
||||||
|
// SETUP
|
||||||
|
router := New()
|
||||||
|
router.Static("/", "./")
|
||||||
|
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, "HEAD", "/")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
bodyAsString := w.Body.String()
|
||||||
|
assert.Equal(t, w.Code, 200)
|
||||||
|
assert.NotEmpty(t, bodyAsString)
|
||||||
|
assert.Contains(t, bodyAsString, "gin.go")
|
||||||
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextGeneralCase(t *testing.T) {
|
||||||
|
signature := ""
|
||||||
|
router := New()
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
signature += "A"
|
||||||
|
c.Next()
|
||||||
|
signature += "B"
|
||||||
|
})
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
signature += "C"
|
||||||
|
})
|
||||||
|
router.GET("/", func(c *Context) {
|
||||||
|
signature += "D"
|
||||||
|
})
|
||||||
|
router.NoRoute(func(c *Context) {
|
||||||
|
signature += "X"
|
||||||
|
})
|
||||||
|
router.NoMethod(func(c *Context) {
|
||||||
|
signature += "X"
|
||||||
|
})
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.Equal(t, w.Code, 200)
|
||||||
|
assert.Equal(t, signature, "ACDB")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
|
||||||
|
func TestContextNextOrder(t *testing.T) {
|
||||||
|
signature := ""
|
||||||
|
router := New()
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
signature += "A"
|
||||||
|
c.Next()
|
||||||
|
signature += "B"
|
||||||
|
})
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
signature += "C"
|
||||||
|
c.Next()
|
||||||
|
signature += "D"
|
||||||
|
})
|
||||||
|
router.NoRoute(func(c *Context) {
|
||||||
|
signature += "E"
|
||||||
|
c.Next()
|
||||||
|
signature += "F"
|
||||||
|
}, func(c *Context) {
|
||||||
|
signature += "G"
|
||||||
|
c.Next()
|
||||||
|
signature += "H"
|
||||||
|
})
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.Equal(t, w.Code, 404)
|
||||||
|
assert.Equal(t, signature, "ACEGHFDB")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
|
||||||
|
func TestAbortHandlersChain(t *testing.T) {
|
||||||
|
signature := ""
|
||||||
|
router := New()
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
signature += "A"
|
||||||
|
})
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
signature += "C"
|
||||||
|
c.AbortWithStatus(409)
|
||||||
|
c.Next()
|
||||||
|
signature += "D"
|
||||||
|
})
|
||||||
|
router.GET("/", func(c *Context) {
|
||||||
|
signature += "D"
|
||||||
|
c.Next()
|
||||||
|
signature += "E"
|
||||||
|
})
|
||||||
|
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.Equal(t, signature, "ACD")
|
||||||
|
assert.Equal(t, w.Code, 409)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbortHandlersChainAndNext(t *testing.T) {
|
||||||
|
signature := ""
|
||||||
|
router := New()
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
signature += "A"
|
||||||
|
c.AbortWithStatus(410)
|
||||||
|
c.Next()
|
||||||
|
signature += "B"
|
||||||
|
|
||||||
|
})
|
||||||
|
router.GET("/", func(c *Context) {
|
||||||
|
signature += "C"
|
||||||
|
c.Next()
|
||||||
|
})
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.Equal(t, signature, "AB")
|
||||||
|
assert.Equal(t, w.Code, 410)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestContextParamsGet tests that a parameter can be parsed from the URL.
|
||||||
|
func TestContextParamsByName(t *testing.T) {
|
||||||
|
name := ""
|
||||||
|
lastName := ""
|
||||||
|
router := New()
|
||||||
|
router.GET("/test/:name/:last_name", func(c *Context) {
|
||||||
|
name = c.Params.ByName("name")
|
||||||
|
lastName = c.Params.ByName("last_name")
|
||||||
|
})
|
||||||
|
// RUN
|
||||||
|
w := performRequest(router, "GET", "/test/john/smith")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.Equal(t, w.Code, 200)
|
||||||
|
assert.Equal(t, name, "john")
|
||||||
|
assert.Equal(t, lastName, "smith")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
|
||||||
|
// as well as Abort
|
||||||
|
func TestFailHandlersChain(t *testing.T) {
|
||||||
|
// SETUP
|
||||||
|
var stepsPassed int = 0
|
||||||
|
r := New()
|
||||||
|
r.Use(func(context *Context) {
|
||||||
|
stepsPassed += 1
|
||||||
|
context.Fail(500, errors.New("foo"))
|
||||||
|
})
|
||||||
|
r.Use(func(context *Context) {
|
||||||
|
stepsPassed += 1
|
||||||
|
context.Next()
|
||||||
|
stepsPassed += 1
|
||||||
|
})
|
||||||
|
// RUN
|
||||||
|
w := performRequest(r, "GET", "/")
|
||||||
|
|
||||||
|
// TEST
|
||||||
|
assert.Equal(t, w.Code, 500, "Response code should be Server error, was: %d", w.Code)
|
||||||
|
assert.Equal(t, stepsPassed, 1, "Falied to switch context in handler function: %d", stepsPassed)
|
||||||
|
}
|
19
utils.go
19
utils.go
|
@ -6,7 +6,6 @@ package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"log"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -50,29 +49,33 @@ func filterFlags(content string) string {
|
||||||
func chooseData(custom, wildcard interface{}) interface{} {
|
func chooseData(custom, wildcard interface{}) interface{} {
|
||||||
if custom == nil {
|
if custom == nil {
|
||||||
if wildcard == nil {
|
if wildcard == nil {
|
||||||
log.Panic("negotiation config is invalid")
|
panic("negotiation config is invalid")
|
||||||
}
|
}
|
||||||
return wildcard
|
return wildcard
|
||||||
}
|
}
|
||||||
return custom
|
return custom
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAccept(acceptHeader string) (parts []string) {
|
func parseAccept(acceptHeader string) []string {
|
||||||
parts = strings.Split(acceptHeader, ",")
|
parts := strings.Split(acceptHeader, ",")
|
||||||
for i, part := range parts {
|
out := make([]string, 0, len(parts))
|
||||||
|
for _, part := range parts {
|
||||||
index := strings.IndexByte(part, ';')
|
index := strings.IndexByte(part, ';')
|
||||||
if index >= 0 {
|
if index >= 0 {
|
||||||
part = part[0:index]
|
part = part[0:index]
|
||||||
}
|
}
|
||||||
parts[i] = strings.TrimSpace(part)
|
part = strings.TrimSpace(part)
|
||||||
|
if len(part) > 0 {
|
||||||
|
out = append(out, part)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func lastChar(str string) uint8 {
|
func lastChar(str string) uint8 {
|
||||||
size := len(str)
|
size := len(str)
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
log.Panic("The length of the string can't be 0")
|
panic("The length of the string can't be 0")
|
||||||
}
|
}
|
||||||
return str[size-1]
|
return str[size-1]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue