diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index afc04ec..36109e6 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -2,6 +2,10 @@ "ImportPath": "github.com/gin-gonic/gin", "GoVersion": "go1.4.2", "Deps": [ + { + "ImportPath": "github.com/julienschmidt/httprouter", + "Rev": "999ba04938b528fb4fb859231ee929958b8db4a6" + }, { "ImportPath": "github.com/mattn/go-colorable", "Rev": "043ae16291351db8465272edf465c9f388161627" @@ -9,6 +13,11 @@ { "ImportPath": "github.com/stretchr/testify/assert", "Rev": "de7fcff264cd05cc0c90c509ea789a436a0dd206" + }, + { + "ImportPath": "gopkg.in/joeybloggs/go-validate-yourself.v4", + "Comment": "v4.0", + "Rev": "a3cb430fa1e43b15e72d7bec5b20d0bdff4c2bb8" } ] } diff --git a/auth.go b/auth.go index da71401..7a65343 100644 --- a/auth.go +++ b/auth.go @@ -9,7 +9,6 @@ import ( "encoding/base64" "errors" "fmt" - "log" "sort" ) @@ -62,12 +61,12 @@ func BasicAuth(accounts Accounts) HandlerFunc { func processAccounts(accounts Accounts) authPairs { if len(accounts) == 0 { - log.Panic("Empty list of authorized credentials") + panic("Empty list of authorized credentials") } pairs := make(authPairs, 0, len(accounts)) for user, password := range accounts { if len(user) == 0 { - log.Panic("User can not be empty") + panic("User can not be empty") } base := user + ":" + password value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base)) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 3284b10..d9aa06c 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -6,7 +6,6 @@ package binding import ( "errors" - "log" "reflect" "strconv" ) @@ -135,6 +134,6 @@ func setFloatField(val string, bitSize int, field reflect.Value) error { // https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659 func ensureNotPointer(obj interface{}) { if reflect.TypeOf(obj).Kind() == reflect.Ptr { - log.Panic("Pointers are not accepted as binding models") + panic("Pointers are not accepted as binding models") } } diff --git a/context.go b/context.go index 215c6f7..088fd69 100644 --- a/context.go +++ b/context.go @@ -6,7 +6,7 @@ package gin import ( "errors" - "log" + "fmt" "math" "net/http" "strings" @@ -32,7 +32,7 @@ type Context struct { Engine *Engine Keys map[string]interface{} Errors errorMsgs - accepted []string + Accepted []string } /************************************/ @@ -46,7 +46,7 @@ func (c *Context) reset() { c.index = -1 c.Keys = nil c.Errors = c.Errors[0:0] - c.accepted = nil + c.Accepted = nil } func (c *Context) Copy() *Context { @@ -83,6 +83,10 @@ func (c *Context) AbortWithStatus(code int) { c.Abort() } +func (c *Context) IsAborted() bool { + return c.index == AbortIndex +} + /************************************/ /********* ERROR MANAGEMENT *********/ /************************************/ @@ -98,7 +102,7 @@ func (c *Context) Fail(code int, err error) { 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{ Err: err.Error(), Type: typ, @@ -147,9 +151,8 @@ func (c *Context) MustGet(key string) interface{} { if value, exists := c.Get(key); exists { return value } else { - log.Panicf("Key %s does not exist", key) + panic("Key " + key + " does not exist") } - return nil } /************************************/ @@ -164,7 +167,7 @@ func (c *Context) ClientIP() string { clientIP = c.Request.Header.Get("X-Forwarded-For") clientIP = strings.Split(clientIP, ",")[0] if len(clientIP) > 0 { - return clientIP + return strings.TrimSpace(clientIP) } return c.Request.RemoteAddr } @@ -237,7 +240,7 @@ func (c *Context) Redirect(code int, location string) { if code >= 300 && code <= 308 { c.Render(code, render.Redirect, c.Request, location) } else { - log.Panicf("Cannot redirect with status code %d", code) + panic(fmt.Sprintf("Cannot redirect with status code %d", code)) } } @@ -276,7 +279,7 @@ func (c *Context) Negotiate(code int, config Negotiate) { case binding.MIMEHTML: 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) c.HTML(code, config.HTMLPath, data) @@ -292,26 +295,24 @@ func (c *Context) Negotiate(code int, config Negotiate) { func (c *Context) NegotiateFormat(offered ...string) string { 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 { - c.accepted = parseAccept(c.Request.Header.Get("Accept")) + if c.Accepted == nil { + c.Accepted = parseAccept(c.Request.Header.Get("Accept")) } - if len(c.accepted) == 0 { + if len(c.Accepted) == 0 { return offered[0] - - } else { - for _, accepted := range c.accepted { - for _, offert := range offered { - if accepted == offert { - return offert - } + } + for _, accepted := range c.Accepted { + for _, offert := range offered { + if accepted == offert { + return offert } } - return "" } + return "" } func (c *Context) SetAccepted(formats ...string) { - c.accepted = formats + c.Accepted = formats } diff --git a/context_test.go b/context_test.go new file mode 100644 index 0000000..36e4a59 --- /dev/null +++ b/context_test.go @@ -0,0 +1,321 @@ +// 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 ( + "bytes" + "errors" + "html/template" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin/binding" + "github.com/julienschmidt/httprouter" + "github.com/stretchr/testify/assert" +) + +func createTestContext() (c *Context, w *httptest.ResponseRecorder, r *Engine) { + w = httptest.NewRecorder() + r = New() + c = r.allocateContext() + c.reset() + c.writermem.reset(w) + return +} + +func TestContextReset(t *testing.T) { + router := New() + c := router.allocateContext() + assert.Equal(t, c.Engine, router) + + 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() + + assert.False(t, c.IsAborted()) + assert.Nil(t, c.Keys) + assert.Nil(t, c.Accepted) + assert.Len(t, c.Errors, 0) + assert.Len(t, c.Params, 0) + assert.Equal(t, c.index, -1) + assert.Equal(t, c.Writer.(*responseWriter), &c.writermem) +} + +// TestContextSetGet tests that a parameter is set correctly on the +// current context and can be retrieved using Get. +func TestContextSetGet(t *testing.T) { + c, _, _ := createTestContext() + c.Set("foo", "bar") + + value, err := c.Get("foo") + assert.Equal(t, value, "bar") + assert.True(t, err) + + value, err = c.Get("foo2") + assert.Nil(t, value) + assert.False(t, err) + + assert.Equal(t, c.MustGet("foo"), "bar") + assert.Panics(t, func() { c.MustGet("no_exist") }) +} + +// Tests that the response is serialized as JSON +// and Content-Type is set to application/json +func TestContextRenderJSON(t *testing.T) { + c, w, _ := createTestContext() + c.JSON(201, H{"foo": "bar"}) + + 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") +} + +// Tests that the response executes the templates +// and responds with Content-Type set to text/html +func TestContextRenderHTML(t *testing.T) { + c, w, router := createTestContext() + templ, _ := template.New("t").Parse(`Hello {{.name}}`) + router.SetHTMLTemplate(templ) + + c.HTML(201, "t", H{"name": "alexandernyquist"}) + + 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") +} + +// TestContextXML tests that the response is serialized as XML +// and Content-Type is set to application/xml +func TestContextRenderXML(t *testing.T) { + c, w, _ := createTestContext() + c.XML(201, H{"foo": "bar"}) + + assert.Equal(t, w.Code, 201) + assert.Equal(t, w.Body.String(), "bar") + assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/xml; charset=utf-8") +} + +// 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) + + 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") +} + +// TestContextString tests that the response is returned +// with Content-Type set to text/html +func TestContextRenderHTMLString(t *testing.T) { + c, w, _ := createTestContext() + c.HTMLString(201, "%s %d", "string", 3) + + assert.Equal(t, w.Code, 201) + assert.Equal(t, w.Body.String(), "string 3") + assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8") +} + +// TestContextData tests that the response can be written from `bytesting` +// with specified MIME type +func TestContextRenderData(t *testing.T) { + c, w, _ := createTestContext() + c.Data(201, "text/csv", []byte(`foo,bar`)) + + assert.Equal(t, w.Code, 201) + assert.Equal(t, w.Body.String(), "foo,bar") + assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/csv") +} + +// TODO +func TestContextRenderRedirectWithRelativePath(t *testing.T) { + c, w, _ := createTestContext() + c.Request, _ = http.NewRequest("POST", "http://example.com", nil) + assert.Panics(t, func() { c.Redirect(299, "/new_path") }) + assert.Panics(t, func() { c.Redirect(309, "/new_path") }) + + c.Redirect(302, "/path") + c.Writer.WriteHeaderNow() + assert.Equal(t, w.Code, 302) + assert.Equal(t, w.Header().Get("Location"), "/path") +} + +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) + } + + for _, err := range c.Errors.ByType(ErrorTypeInternal) { + assert.Equal(t, err.Type, ErrorTypeInternal) + } +} + +func TestContextFail(t *testing.T) { + c, w, _ := createTestContext() + c.Fail(401, errors.New("bad input")) + c.Writer.WriteHeaderNow() + + assert.Equal(t, w.Code, 401) + assert.Equal(t, c.LastError().Error(), "bad input") + assert.Equal(t, c.index, AbortIndex) + assert.True(t, c.IsAborted()) +} + +func TestContextClientIP(t *testing.T) { + c, _, _ := createTestContext() + c.Request, _ = http.NewRequest("POST", "", nil) + + c.Request.Header.Set("X-Real-IP", "10.10.10.10") + c.Request.Header.Set("X-Forwarded-For", "20.20.20.20 , 30.30.30.30") + c.Request.RemoteAddr = "40.40.40.40" + + assert.Equal(t, c.ClientIP(), "10.10.10.10") + c.Request.Header.Del("X-Real-IP") + assert.Equal(t, c.ClientIP(), "20.20.20.20") + c.Request.Header.Del("X-Forwarded-For") + assert.Equal(t, c.ClientIP(), "40.40.40.40") +} + +func TestContextContentType(t *testing.T) { + c, _, _ := createTestContext() + c.Request, _ = http.NewRequest("POST", "", nil) + c.Request.Header.Set("Content-Type", "application/json; charset=utf-8") + + assert.Equal(t, c.ContentType(), "application/json") +} + +func TestContextAutoBind(t *testing.T) { + c, w, _ := createTestContext() + c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}")) + c.Request.Header.Add("Content-Type", MIMEJSON) + var obj struct { + Foo string `json:"foo"` + Bar string `json:"bar"` + } + assert.True(t, c.Bind(&obj)) + assert.Equal(t, obj.Bar, "foo") + assert.Equal(t, obj.Foo, "bar") + assert.Equal(t, w.Body.Len(), 0) +} + +func TestContextBadAutoBind(t *testing.T) { + c, w, _ := createTestContext() + c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}")) + c.Request.Header.Add("Content-Type", MIMEJSON) + var obj struct { + Foo string `json:"foo"` + Bar string `json:"bar"` + } + + 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 TestContextBindWith(t *testing.T) { + c, w, _ := createTestContext() + c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}")) + c.Request.Header.Add("Content-Type", MIMEXML) + var obj struct { + Foo string `json:"foo"` + Bar string `json:"bar"` + } + assert.True(t, c.BindWith(&obj, binding.JSON)) + assert.Equal(t, obj.Bar, "foo") + assert.Equal(t, obj.Foo, "bar") + assert.Equal(t, w.Body.Len(), 0) +} diff --git a/debug.go b/debug.go index 3670b98..6c04aa0 100644 --- a/debug.go +++ b/debug.go @@ -4,7 +4,12 @@ package gin -import "log" +import ( + "log" + "os" +) + +var debugLogger = log.New(os.Stdout, "[GIN-debug] ", 0) func IsDebugging() bool { return ginMode == debugCode @@ -20,6 +25,6 @@ func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) { func debugPrint(format string, values ...interface{}) { if IsDebugging() { - log.Printf("[GIN-debug] "+format, values...) + debugLogger.Printf(format, values...) } } diff --git a/debug_test.go b/debug_test.go new file mode 100644 index 0000000..1e1e522 --- /dev/null +++ b/debug_test.go @@ -0,0 +1,20 @@ +// 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()) +} diff --git a/errors.go b/errors.go index f258ff3..819c294 100644 --- a/errors.go +++ b/errors.go @@ -18,13 +18,13 @@ const ( // Used internally to collect errors that occurred during an http request. type errorMsg struct { Err string `json:"error"` - Type uint32 `json:"-"` + Type int `json:"-"` Meta interface{} `json:"meta"` } type errorMsgs []errorMsg -func (a errorMsgs) ByType(typ uint32) errorMsgs { +func (a errorMsgs) ByType(typ int) errorMsgs { if len(a) == 0 { return a } diff --git a/examples/pluggable_renderer/example_pongo2.go b/examples/pluggable_renderer/example_pongo2.go index 9f745e1..9b79deb 100644 --- a/examples/pluggable_renderer/example_pongo2.go +++ b/examples/pluggable_renderer/example_pongo2.go @@ -1,11 +1,26 @@ package main import ( + "net/http" + "github.com/flosch/pongo2" "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 { cache map[string]*pongo2.Template } @@ -14,13 +29,6 @@ func newPongoRender() *pongoRender { 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 { file := data[0].(string) ctx := data[1].(pongo2.Context) @@ -36,23 +44,6 @@ func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{ p.cache[file] = tmpl t = tmpl } - writeHeader(w, code, "text/html") + render.WriteHeader(w, code, "text/html") 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") -} diff --git a/gin_test.go b/gin_test.go new file mode 100644 index 0000000..baac976 --- /dev/null +++ b/gin_test.go @@ -0,0 +1,141 @@ +// 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 init() { + SetMode(TestMode) +} + +func TestCreateEngine(t *testing.T) { + router := New() + assert.Equal(t, "/", router.absolutePath) + assert.Equal(t, router.engine, router) + assert.Empty(t, router.Handlers) + + // TODO + // assert.Equal(t, router.router.NotFound, router.handle404) + // assert.Equal(t, router.router.MethodNotAllowed, router.handle405) +} + +func TestCreateDefaultRouter(t *testing.T) { + router := Default() + assert.Len(t, router.Handlers, 2) +} + +func TestNoRouteWithoutGlobalHandlers(t *testing.T) { + middleware0 := func(c *Context) {} + middleware1 := func(c *Context) {} + + router := New() + + router.NoRoute(middleware0) + assert.Nil(t, router.Handlers) + assert.Len(t, router.noRoute, 1) + assert.Len(t, router.allNoRoute, 1) + assert.Equal(t, router.noRoute[0], middleware0) + assert.Equal(t, router.allNoRoute[0], middleware0) + + 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) +} + +func TestNoRouteWithGlobalHandlers(t *testing.T) { + middleware0 := func(c *Context) {} + middleware1 := func(c *Context) {} + middleware2 := func(c *Context) {} + + router := New() + router.Use(middleware2) + + 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) +} + +func TestNoMethodWithoutGlobalHandlers(t *testing.T) { + middleware0 := func(c *Context) {} + middleware1 := func(c *Context) {} + + router := New() + + router.NoMethod(middleware0) + assert.Empty(t, router.Handlers) + assert.Len(t, router.noMethod, 1) + assert.Len(t, router.allNoMethod, 1) + assert.Equal(t, router.noMethod[0], middleware0) + 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) +} + +func TestRebuild404Handlers(t *testing.T) { + +} + +func TestNoMethodWithGlobalHandlers(t *testing.T) { + middleware0 := func(c *Context) {} + middleware1 := func(c *Context) {} + middleware2 := func(c *Context) {} + + router := New() + router.Use(middleware2) + + router.NoMethod(middleware0) + assert.Len(t, router.allNoMethod, 2) + assert.Len(t, router.Handlers, 1) + assert.Len(t, router.noMethod, 1) + + assert.Equal(t, router.Handlers[0], middleware2) + assert.Equal(t, router.noMethod[0], middleware0) + assert.Equal(t, router.allNoMethod[0], middleware2) + assert.Equal(t, router.allNoMethod[1], middleware0) + + router.Use(middleware1) + assert.Len(t, router.allNoMethod, 3) + assert.Len(t, router.Handlers, 2) + assert.Len(t, router.noMethod, 1) + + assert.Equal(t, router.Handlers[0], middleware2) + assert.Equal(t, router.Handlers[1], middleware1) + assert.Equal(t, router.noMethod[0], middleware0) + assert.Equal(t, router.allNoMethod[0], middleware2) + assert.Equal(t, router.allNoMethod[1], middleware1) + assert.Equal(t, router.allNoMethod[2], middleware0) +} diff --git a/input_holder.go b/input_holder.go index 9888e50..aa5fca9 100644 --- a/input_holder.go +++ b/input_holder.go @@ -19,10 +19,10 @@ func (i inputHolder) FromPOST(key string) (va string) { } func (i inputHolder) Get(key string) string { - if value, exists := i.fromGET(key); exists { + if value, exists := i.fromPOST(key); exists { return value } - if value, exists := i.fromPOST(key); exists { + if value, exists := i.fromGET(key); exists { return value } return "" @@ -31,19 +31,17 @@ func (i inputHolder) Get(key string) string { func (i inputHolder) fromGET(key string) (string, bool) { req := i.context.Request req.ParseForm() - if values, ok := req.Form[key]; ok { + if values, ok := req.Form[key]; ok && len(values) > 0 { return values[0], true - } else { - return "", false } + return "", false } func (i inputHolder) fromPOST(key string) (string, bool) { req := i.context.Request req.ParseForm() - if values, ok := req.PostForm[key]; ok { + if values, ok := req.PostForm[key]; ok && len(values) > 0 { return values[0], true - } else { - return "", false } + return "", false } diff --git a/logger.go b/logger.go index fedfe24..87304dd 100644 --- a/logger.go +++ b/logger.go @@ -25,27 +25,27 @@ func ErrorLogger() HandlerFunc { return ErrorLoggerT(ErrorTypeAll) } -func ErrorLoggerT(typ uint32) HandlerFunc { +func ErrorLoggerT(typ int) HandlerFunc { return func(c *Context) { c.Next() if !c.Writer.Written() { - errs := c.Errors.ByType(typ) - if len(errs) > 0 { - c.JSON(-1, c.Errors) + if errs := c.Errors.ByType(typ); len(errs) > 0 { + c.JSON(-1, errs) } } } } func Logger() HandlerFunc { - return LoggerWithFile(DefaultLogFile) + return LoggerWithFile(DefaultWriter) } func LoggerWithFile(out io.Writer) HandlerFunc { return func(c *Context) { // Start timer start := time.Now() + path := c.Request.URL.Path // Process request c.Next() @@ -67,7 +67,7 @@ func LoggerWithFile(out io.Writer) HandlerFunc { latency, clientIP, methodColor, reset, method, - c.Request.URL.Path, + path, comment, ) } diff --git a/mode.go b/mode.go index 21b9ac5..8c54fdb 100644 --- a/mode.go +++ b/mode.go @@ -5,7 +5,6 @@ package gin import ( - "log" "os" "github.com/mattn/go-colorable" @@ -24,7 +23,7 @@ const ( testCode = iota ) -var DefaultLogFile = colorable.NewColorableStdout() +var DefaultWriter = colorable.NewColorableStdout() var ginMode int = debugCode var modeName string = DebugMode @@ -46,7 +45,7 @@ func SetMode(value string) { case TestMode: ginMode = testCode default: - log.Panic("gin mode unknown: " + value) + panic("gin mode unknown: " + value) } modeName = value } diff --git a/recovery.go b/recovery.go index 82b76ee..e8b1ba4 100644 --- a/recovery.go +++ b/recovery.go @@ -7,9 +7,9 @@ package gin import ( "bytes" "fmt" + "io" "io/ioutil" "log" - "net/http" "runtime" ) @@ -20,6 +20,31 @@ var ( slash = []byte("/") ) +// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. +// While Gin is in development mode, Recovery will also output the panic as HTML. +func Recovery() HandlerFunc { + return RecoveryWithFile(DefaultWriter) +} + +func RecoveryWithFile(out io.Writer) HandlerFunc { + var logger *log.Logger + if out != nil { + logger = log.New(out, "", log.LstdFlags) + } + return func(c *Context) { + defer func() { + if err := recover(); err != nil { + if logger != nil { + stack := stack(3) + logger.Printf("Gin Panic Recover!! -> %s\n%s\n", err, stack) + } + c.AbortWithStatus(500) + } + }() + c.Next() + } +} + // stack returns a nicely formated stack frame, skipping skip frames func stack(skip int) []byte { buf := new(bytes.Buffer) // the returned data @@ -80,19 +105,3 @@ func function(pc uintptr) []byte { name = bytes.Replace(name, centerDot, dot, -1) return name } - -// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. -// While Gin is in development mode, Recovery will also output the panic as HTML. -func Recovery() HandlerFunc { - return func(c *Context) { - defer func() { - if err := recover(); err != nil { - stack := stack(3) - log.Printf("PANIC: %s\n%s", err, stack) - c.Writer.WriteHeader(http.StatusInternalServerError) - } - }() - - c.Next() - } -} diff --git a/recovery_test.go b/recovery_test.go new file mode 100644 index 0000000..d471306 --- /dev/null +++ b/recovery_test.go @@ -0,0 +1,42 @@ +// 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 ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +// TestPanicInHandler assert that panic has been recovered. +func TestPanicInHandler(t *testing.T) { + buffer := new(bytes.Buffer) + router := New() + router.Use(RecoveryWithFile(buffer)) + router.GET("/recovery", func(_ *Context) { + panic("Oupps, Houston, we have a problem") + }) + // RUN + w := performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, w.Code, 500) + assert.Contains(t, buffer.String(), "Gin Panic Recover!! -> Oupps, Houston, we have a problem") + assert.Contains(t, buffer.String(), "TestPanicInHandler") +} + +// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used. +func TestPanicWithAbort(t *testing.T) { + router := New() + router.Use(RecoveryWithFile(nil)) + router.GET("/recovery", func(c *Context) { + c.AbortWithStatus(400) + panic("Oupps, Houston, we have a problem") + }) + // RUN + w := performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, w.Code, 500) // NOT SURE +} diff --git a/routergroup.go b/routergroup.go index 9b51221..843238c 100644 --- a/routergroup.go +++ b/routergroup.go @@ -103,11 +103,11 @@ func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) { func (group *RouterGroup) Static(relativePath, root string) { absolutePath := group.calculateAbsolutePath(relativePath) handler := group.createStaticHandler(absolutePath, root) - absolutePath = path.Join(absolutePath, "/*filepath") + relativePath = path.Join(relativePath, "/*filepath") // Register GET and HEAD handlers - group.GET(absolutePath, handler) - group.HEAD(absolutePath, handler) + group.GET(relativePath, handler) + group.HEAD(relativePath, handler) } func (group *RouterGroup) createStaticHandler(absolutePath, root string) func(*Context) { diff --git a/routes_test.go b/routes_test.go new file mode 100644 index 0000000..ce61a41 --- /dev/null +++ b/routes_test.go @@ -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) +} diff --git a/utils.go b/utils.go index 20ba5a8..19fef55 100644 --- a/utils.go +++ b/utils.go @@ -6,7 +6,6 @@ package gin import ( "encoding/xml" - "log" "path" "reflect" "runtime" @@ -51,29 +50,33 @@ func filterFlags(content string) string { func chooseData(custom, wildcard interface{}) interface{} { if custom == nil { if wildcard == nil { - log.Panic("negotiation config is invalid") + panic("negotiation config is invalid") } return wildcard } return custom } -func parseAccept(acceptHeader string) (parts []string) { - parts = strings.Split(acceptHeader, ",") - for i, part := range parts { +func parseAccept(acceptHeader string) []string { + parts := strings.Split(acceptHeader, ",") + out := make([]string, 0, len(parts)) + for _, part := range parts { index := strings.IndexByte(part, ';') if index >= 0 { 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 { size := len(str) 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] }