Merge branch 'master' into develop

# Conflicts:
#	debug_test.go
This commit is contained in:
Manu Mtz.-Almeida 2016-01-27 03:40:52 +01:00
commit 8949247b92
10 changed files with 47 additions and 36 deletions

View File

@ -1,6 +1,6 @@
#Gin Web Framework #Gin Web Framework
<img align="right" src="https://s3.amazonaws.com/uploads.hipchat.com/36744/1498287/JVR32LgyEGCiy01/path4201%20copy%202.png"> <img align="right" src="https://raw.githubusercontent.com/gin-gonic/gin/master/logo.jpg">
[![Build Status](https://travis-ci.org/gin-gonic/gin.svg)](https://travis-ci.org/gin-gonic/gin) [![Build Status](https://travis-ci.org/gin-gonic/gin.svg)](https://travis-ci.org/gin-gonic/gin)
[![Coverage Status](https://coveralls.io/repos/gin-gonic/gin/badge.svg?branch=master)](https://coveralls.io/r/gin-gonic/gin?branch=master) [![Coverage Status](https://coveralls.io/repos/gin-gonic/gin/badge.svg?branch=master)](https://coveralls.io/r/gin-gonic/gin?branch=master)
[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.svg)](https://godoc.org/github.com/gin-gonic/gin) [![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.svg)](https://godoc.org/github.com/gin-gonic/gin)

View File

@ -185,7 +185,8 @@ func (c *Context) MustGet(key string) interface{} {
// Query is a shortcut for c.Request.URL.Query().Get(key) // Query is a shortcut for c.Request.URL.Query().Get(key)
// It is used to return the url query values. // It is used to return the url query values.
// ?id=1234&name=Manu // It returns an empty string ("") when the value does not exist.
// /path?id=1234&name=Manu
// c.Query("id") == "1234" // c.Query("id") == "1234"
// c.Query("name") == "Manu" // c.Query("name") == "Manu"
// c.Query("wtf") == "" // c.Query("wtf") == ""
@ -195,12 +196,18 @@ func (c *Context) Query(key string) string {
} }
// PostForm is a shortcut for c.Request.PostFormValue(key) // PostForm is a shortcut for c.Request.PostFormValue(key)
// It returns an empty string ("") when the value does not exist.
func (c *Context) PostForm(key string) string { func (c *Context) PostForm(key string) string {
value, _ := c.postForm(key) value, _ := c.postForm(key)
return value return value
} }
// Param is a shortcut for c.Params.ByName(key) // Param returns the value of the URL param.
// It is a shortcut for c.Params.ByName(key)
// router.GET("/user/:id", func(c *gin.Context) {
// // a GET request to /user/john
// id := c.Param("id") // id == "john"
// })
func (c *Context) Param(key string) string { func (c *Context) Param(key string) string {
return c.Params.ByName(key) return c.Params.ByName(key)
} }
@ -214,11 +221,9 @@ func (c *Context) DefaultPostForm(key, defaultValue string) string {
// DefaultQuery returns the keyed url query value if it exists, othewise it returns the // DefaultQuery returns the keyed url query value if it exists, othewise it returns the
// specified defaultValue. // specified defaultValue.
// ``` // //?name=Manu
// /?name=Manu
// c.DefaultQuery("name", "unknown") == "Manu" // c.DefaultQuery("name", "unknown") == "Manu"
// c.DefaultQuery("id", "none") == "none" // c.DefaultQuery("id", "none") == "none"
// ```
func (c *Context) DefaultQuery(key, defaultValue string) string { func (c *Context) DefaultQuery(key, defaultValue string) string {
if value, ok := c.query(key); ok { if value, ok := c.query(key); ok {
return value return value

View File

@ -153,7 +153,7 @@ func TestContextHandlerName(t *testing.T) {
c, _, _ := CreateTestContext() c, _, _ := CreateTestContext()
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest} c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}
assert.Equal(t, c.HandlerName(), "github.com/gin-gonic/gin.handlerNameTest") assert.Regexp(t, "^(.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest$", c.HandlerName())
} }
func handlerNameTest(c *Context) { func handlerNameTest(c *Context) {

View File

@ -63,7 +63,7 @@ func TestDebugPrintRoutes(t *testing.T) {
defer teardown() defer teardown()
debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest}) debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
assert.Equal(t, w.String(), "[GIN-debug] GET /path/to/route/:param --> github.com/gin-gonic/gin.handlerNameTest (2 handlers)\n") assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, w.String())
} }
func setup(w io.Writer) { func setup(w io.Writer) {

View File

@ -109,13 +109,11 @@ func (a errorMsgs) Last() *Error {
} }
// Returns an array will all the error messages. // Returns an array will all the error messages.
// Example // Example:
// ```
// c.Error(errors.New("first")) // c.Error(errors.New("first"))
// c.Error(errors.New("second")) // c.Error(errors.New("second"))
// c.Error(errors.New("third")) // c.Error(errors.New("third"))
// c.Errors.Errors() // == []string{"first", "second", "third"} // c.Errors.Errors() // == []string{"first", "second", "third"}
// ``
func (a errorMsgs) Errors() []string { func (a errorMsgs) Errors() []string {
if len(a) == 0 { if len(a) == 0 {
return nil return nil

View File

@ -214,32 +214,42 @@ func TestListOfRoutes(t *testing.T) {
list := router.Routes() list := router.Routes()
assert.Len(t, list, 7) assert.Len(t, list, 7)
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/favicon.ico", Path: "/favicon.ico",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test1$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/", Path: "/",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test1$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/users/", Path: "/users/",
Handler: "github.com/gin-gonic/gin.handler_test2", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test2$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "GET", Method: "GET",
Path: "/users/:id", Path: "/users/:id",
Handler: "github.com/gin-gonic/gin.handler_test1", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test1$",
}) })
assert.Contains(t, list, RouteInfo{ assertRoutePresent(t, list, RouteInfo{
Method: "POST", Method: "POST",
Path: "/users/:id", Path: "/users/:id",
Handler: "github.com/gin-gonic/gin.handler_test2", Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handler_test2$",
}) })
} }
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
for _, gotRoute := range gotRoutes {
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
assert.Regexp(t, wantRoute.Path, gotRoute.Path)
return
}
}
t.Errorf("route not found: %v", wantRoute)
}
func handler_test1(c *Context) {} func handler_test1(c *Context) {}
func handler_test2(c *Context) {} func handler_test2(c *Context) {}

BIN
logo.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -29,10 +29,8 @@ const (
// Note that both Logger and Recovery provides custom ways to configure their // Note that both Logger and Recovery provides custom ways to configure their
// output io.Writer. // output io.Writer.
// To support coloring in Windows use: // To support coloring in Windows use:
// ```
// import "github.com/mattn/go-colorable" // import "github.com/mattn/go-colorable"
// gin.DefaultWriter = colorable.NewColorableStdout() // gin.DefaultWriter = colorable.NewColorableStdout()
// ```
var DefaultWriter io.Writer = os.Stdout var DefaultWriter io.Writer = os.Stdout
var DefaultErrorWriter io.Writer = os.Stderr var DefaultErrorWriter io.Writer = os.Stderr

View File

@ -23,7 +23,7 @@ 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.
func Recovery() HandlerFunc { func Recovery() HandlerFunc {
return RecoveryWithWriter(DefaultWriter) return RecoveryWithWriter(DefaultErrorWriter)
} }
func RecoveryWithWriter(out io.Writer) HandlerFunc { func RecoveryWithWriter(out io.Writer) HandlerFunc {

View File

@ -78,7 +78,7 @@ func TestFilterFlags(t *testing.T) {
} }
func TestFunctionName(t *testing.T) { func TestFunctionName(t *testing.T) {
assert.Equal(t, nameOfFunction(somefunction), "github.com/gin-gonic/gin.somefunction") assert.Regexp(t, `^(.*/vendor/)?github.com/gin-gonic/gin.somefunction$`, nameOfFunction(somefunction))
} }
func somefunction() { func somefunction() {