Commit Graph

184 Commits

Author SHA1 Message Date
guonaihong f98b339b77 support bind http header param #1956 (#1957)
* support bind http header param #1956

update #1956
```
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

type testHeader struct {
	Rate   int    `header:"Rate"`
	Domain string `header:"Domain"`
}

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		h := testHeader{}

		if err := c.ShouldBindHeader(&h); err != nil {
			c.JSON(200, err)
		}

		fmt.Printf("%#v\n", h)
		c.JSON(200, gin.H{"Rate": h.Rate, "Domain": h.Domain})
	})

	r.Run()

// client
// curl -H "rate:300" -H "domain:music" 127.0.0.1:8080/
// output
// {"Domain":"music","Rate":300}
}
```

* add unit test

* Modify the code to get the http header

When the http header is obtained in the standard library,
the key value will be modified by the CanonicalMIMEHeaderKey function,
and finally the value of the http header will be obtained from the map.
As follows.
```go
func (h MIMEHeader) Get(key string) string {
        // ...
         v := h[CanonicalMIMEHeaderKey(key)]
        // ...
}
```

This pr also follows this modification

* Thanks to vkd for suggestions, modifying code

* Increase test coverage

env GOPATH=`pwd` go test github.com/gin-gonic/gin/binding -coverprofile=cover.prof
ok  	github.com/gin-gonic/gin/binding	0.015s	coverage: 100.0% of statements

* Rollback check code

