Cosmetic changes

This commit is contained in:
Manu Mtz-Almeida 2015-05-18 20:50:46 +02:00
parent 21b5154fd7
commit 9ecb76ef6e
4 changed files with 30 additions and 44 deletions

26
gin.go
View File

@ -175,17 +175,24 @@ func (engine *Engine) RunTLS(addr string, cert string, key string) (err error) {
// ServeHTTP makes the router implement the http.Handler interface. // ServeHTTP makes the router implement the http.Handler interface.
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
context := engine.pool.Get().(*Context) c := engine.getcontext(w, req)
context.writermem.reset(w) engine.handleHTTPRequest(c)
context.Request = req engine.putcontext(c)
context.reset()
engine.serveHTTPRequest(context)
engine.pool.Put(context)
} }
func (engine *Engine) serveHTTPRequest(context *Context) { func (engine *Engine) getcontext(w http.ResponseWriter, req *http.Request) *Context {
c := engine.pool.Get().(*Context)
c.writermem.reset(w)
c.Request = req
c.reset()
return c
}
func (engine *Engine) putcontext(c *Context) {
engine.pool.Put(c)
}
func (engine *Engine) handleHTTPRequest(context *Context) {
httpMethod := context.Request.Method httpMethod := context.Request.Method
path := context.Request.URL.Path path := context.Request.URL.Path
@ -193,7 +200,6 @@ func (engine *Engine) serveHTTPRequest(context *Context) {
if root := engine.trees[httpMethod]; root != nil { if root := engine.trees[httpMethod]; root != nil {
// Find route in tree // Find route in tree
handlers, params, tsr := root.getValue(path, context.Params) handlers, params, tsr := root.getValue(path, context.Params)
// Dispatch if we found any handlers
if handlers != nil { if handlers != nil {
context.handlers = handlers context.handlers = handlers
context.Params = params context.Params = params

View File

@ -21,7 +21,6 @@ var (
) )
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. // 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 { func Recovery() HandlerFunc {
return RecoveryWithWriter(DefaultWriter) return RecoveryWithWriter(DefaultWriter)
} }
@ -36,7 +35,7 @@ func RecoveryWithWriter(out io.Writer) HandlerFunc {
if err := recover(); err != nil { if err := recover(); err != nil {
if logger != nil { if logger != nil {
stack := stack(3) stack := stack(3)
logger.Printf("Gin Panic Recover!! -> %s\n%s\n", err, stack) logger.Printf("Panic recovery -> %s\n%s\n", err, stack)
} }
c.AbortWithStatus(500) c.AbortWithStatus(500)
} }

View File

@ -23,7 +23,7 @@ func TestPanicInHandler(t *testing.T) {
w := performRequest(router, "GET", "/recovery") w := performRequest(router, "GET", "/recovery")
// TEST // TEST
assert.Equal(t, w.Code, 500) 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(), "Panic recovery -> Oupps, Houston, we have a problem")
assert.Contains(t, buffer.String(), "TestPanicInHandler") assert.Contains(t, buffer.String(), "TestPanicInHandler")
} }

View File

@ -24,40 +24,34 @@ func performRequest(r http.Handler, method, path string) *httptest.ResponseRecor
} }
func testRouteOK(method string, t *testing.T) { func testRouteOK(method string, t *testing.T) {
// SETUP
passed := false passed := false
r := New() r := New()
r.Handle(method, "/test", HandlersChain{func(c *Context) { r.Handle(method, "/test", HandlersChain{func(c *Context) {
passed = true passed = true
}}) }})
// RUN
w := performRequest(r, method, "/test") w := performRequest(r, method, "/test")
// TEST
assert.True(t, passed) assert.True(t, passed)
assert.Equal(t, w.Code, http.StatusOK) assert.Equal(t, w.Code, http.StatusOK)
} }
// TestSingleRouteOK tests that POST route is correctly invoked. // TestSingleRouteOK tests that POST route is correctly invoked.
func testRouteNotOK(method string, t *testing.T) { func testRouteNotOK(method string, t *testing.T) {
// SETUP
passed := false passed := false
router := New() router := New()
router.Handle(method, "/test_2", HandlersChain{func(c *Context) { router.Handle(method, "/test_2", HandlersChain{func(c *Context) {
passed = true passed = true
}}) }})
// RUN
w := performRequest(router, method, "/test") w := performRequest(router, method, "/test")
// TEST
assert.False(t, passed) assert.False(t, passed)
assert.Equal(t, w.Code, http.StatusNotFound) assert.Equal(t, w.Code, http.StatusNotFound)
} }
// TestSingleRouteOK tests that POST route is correctly invoked. // TestSingleRouteOK tests that POST route is correctly invoked.
func testRouteNotOK2(method string, t *testing.T) { func testRouteNotOK2(method string, t *testing.T) {
// SETUP
passed := false passed := false
router := New() router := New()
var methodRoute string var methodRoute string
@ -70,10 +64,8 @@ func testRouteNotOK2(method string, t *testing.T) {
passed = true passed = true
}}) }})
// RUN
w := performRequest(router, method, "/test") w := performRequest(router, method, "/test")
// TEST
assert.False(t, passed) assert.False(t, passed)
assert.Equal(t, w.Code, http.StatusMethodNotAllowed) assert.Equal(t, w.Code, http.StatusMethodNotAllowed)
} }
@ -125,10 +117,9 @@ func TestRouteParamsByName(t *testing.T) {
assert.Equal(t, lastName, c.DefaultParamValue("last_name", "nothing")) assert.Equal(t, lastName, c.DefaultParamValue("last_name", "nothing"))
assert.Equal(t, c.DefaultParamValue("noKey", "default"), "default") assert.Equal(t, c.DefaultParamValue("noKey", "default"), "default")
}) })
// RUN
w := performRequest(router, "GET", "/test/john/smith/is/super/great") w := performRequest(router, "GET", "/test/john/smith/is/super/great")
// TEST
assert.Equal(t, w.Code, 200) assert.Equal(t, w.Code, 200)
assert.Equal(t, name, "john") assert.Equal(t, name, "john")
assert.Equal(t, lastName, "smith") assert.Equal(t, lastName, "smith")
@ -149,13 +140,11 @@ func TestRouteStaticFile(t *testing.T) {
f.Close() f.Close()
// SETUP gin // SETUP gin
r := New() router := New()
r.Static("./", testRoot) router.Static("./", testRoot)
// RUN w := performRequest(router, "GET", filePath)
w := performRequest(r, "GET", filePath)
// TEST
assert.Equal(t, w.Code, 200) assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Body.String(), "Gin Web Framework") assert.Equal(t, w.Body.String(), "Gin Web Framework")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8") assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
@ -163,35 +152,27 @@ func TestRouteStaticFile(t *testing.T) {
// TestHandleStaticDir - ensure the root/sub dir handles properly // TestHandleStaticDir - ensure the root/sub dir handles properly
func TestRouteStaticDir(t *testing.T) { func TestRouteStaticDir(t *testing.T) {
// SETUP router := New()
r := New() router.Static("/", "./")
r.Static("/", "./")
// RUN w := performRequest(router, "GET", "/")
w := performRequest(r, "GET", "/")
// TEST
bodyAsString := w.Body.String()
assert.Equal(t, w.Code, 200) assert.Equal(t, w.Code, 200)
assert.NotEmpty(t, bodyAsString) assert.Contains(t, w.Body.String(), "gin.go")
assert.Contains(t, bodyAsString, "gin.go")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8") assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
} }
// TestHandleHeadToDir - ensure the root/sub dir handles properly // TestHandleHeadToDir - ensure the root/sub dir handles properly
func TestRouteHeadToDir(t *testing.T) { func TestRouteHeadToDir(t *testing.T) {
// SETUP
router := New() router := New()
router.Static("/", "./") router.Static("/", "./")
// RUN
w := performRequest(router, "HEAD", "/") w := performRequest(router, "HEAD", "/")
// TEST
bodyAsString := w.Body.String()
assert.Equal(t, w.Code, 200) assert.Equal(t, w.Code, 200)
assert.NotEmpty(t, bodyAsString) assert.Contains(t, w.Body.String(), "gin.go")
assert.Contains(t, bodyAsString, "gin.go") assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
}
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8") assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
} }