From 66c4b81579a6b357b848dafffde22159d3dfd8af Mon Sep 17 00:00:00 2001 From: Naoki Takano Date: Wed, 30 Dec 2015 22:49:38 -0800 Subject: [PATCH 01/18] Added exits check tests for binding --- binding/binding_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/binding/binding_test.go b/binding/binding_test.go index 713e2e5a..1488d575 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -121,6 +121,28 @@ func TestValidationDisabled(t *testing.T) { assert.NoError(t, err) } +func TestExistsSucceeds(t *testing.T) { + type HogeStruct struct { + Hoge *int `json:"hoge" binding:"exists"` + } + + var obj HogeStruct + req := requestWithBody("POST", "/", `{"hoge": 0}`) + err := JSON.Bind(req, &obj) + assert.NoError(t, err) +} + +func TestExistsFails(t *testing.T) { + type HogeStruct struct { + Hoge *int `json:"foo" binding:"exists"` + } + + var obj HogeStruct + req := requestWithBody("POST", "/", `{"boen": 0}`) + err := JSON.Bind(req, &obj) + assert.Error(t, err) +} + func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) { b := Form assert.Equal(t, b.Name(), "form") From 3e56f79fba6ce82175c92c2d01b5ce4faba92c3a Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Sat, 30 Jan 2016 01:32:46 +0100 Subject: [PATCH 02/18] Coherent example code in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a111d29..7db3fb2d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ - "message": "hello world", + "message": "pong", }) }) r.Run() // listen and server on 0.0.0.0:8080 From 27f912f5b2c8ee7dd7655d479dee2fc4d41d32d7 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Sat, 30 Jan 2016 01:34:20 +0100 Subject: [PATCH 03/18] README little fixes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7db3fb2d..14bfe0e0 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.svg)](https://godoc.org/github.com/gin-gonic/gin) [![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster thanks to [httprouter](https://github.com/julienschmidt/httprouter). If you need performance and good productivity, you will love Gin. +Gin is a web framework written in Go (Golang). It features a martini-like API with much better performance, up to 40 times faster thanks to [httprouter](https://github.com/julienschmidt/httprouter). If you need performance and good productivity, you will love Gin. From 00ff808ac5f89e78d545dd83ff4bf35e09a89e1c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 17 Feb 2016 10:45:44 +0800 Subject: [PATCH 04/18] fix: example format. --- examples/app-engine/hello.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/app-engine/hello.go b/examples/app-engine/hello.go index f5daf824..a5e17962 100644 --- a/examples/app-engine/hello.go +++ b/examples/app-engine/hello.go @@ -1,8 +1,8 @@ package hello import ( - "net/http" "github.com/gin-gonic/gin" + "net/http" ) // This function's name is a must. App Engine uses it to drive the requests properly. @@ -11,13 +11,13 @@ func init() { r := gin.New() // Define your handlers - r.GET("/", func(c *gin.Context){ + r.GET("/", func(c *gin.Context) { c.String(200, "Hello World!") }) - r.GET("/ping", func(c *gin.Context){ + r.GET("/ping", func(c *gin.Context) { c.String(200, "pong") }) // Handle all requests using net/http http.Handle("/", r) -} \ No newline at end of file +} From 701989d26e4aa9055d1291f27c3c188b018359a7 Mon Sep 17 00:00:00 2001 From: Justin Mayhew Date: Wed, 17 Feb 2016 21:45:37 -0400 Subject: [PATCH 05/18] Fix README mistake --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 14bfe0e0..53763a3d 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ func main() { // By default it serves on :8080 unless a // PORT environment variable was defined. router.Run() - // router.Run.Run(":3000") for a hard coded port + // router.Run(":3000") for a hard coded port } ``` From 9b95308930512a4b1c097ee0d56d4df847ad64a0 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 20 Feb 2016 11:18:40 +0800 Subject: [PATCH 06/18] Add 1.5 and 1.6 test. Signed-off-by: Bo-Yi Wu --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 695f0b7e..2f78c8af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,8 @@ language: go sudo: false go: - 1.4 - - 1.4.2 + - 1.5 + - 1.6 - tip script: From 3a040f8550191dd89e200cdbdd52de1569cee848 Mon Sep 17 00:00:00 2001 From: bigwheel Date: Wed, 24 Feb 2016 02:44:52 +0900 Subject: [PATCH 07/18] Add integration test using httptest --- gin_integration_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/gin_integration_test.go b/gin_integration_test.go index 4777c0c9..85216970 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "net/http/httptest" ) func testRequest(t *testing.T, url string) { @@ -103,3 +104,28 @@ func TestBadUnixSocket(t *testing.T) { router := New() assert.Error(t, router.RunUnix("#/tmp/unix_unit_test")) } + +func TestWithHttptestWithAutoSelectedPort(t *testing.T) { + router := New() + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + + ts := httptest.NewServer(router) + defer ts.Close() + + testRequest(t, ts.URL+"/example") +} + +func TestWithHttptestWithSpecifiedPort(t *testing.T) { + router := New() + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + + l, _ := net.Listen("tcp", ":8033") + ts := httptest.Server{ + Listener: l, + Config: &http.Server{Handler: router}, + } + ts.Start() + defer ts.Close() + + testRequest(t, "http://localhost:8033/example") +} From 8b649abbf5ba5d298d11e75b99f2b079465efbc3 Mon Sep 17 00:00:00 2001 From: Roman Belyakovsky Date: Wed, 24 Feb 2016 17:02:40 +0300 Subject: [PATCH 08/18] Added note for net/http import and fixed numbering in readme --- README.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 53763a3d..0086666b 100644 --- a/README.md +++ b/README.md @@ -85,14 +85,21 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648 ## Start using it 1. Download and install it: -```sh -$ go get github.com/gin-gonic/gin -``` + ```sh + $ go get github.com/gin-gonic/gin + ``` + 2. Import it in your code: -```go -import "github.com/gin-gonic/gin" -``` + ```go + import "github.com/gin-gonic/gin" + ``` + +3. Import net/http if required: + + ```go + import "net/http" + ``` ##API Examples From cbf43049fed5d0a2aa7ca2feb7c1edb5ef89e152 Mon Sep 17 00:00:00 2001 From: Javier Provecho Fernandez Date: Wed, 24 Feb 2016 19:02:39 +0100 Subject: [PATCH 09/18] Explain better the `net/http` note. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0086666b..9dd074a9 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648 import "github.com/gin-gonic/gin" ``` -3. Import net/http if required: +3. (Optional) Import `net/http`. This is required for example if using constants such as `http.StatusOK`. ```go import "net/http" From 19f77bdd4ce66f2c79dd56cec3baf8de206f1115 Mon Sep 17 00:00:00 2001 From: ishanray Date: Sat, 5 Mar 2016 02:27:19 +0400 Subject: [PATCH 10/18] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a111d29..f47e020c 100644 --- a/README.md +++ b/README.md @@ -583,7 +583,7 @@ func main() { // /admin/secrets endpoint // hit "localhost:8080/admin/secrets authorized.GET("/secrets", func(c *gin.Context) { - // get user, it was setted by the BasicAuth middleware + // get user, it was set by the BasicAuth middleware user := c.MustGet(gin.AuthUserKey).(string) if secret, ok := secrets[user]; ok { c.JSON(http.StatusOK, gin.H{"user": user, "secret": secret}) From 13565e1bf53792becc94fd4f0e775f5b2b0a639e Mon Sep 17 00:00:00 2001 From: Michael Puncel Date: Mon, 7 Mar 2016 20:54:10 -0800 Subject: [PATCH 11/18] fix typo in godoc for func (*Context) HandlerName --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index 2fb69b73..a2d2d220 100644 --- a/context.go +++ b/context.go @@ -77,7 +77,7 @@ func (c *Context) Copy() *Context { return &cp } -// HandlerName returns the main handle's name. For example if the handler is "handleGetUsers()", this +// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()", this // function will return "main.handleGetUsers" func (c *Context) HandlerName() string { return nameOfFunction(c.handlers.Last()) From 7171b967a3f1cf44edef4071476f4e00ccfdb03c Mon Sep 17 00:00:00 2001 From: Michael Puncel Date: Mon, 7 Mar 2016 21:56:46 -0800 Subject: [PATCH 12/18] s/currect/current --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index a2d2d220..5db121a5 100644 --- a/context.go +++ b/context.go @@ -98,7 +98,7 @@ func (c *Context) Next() { } } -// IsAborted returns true if the currect context was aborted. +// IsAborted returns true if the current context was aborted. func (c *Context) IsAborted() bool { return c.index >= abortIndex } From 1f2e53dfd1c4ba0195caf0b7008d66a086751438 Mon Sep 17 00:00:00 2001 From: Albin Gilles Date: Wed, 9 Mar 2016 08:29:01 +0100 Subject: [PATCH 13/18] Fix typo on gin_test.go Fixes #550, change a duplicate assert on the Path of the route by an assert on Handler name associated with the route. --- gin_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gin_test.go b/gin_test.go index 15f480e4..af95ab84 100644 --- a/gin_test.go +++ b/gin_test.go @@ -244,7 +244,7 @@ func TestListOfRoutes(t *testing.T) { 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) + assert.Regexp(t, wantRoute.Handler, gotRoute.Handler) return } } From 38b77a71d984c75f99c3c2e9f3b77b53b48c3c7c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 14 Mar 2016 09:11:59 +0800 Subject: [PATCH 14/18] Add manners package. --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 56d9f6e7..37047d2b 100644 --- a/README.md +++ b/README.md @@ -680,5 +680,8 @@ router := gin.Default() router.GET("/", handler) // [...] endless.ListenAndServe(":4242", router) - ``` + +An alternative to endless: + +* [manners](https://github.com/braintree/manners): A polite Go HTTP server that shuts down gracefully. From d1f7f35d1cb73da7897fe6af507072f2f6cdcb4e Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 14 Mar 2016 10:07:55 +0800 Subject: [PATCH 15/18] Add upload file example --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 56d9f6e7..4e4b19cc 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,32 @@ func main() { id: 1234; page: 1; name: manu; message: this_is_great ``` +### Another example: upload file + +Reference issue [#548](https://github.com/gin-gonic/gin/issues/548) + +```go +func main() { + router := gin.Default() + + router.POST("/upload", func(c *gin.Context) { + + file, header , err := c.Request.FormFile("upload") + filename := header.Filename + fmt.Println(header.Filename) + out, err := os.Create("./tmp/"+filename+".png") + if err != nil { + log.Fatal(err) + } + defer out.Close() + _, err = io.Copy(out, file) + if err != nil { + log.Fatal(err) + } + }) + router.Run(":8080") +} +``` #### Grouping routes ```go From 61729ed6fa70234c002a21c8bfbd89384b50de43 Mon Sep 17 00:00:00 2001 From: Christian Persson Date: Fri, 1 Apr 2016 09:57:00 +0200 Subject: [PATCH 16/18] Fix typos and improve wording --- README.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0ffe16de..47ebbed7 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ id: 1234; page: 1; name: manu; message: this_is_great ### Another example: upload file -Reference issue [#548](https://github.com/gin-gonic/gin/issues/548) +References issue [#548](https://github.com/gin-gonic/gin/issues/548). ```go func main() { @@ -300,7 +300,7 @@ func main() { // Authorization group // authorized := r.Group("/", AuthRequired()) - // exactly the same than: + // exactly the same as: authorized := r.Group("/") // per group middleware! in this case we use the custom created // AuthRequired() middleware just in the "authorized" group. @@ -645,7 +645,7 @@ func main() { // simulate a long task with time.Sleep(). 5 seconds time.Sleep(5 * time.Second) - // note than you are using the copied context "c_cp", IMPORTANT + // note that you are using the copied context "cCp", IMPORTANT log.Println("Done! in path " + cCp.Request.URL.Path) }() }) @@ -693,13 +693,9 @@ func main() { #### Graceful restart or stop Do you want to graceful restart or stop your web server? -There be some ways. +There are some ways this can be done. -We can using fvbock/endless to replace the default ListenAndServe - -Refer the issue for more details: - -https://github.com/gin-gonic/gin/issues/296 +We can use [fvbock/endless](https://github.com/fvbock/endless) to replace the default `ListenAndServe`. Refer issue [#296](https://github.com/gin-gonic/gin/issues/296) for more details. ```go router := gin.Default() From 70c104e16cc5ffcbc1eaff793e19f88ed9d07cc3 Mon Sep 17 00:00:00 2001 From: Kamron Batman Date: Fri, 1 Apr 2016 16:29:28 -0700 Subject: [PATCH 17/18] [Cleanup] Typo in context.go Fixes typo from 'If Parses' to 'It parses' --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index 5db121a5..b043d1b4 100644 --- a/context.go +++ b/context.go @@ -279,7 +279,7 @@ func (c *Context) GetPostForm(key string) (string, bool) { // "application/json" --> JSON binding // "application/xml" --> XML binding // otherwise --> returns an error -// If Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. +// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. // It decodes the json payload into the struct specified as a pointer. // Like ParseBody() but this method also writes a 400 error if the json is not valid. func (c *Context) Bind(obj interface{}) error { From 2fd607be4c31e33b91727bc43c21c96b2f385bd8 Mon Sep 17 00:00:00 2001 From: Miki Tebeka Date: Thu, 7 Apr 2016 07:33:28 +0300 Subject: [PATCH 18/18] Rename to conform with test files naming (closes #580) --- test_helpers.go => helpers_test.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test_helpers.go => helpers_test.go (100%) diff --git a/test_helpers.go b/helpers_test.go similarity index 100% rename from test_helpers.go rename to helpers_test.go