* add use case to README.md
2019-06-27 12:47:45 +08:00
田欧 73c4633943
use context instead of x/net/context (#1922) 2019-06-03 22:52:33 +08:00
Samuel Abreu 6e320c97e8 Fix context.Params race condition on Copy() (#1841)
* Fix context.Params race condition on Copy()

Using context.Param(key) on a context.Copy inside a goroutine
may lead to incorrect value on a high load, where another request
overwrite a Param

* Using waitgroup to wait asynchronous test case
2019-05-27 14:04:30 +08:00
itcloudy 0cbf290302 use encode replace json marshal increase json encoder speed (#1546) 2019-05-22 07:48:50 +08:00
Dan Markham 094f9a9105 v1.4.0 + #1631 (remove go1.6/go1,7 support) (#1851)
* remove go1.6 support

* remove build tag

* remove todo

* remove go1.6 support: https://github.com/gin-gonic/gin/pull/1383/commits

* update readme

* remove go1.7 support

* fix embedmd error

* test

* revert it

* revert it

* remove context_17

* add pusher test

* v1.4.0 rc1
2019-05-07 18:32:32 +08:00
Emmanuel Goh ccb9e90295 Extend context.File to allow for the content-dispositon attachments via a new method context.Attachment (#1260)
* Add FileAttachment method to context to allow instant downloads with filenames

* Add relevant tests for FileAttachment method
2019-03-01 10:17:47 +08:00
Equim 2dd3193006 Support negotiation wildcards, fix #391 (#1112)
* support negotiation wildcards, fix #391

* fix typo
2019-03-01 10:03:14 +08:00
Dmitry Kutakov 7dfa6c936a fix #1784: correct error comparison on tests (#1785) 2019-02-28 22:43:27 +08:00
Raphael Gavache e207a3ce65 Fix context.Copy() race condition (#1020)
* Fix context.Copy race condition

* Update githubapi_test.go

* fix code format
2019-02-26 15:10:16 +08:00
Luis GG 62749f0db4 Add context.HandlerNames() (#1729)
* Add context.HandlerNames()

This change adds a HandlerNames method that will return all registered handles in the context, in descending order

This is useful for debugging and troubleshooting purposes, especially in large apps

* Tests

Add tests for HandlerNames

* Fix HandlerNames test

* Simplify test
2019-02-26 12:15:40 +08:00
Dmitry Kutakov 4867ff9634 fix Context.Next() - recheck len of handlers every iteration (#1745)
* fix Context.Next() - recheck len of handlers every iteration

* add tests when Context.reset() can be called inside of handler

TestEngineHandleContext
TestContextResetInHandler
TestRouterStaticFSFileNotFound

* Context.Next() - format to while style
2019-01-18 09:57:06 +08:00
Dmitry Kutakov b056a34bdc fix errcheck warnings (#1739) 2019-01-18 09:32:53 +08:00
Bo-Yi Wu 85b92cdf4b
chore(testing): case sensitive for query string (#1720)
fix #1692
2018-12-29 11:46:26 +08:00
Gordon Tyler 8cb390f8fe Yaml binding (#1618)
* Add YAML binding for application/x-yaml.

* Add YAML binding methods to Context.

* Review fixes.

* Revert accidentally removed import.
2018-11-06 09:49:45 +08:00
Ismail Gjevori dbc330b804 Pass MaxMultipartMemory when FormFile is called (#1600)
When `gin.Context.FormFile("...")` is called the `engine.MaxMultipartMemory` is never used. This PR makes sure that the `MaxMultipartMemory` is passed and removes 2 calls to `http.Request.ParseForm` since they are called from `http.Request.ParseMultipartForm`
2018-10-22 23:01:14 +08:00
andriikushch 6ab50f944c replace deprecated HeaderMap with Header() (#1585) 2018-10-12 07:31:31 +08:00
Bo-Yi Wu 07f1bf0e63
feat: replace debug log with fmt package. (#1560) 2018-09-19 13:57:00 +08:00
Iskander (Alex) Sharipov 3f27866f80 simplify slice expressions: s[:] => s (#1541)
Found using https://go-critic.github.io/overview#unslice-ref
2018-09-12 21:21:26 +08:00
Filip Figiel c6110f970c Add PureJSON renderer (#694)
Closes #693
2018-08-20 15:15:31 +08:00
aljun efdd3c8b81 Add support for Protobuf format response and unit test (#1479)
`Gin` now have the `protobufBinding` function to check the request format, but didn't have a protobuf response function like `c.YAML()`.
In our company [ByteDance](http://bytedance.com/), the largest internet company using golang in China, we use `gin` to transfer __Protobuf__  instead of __Json__, we have to write some internal library to make some wrappers to achieve that, and the code is not elegant. So we really want such a feature.
2018-08-19 10:45:56 +08:00
syssam 40ab9de4b5 Add BindXML AND ShouldBindXML #1484 (#1485)
Add BindXML AND ShouldBindXML #1484
2018-08-17 09:12:15 +08:00
田欧 f45c928a15 chore: use http.Status* instead of hard code (#1482) 2018-08-14 09:51:56 +08:00
田欧 7e64d32269 Attempt to fix #1462 (#1463)
#1462
2018-08-12 10:12:33 +08:00
Dmitry Dorogin 9b7e7bdce6 Add tests for context.Stream (#1433) 2018-08-07 06:44:32 +08:00
田欧 647535cd9b Support map as query string or post form parameters (#1383)
* support query map

* add GetQueryMap and unittest

* support post-form map

* add readme for query map

* attempt to fix bug for post-form map when go version is 1.6

* remove duplicate code

* remove comment
2018-08-06 12:07:11 +08:00
solos 220e8d3453 return json if jsonp has not callback (#1438)
return json if jsonp has not callback
2018-07-21 00:52:55 +08:00
Rex Lee(李俊) 85221af84c add json ASCII string render (#1358)
add a json render that rendering json as ASCII string
2018-07-03 17:17:08 +08:00
Jean-Christophe Lebreton bf7803815b Serve easily dynamic files with `DataFromReader` context method (#1304)
* Add DataFromReader context method

* Replace fmt by strconv.FormatInt

* Add pull request link to README
2018-05-12 11:00:42 +08:00
JINNOUCHI Yasushi 995fa8e9ce Fix #216: Enable to call binding multiple times in some formats (#1341)
* Add interface to read body bytes in binding

* Add BindingBody implementation for some binding

* Fix to use `BindBodyBytesKey` for key

* Revert "Fix to use `BindBodyBytesKey` for key"

This reverts commit 2c82901cea.

* Use private-like key for body bytes

* Add tests for BindingBody & ShouldBindBodyWith

* Add note for README

* Remove redundant space between sentences
2018-05-11 10:33:33 +08:00
senhtry 8c24018290 Add Jsonp Support to Context (#1333) 2018-04-26 11:52:19 +08:00
田欧 9e895470dd add deprecated test case (#1176)
* add deprecated test case

* swap assert param

* remove
2017-11-29 16:42:51 +08:00
田欧 eeb57848ca update assert param(expect, actual) position (#1177) 2017-11-21 21:18:45 +08:00
Suhas Karanth dfb68ce085 feat(context): ShouldBind counterparts for Bind methods (#1047)
* feat(context): ShouldBind counterparts for Bind methods + tests

* docs(readme): Switch examples to use ShouldBind methods

Add section for bind methods types, explain difference in behavior.
Switch all `c.Bind` examples to use `c.ShouldBind`.
2017-10-23 11:14:09 +02:00
delphinus a8c53949e5 Support time location on form binding (#1117) 2017-09-28 22:23:18 +08:00
Eason Lin c19aa0598b feat(context): add BindQuery func (#1029)
* feat(context): add BindQuery func, only parse/bind the query string params.

* docs(readme): add BindQuery section.

* docs(readme): fix import.

* docs(readme): separate import
2017-07-19 09:50:05 +02:00
Eason Lin 93b3a0d7ec feat(context): add SaveUploadedFile func. (#1022)
* feat(context): add SaveUploadedFile func.

* feat(context): update multiple upload examples.

* style(example): fix gofmt

* fix(example): add missing return
2017-07-15 22:42:08 -05:00
Eason Lin 02a6f9b6bc chore(vendor): update json-iterator revison to fix TestRenderIndentedJSON failed (#1003)
* update json-iterator revison to fix TestRenderIndentedJSON failed

* fix(test): add space between key and value as same as standard JSON.

* fix(test): add space between key and value as same as standard JSON.
2017-07-11 00:59:03 -05:00
Bo-Yi Wu e23842ecab
fix json sort the map keys
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2017-07-08 18:19:09 +08:00
Bo-Yi Wu 08338eff82
fix testing
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2017-07-08 17:03:14 +08:00
Eason Lin 75ed286c60 feat: add SecureJSON func to prevent json hijacking 2017-07-08 01:21:30 +08:00
Javier Provecho Fernandez d875f07409 Merge branch 'master' into develop 2017-07-02 14:23:55 +02:00
Javier Provecho Fernandez 9a79e3f144 fix(import): switch sse import from gopkg to github 2017-06-27 23:17:02 +02:00
Bo-Yi Wu 1e1e4fc867 Add Makefile to check the following thing (#947)
* Add Makefile to check the following thing.

* vet check
* fmt check
* embedmd check
* misspell check

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* remove unused variable.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2017-06-12 21:50:42 -05:00
collinmsn 3f95933c3d Add method to return main handler (#930)
Fix #928 Method to get main handler is desired
2017-06-02 03:00:55 -05:00
Javier Provecho Fernandez 5eea51b6c9 feat(context): add cast helpers to c.Keys (#856)
* feat(context): add cast helpers to c.Keys

* Add tests for cast helpers to c.Keys
2017-06-01 20:00:04 -05:00
Eason Lin 8295db44ed Add content negotiation tests code coverage (#921) 2017-05-29 01:28:38 -05:00
Eason Lin 214a746b1d Improve cookie tests code coverage (#918) 2017-05-24 17:39:05 +08:00
Ammar Bandukwala 781cbd19f0
panic if err is nil on c.Error
A panic here provides a more informative stack trace than the panic which would otherwise occur while errors are being collected.
2017-05-09 22:30:56 -05:00
Bo-Yi Wu 41316b9ca9 feat: add GetRawData func. (#857)
* feat: add GetRawData func.

* update return style

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2017-03-31 08:45:56 +08:00
Bo-Yi Wu ad2dacedd6 feat: Support get value from request header. (#839) 2017-03-24 20:43:23 +08:00
Andrey Nering 863248034b Support time.Time on form binding (#801) 2017-02-17 21:32:36 +08:00
pjgg 6ce1e86a27 chore(errorHandler):new abortWithStatus method with Json body (#800) 2017-02-14 09:11:01 +08:00
Javier Provecho Fernandez 963acc4b0c Fix #198 (#781)
* Add new function to Render interface for writing content type only

* Add support for the new function in Render interface for writing content-type only

* Fix unhandled merge conflict in context_test.go

* Update vendor.json
2017-01-09 16:24:48 +01:00
Bo-Yi Wu 970e96e388 test: update client ip testing. 2017-01-03 23:42:21 +08:00
David Irvine ebe3580daf Add convenience method to check if websockets required (#779)
* Add convenience method to check if websockets required

* Add tests

* Fix up tests for develop branch
2017-01-02 16:05:30 +08:00
Bo-Yi Wu 5cc3d5955f Support upload single or multiple files.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-12-24 12:25:01 +08:00
Bo-Yi Wu 787bff85e5 fix testing.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-12-11 10:14:23 +08:00
Bo-Yi Wu 764e138e32 Merge pull request #755 from gin-gonic/755-app-engine-client-ip
Fix #723
2016-12-06 08:14:11 -06:00
Javier Provecho Fernandez 7e58c80a7c Fix #723 2016-12-06 14:28:01 +01:00
Javier Provecho Fernandez 273e43a27a Merge pull request #724 from chiffa-org/redundant_context_import
Move golang.org/x/net/context.Context interface implementation check to tests
2016-12-05 10:55:06 +01:00
Bo-Yi Wu e1efd4fce1 Merge pull request #707 from yehezkielbs/develop
Make CreateTestContext public without importing net/http/httptest
2016-12-05 16:48:30 +08:00
Bo-Yi Wu 6f474cb42d Merge pull request #570 from andreynering/array
Implement QueryArray and PostFormArray methods
2016-12-03 08:12:19 +08:00
Vyacheslav Dubinin ceb250ba20 Move golang.org/x/net/context.Context interface implementation check to tests 2016-10-19 17:13:38 +03:00
Yehezkiel Syamsuhadi 61ba9db5af Make CreateTestContext public without importing net/http/httptest 2016-09-21 12:16:51 +10:00
Javier Provecho Fernandez 9e930b9bdd lint code 2016-04-15 01:16:46 +02:00
Javier Provecho Fernandez 4df51ad4f8 Merge pull request #587 from roylou/develop
Write header immediately in AbortWithStatus(), close #585
2016-04-15 00:36:29 +02:00
Javier Provecho 007bd5124a closes #514, code from bobbo@b4f0b50 2016-04-14 23:47:49 +02:00
Roy Lou 4c4444b160 Write header immediately in AbortWithStatus()
Otherwise, caller needs to invoke WriteHeaderNow himself after
AbortWithStatus(), which is error-prone.

Also modified ErrorLoggerT() such that it always writes log to response
body. Otherwise calling AbortWithStatus() will fail to write body because
c.Writer.Written() is set true by WriteHeaderNow().
2016-04-15 00:02:29 +08:00
Andrey Nering 9366e33ffc Implement QueryArray and PostArray methods 2016-03-29 21:48:50 -03:00
Manu Mtz.-Almeida afc499f306 Adds GetQuery() and GetPostForm() APIs 2016-01-29 02:07:44 +01:00
Manu Mtz.-Almeida d64a1fb91c Cosmetic changes 2016-01-28 00:35:09 +01:00
Manu Mtz.-Almeida 97cd894279 c.Redirect() allows 201 status code 2016-01-28 00:34:05 +01:00
Manu Mtz.-Almeida d0065c712e Merge pull request #411 from phicode/vendoring
tests: make path assertions aware of vendoring
2016-01-27 03:22:48 +01:00
Javier Provecho Fernandez fe49f0b616 Fix exported test function 2015-10-02 12:39:25 +02:00
Javier Provecho Fernandez 4892650ef8 Merge branch 'se77en-master' into develop 2015-10-02 12:38:32 +02:00
Javier Provecho Fernandez 8e37eb8498 Fix tests for GetCookie() and SetCookie() 2015-10-02 12:37:51 +02:00
Javier Provecho Fernandez 45b72951af Merge branch 'master' of https://github.com/se77en/gin into se77en-master 2015-10-02 11:25:25 +02:00
Javier Provecho Fernandez ee820830d5 Merge branch 'danielalves-master' into develop 2015-10-02 10:26:23 +02:00
Javier Provecho Fernandez afbf1cf37d Merge branch 'master' of https://github.com/danielalves/gin into danielalves-master
Conflicts:
	context_test.go
2015-10-02 10:26:02 +02:00
Javier Provecho Fernandez 558d6b582f Merge branch 'fix' of https://github.com/donnpebe/gin into donnpebe-fix 2015-10-02 09:54:06 +02:00
Javier Provecho Fernandez 8553030656 Remove old test 2015-09-25 12:12:06 +02:00
Javier Provecho Fernandez 6db7a17513 Trying a new fix. 2015-09-25 12:04:55 +02:00
Javier Provecho Fernandez aa934766b4 Fix TestContextRenderSSE-2 in context_test.go 2015-09-25 11:43:07 +02:00
Damon Zhao 7bf9788326 fix ci 2015-08-27 16:16:16 +08:00
Damon Zhao f5b1fb44bb Add SetCookie and GetCookie method for Context 2015-08-27 16:04:50 +08:00
Philipp Meinen 9fd8aff56e tests: make path assertions aware of vendoring
The path of a package can change in a situation where
dependency vendoring is in use.
This change modifies gin's unit tests to allow such paths.
2015-08-23 00:13:41 +02:00
Manu Mtz-Almeida a97c239b7a fixes unit tests 2015-08-16 16:36:47 +02:00
Manu Mtz-Almeida 0873992f38 More unit tests for form binding 2015-07-08 04:26:37 +02:00
Manu Mtz-Almeida 0316b735c4 More unit tests 2015-07-03 04:20:18 +02:00
Manu Mtz-Almeida 8f3047814e Comments + IRoutes + IRouter + unexported AbortIndex 2015-07-02 20:24:54 +02:00
Manu Mtz-Almeida 13f57702d4 Adds more c.Next() just to be sure 2015-07-02 18:45:09 +02:00
Adam Dratwinski 050a55b006 Use c.Next() instead of c.index++ in UnitTest 2015-07-02 16:37:35 +02:00
Adam Dratwinski 74f5051cb5 Fix IsAborted() method 2015-07-02 13:37:11 +02:00
Donn Pebe c1719f7e20 Use only the ip part of request RemoteAddr 2015-07-02 01:48:21 +07:00
Manu Mtz-Almeida 95c08d5f84 Adds HandlerName() 2015-06-25 19:44:52 +02:00
Manu Mtz-Almeida a7c957af7d Adds supports for custom JSON Content-type 2015-06-13 04:29:10 +02:00
Manu Mtz-Almeida 70325deb98 c.ClientIP() performance improvement
benchmark                 old ns/op     new ns/op     delta
BenchmarkManyHandlers     4956          4463          -9.95%

benchmark                 old allocs     new allocs     delta
BenchmarkManyHandlers     16             13             -18.75%

benchmark                 old bytes     new bytes     delta
BenchmarkManyHandlers     256           216           -15.62%
2015-06-04 13:15:22 +02:00
danielalves f831ac80ac Exporting CreateTestContext 2015-06-02 17:16:51 -03:00
Manu Mtz-Almeida 766493c916 Fixes all unit tests 2015-05-31 22:35:49 +02:00
Manu Mtz-Almeida a843d320a5 Better unit tests for binding forms 2015-05-26 17:22:39 +02:00