Browse Source

Cosmetic changes

Manu Mtz-Almeida 10 năm trước cách đây
mục cha
commit
9ecb76ef6e
4 tập tin đã thay đổi với 28 bổ sung42 xóa
  1. 14 8
      gin.go
  2. 1 2
      recovery.go
  3. 1 1
      recovery_test.go
  4. 12 31
      routes_test.go

+ 14 - 8
gin.go

@@ -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.
 func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
-	context := engine.pool.Get().(*Context)
-	context.writermem.reset(w)
-	context.Request = req
-	context.reset()
+	c := engine.getcontext(w, req)
+	engine.handleHTTPRequest(c)
+	engine.putcontext(c)
+}
 
-	engine.serveHTTPRequest(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
+}
 
-	engine.pool.Put(context)
+func (engine *Engine) putcontext(c *Context) {
+	engine.pool.Put(c)
 }
 
-func (engine *Engine) serveHTTPRequest(context *Context) {
+func (engine *Engine) handleHTTPRequest(context *Context) {
 	httpMethod := context.Request.Method
 	path := context.Request.URL.Path
 
@@ -193,7 +200,6 @@ func (engine *Engine) serveHTTPRequest(context *Context) {
 	if root := engine.trees[httpMethod]; root != nil {
 		// Find route in tree
 		handlers, params, tsr := root.getValue(path, context.Params)
-		// Dispatch if we found any handlers
 		if handlers != nil {
 			context.handlers = handlers
 			context.Params = params

+ 1 - 2
recovery.go

@@ -21,7 +21,6 @@ var (
 )
 
 // 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 RecoveryWithWriter(DefaultWriter)
 }
@@ -36,7 +35,7 @@ func RecoveryWithWriter(out io.Writer) HandlerFunc {
 			if err := recover(); err != nil {
 				if logger != nil {
 					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)
 			}

+ 1 - 1
recovery_test.go

@@ -23,7 +23,7 @@ func TestPanicInHandler(t *testing.T) {
 	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(), "Panic recovery -> Oupps, Houston, we have a problem")
 	assert.Contains(t, buffer.String(), "TestPanicInHandler")
 }
 

+ 12 - 31
routes_test.go

@@ -24,40 +24,34 @@ func performRequest(r http.Handler, method, path string) *httptest.ResponseRecor
 }
 
 func testRouteOK(method string, t *testing.T) {
-	// SETUP
 	passed := false
 	r := New()
 	r.Handle(method, "/test", HandlersChain{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", HandlersChain{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
@@ -70,10 +64,8 @@ func testRouteNotOK2(method string, t *testing.T) {
 		passed = true
 	}})
 
-	// RUN
 	w := performRequest(router, method, "/test")
 
-	// TEST
 	assert.False(t, passed)
 	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, c.DefaultParamValue("noKey", "default"), "default")
 	})
-	// RUN
+
 	w := performRequest(router, "GET", "/test/john/smith/is/super/great")
 
-	// TEST
 	assert.Equal(t, w.Code, 200)
 	assert.Equal(t, name, "john")
 	assert.Equal(t, lastName, "smith")
@@ -149,13 +140,11 @@ func TestRouteStaticFile(t *testing.T) {
 	f.Close()
 
 	// SETUP gin
-	r := New()
-	r.Static("./", testRoot)
+	router := New()
+	router.Static("./", testRoot)
 
-	// RUN
-	w := performRequest(r, "GET", filePath)
+	w := performRequest(router, "GET", filePath)
 
-	// TEST
 	assert.Equal(t, w.Code, 200)
 	assert.Equal(t, w.Body.String(), "Gin Web Framework")
 	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
 func TestRouteStaticDir(t *testing.T) {
-	// SETUP
-	r := New()
-	r.Static("/", "./")
+	router := New()
+	router.Static("/", "./")
 
-	// RUN
-	w := performRequest(r, "GET", "/")
+	w := performRequest(router, "GET", "/")
 
-	// TEST
-	bodyAsString := w.Body.String()
 	assert.Equal(t, w.Code, 200)
-	assert.NotEmpty(t, bodyAsString)
-	assert.Contains(t, bodyAsString, "gin.go")
+	assert.Contains(t, w.Body.String(), "gin.go")
 	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
 }
 
 // TestHandleHeadToDir - ensure the root/sub dir handles properly
 func TestRouteHeadToDir(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.Contains(t, w.Body.String(), "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")
 }