mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-19 07:38:57 +08:00
Merge branch 'master' into master
This commit is contained in:
commit
9f94b4f513
46
.github/ISSUE_TEMPLATE.md
vendored
46
.github/ISSUE_TEMPLATE.md
vendored
@ -3,11 +3,47 @@
|
||||
- Please provide source code and commit sha if you found a bug.
|
||||
- Review existing issues and provide feedback or react to them.
|
||||
|
||||
## Description
|
||||
|
||||
<!-- Description of a problem -->
|
||||
|
||||
## How to reproduce
|
||||
|
||||
<!-- The smallest possible code example to show the problem that can be compiled, like -->
|
||||
```
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g := gin.Default()
|
||||
g.GET("/hello/:name", func(c *gin.Context) {
|
||||
c.String(200, "Hello %s", c.Param("name"))
|
||||
})
|
||||
g.Run(":9000")
|
||||
}
|
||||
```
|
||||
|
||||
## Expectations
|
||||
|
||||
<!-- Your expectation result of 'curl' command, like -->
|
||||
```
|
||||
$ curl http://localhost:8201/hello/world
|
||||
Hello world
|
||||
```
|
||||
|
||||
## Actual result
|
||||
|
||||
<!-- Actual result showing the problem -->
|
||||
```
|
||||
$ curl -i http://localhost:8201/hello/world
|
||||
<YOUR RESULT>
|
||||
```
|
||||
|
||||
## Environment
|
||||
|
||||
- go version:
|
||||
- gin version (or commit ref):
|
||||
- operating system:
|
||||
|
||||
## Description
|
||||
|
||||
## Screenshots
|
||||
|
||||
|
@ -3,14 +3,12 @@ language: go
|
||||
matrix:
|
||||
fast_finish: true
|
||||
include:
|
||||
- go: 1.10.x
|
||||
- go: 1.11.x
|
||||
env: GO111MODULE=on
|
||||
- go: 1.12.x
|
||||
env: GO111MODULE=on
|
||||
- go: 1.13.x
|
||||
- go: master
|
||||
env: GO111MODULE=on
|
||||
|
||||
git:
|
||||
depth: 10
|
||||
|
44
CHANGELOG.md
44
CHANGELOG.md
@ -1,5 +1,39 @@
|
||||
### Gin v1.5.0
|
||||
|
||||
### Gin 1.4.0
|
||||
- [FIX] Use DefaultWriter and DefaultErrorWriter for debug messages [#1891](https://github.com/gin-gonic/gin/pull/1891)
|
||||
- [NEW] Now you can parse the inline lowercase start structure [#1893](https://github.com/gin-gonic/gin/pull/1893)
|
||||
- [FIX] Some code improvements [#1909](https://github.com/gin-gonic/gin/pull/1909)
|
||||
- [FIX] Use encode replace json marshal increase json encoder speed [#1546](https://github.com/gin-gonic/gin/pull/1546)
|
||||
- [NEW] Hold matched route full path in the Context [#1826](https://github.com/gin-gonic/gin/pull/1826)
|
||||
- [FIX] Fix context.Params race condition on Copy() [#1841](https://github.com/gin-gonic/gin/pull/1841)
|
||||
- [NEW] Add context param query cache [#1450](https://github.com/gin-gonic/gin/pull/1450)
|
||||
- [FIX] Improve GetQueryMap performance [#1918](https://github.com/gin-gonic/gin/pull/1918)
|
||||
- [FIX] Improve get post data [#1920](https://github.com/gin-gonic/gin/pull/1920)
|
||||
- [FIX] Use context instead of x/net/context [#1922](https://github.com/gin-gonic/gin/pull/1922)
|
||||
- [FIX] Attempt to fix PostForm cache bug [#1931](https://github.com/gin-gonic/gin/pull/1931)
|
||||
- [NEW] Add support of multipart multi files [#1949](https://github.com/gin-gonic/gin/pull/1949)
|
||||
- [NEW] Support bind http header param [#1957](https://github.com/gin-gonic/gin/pull/1957)
|
||||
- [FIX] Drop support for go1.8 and go1.9 [#1933](https://github.com/gin-gonic/gin/pull/1933)
|
||||
- [FIX] Bugfix for the FullPath feature [#1919](https://github.com/gin-gonic/gin/pull/1919)
|
||||
- [FIX] Gin1.5 bytes.Buffer to strings.Builder [#1939](https://github.com/gin-gonic/gin/pull/1939)
|
||||
- [FIX] Upgrade github.com/ugorji/go/codec [#1969](https://github.com/gin-gonic/gin/pull/1969)
|
||||
- [NEW] Support bind unix time [#1980](https://github.com/gin-gonic/gin/pull/1980)
|
||||
- [FIX] Simplify code [#2004](https://github.com/gin-gonic/gin/pull/2004)
|
||||
- [NEW] Support negative Content-Length in DataFromReader [#1981](https://github.com/gin-gonic/gin/pull/1981)
|
||||
- [FIX] Identify terminal on a RISC-V architecture for auto-colored logs [#2019](https://github.com/gin-gonic/gin/pull/2019)
|
||||
- [BREAKING] `Context.JSONP()` now expects a semicolon (`;`) at the end [#2007](https://github.com/gin-gonic/gin/pull/2007)
|
||||
- [BREAKING] Upgrade default `binding.Validator` to v9 (see [its changelog](https://github.com/go-playground/validator/releases/tag/v9.0.0)) [#1015](https://github.com/gin-gonic/gin/pull/1015)
|
||||
- [NEW] Add `DisallowUnknownFields()` in `Context.BindJSON()` [#2028](https://github.com/gin-gonic/gin/pull/2028)
|
||||
- [NEW] Use specific `net.Listener` with `Engine.RunListener()` [#2023](https://github.com/gin-gonic/gin/pull/2023)
|
||||
- [FIX] Fix some typo [#2079](https://github.com/gin-gonic/gin/pull/2079) [#2080](https://github.com/gin-gonic/gin/pull/2080)
|
||||
- [FIX] Relocate binding body tests [#2086](https://github.com/gin-gonic/gin/pull/2086)
|
||||
- [FIX] Use Writer in Context.Status [#1606](https://github.com/gin-gonic/gin/pull/1606)
|
||||
- [FIX] `Engine.RunUnix()` now returns the error if it can't change the file mode [#2093](https://github.com/gin-gonic/gin/pull/2093)
|
||||
- [FIX] `RouterGroup.StaticFS()` leaked files. Now it closes them. [#2118](https://github.com/gin-gonic/gin/pull/2118)
|
||||
- [FIX] `Context.Request.FormFile` leaked file. Now it closes it. [#2114](https://github.com/gin-gonic/gin/pull/2114)
|
||||
- [FIX] Ignore walking on `form:"-"` mapping [#1943](https://github.com/gin-gonic/gin/pull/1943)
|
||||
|
||||
### Gin v1.4.0
|
||||
|
||||
- [NEW] Support for [Go Modules](https://github.com/golang/go/wiki/Modules) [#1569](https://github.com/gin-gonic/gin/pull/1569)
|
||||
- [NEW] Refactor of form mapping multipart request [#1829](https://github.com/gin-gonic/gin/pull/1829)
|
||||
@ -15,7 +49,7 @@
|
||||
- [NEW] Refactor form mappings [#1749](https://github.com/gin-gonic/gin/pull/1749)
|
||||
- [NEW] Added flag to context.Stream indicates if client disconnected in middle of stream [#1252](https://github.com/gin-gonic/gin/pull/1252)
|
||||
- [FIX] Moved [examples](https://github.com/gin-gonic/examples) to stand alone Repo [#1775](https://github.com/gin-gonic/gin/pull/1775)
|
||||
- [NEW] Extend context.File to allow for the content-dispositon attachments via a new method context.Attachment [#1260](https://github.com/gin-gonic/gin/pull/1260)
|
||||
- [NEW] Extend context.File to allow for the content-disposition attachments via a new method context.Attachment [#1260](https://github.com/gin-gonic/gin/pull/1260)
|
||||
- [FIX] Support HTTP content negotiation wildcards [#1112](https://github.com/gin-gonic/gin/pull/1112)
|
||||
- [NEW] Add prefix from X-Forwarded-Prefix in redirectTrailingSlash [#1238](https://github.com/gin-gonic/gin/pull/1238)
|
||||
- [FIX] context.Copy() race condition [#1020](https://github.com/gin-gonic/gin/pull/1020)
|
||||
@ -56,7 +90,7 @@
|
||||
- [NEW] Upgrade dependency libraries [#1491](https://github.com/gin-gonic/gin/pull/1491)
|
||||
|
||||
|
||||
### Gin 1.3.0
|
||||
### Gin v1.3.0
|
||||
|
||||
- [NEW] Add [`func (*Context) QueryMap`](https://godoc.org/github.com/gin-gonic/gin#Context.QueryMap), [`func (*Context) GetQueryMap`](https://godoc.org/github.com/gin-gonic/gin#Context.GetQueryMap), [`func (*Context) PostFormMap`](https://godoc.org/github.com/gin-gonic/gin#Context.PostFormMap) and [`func (*Context) GetPostFormMap`](https://godoc.org/github.com/gin-gonic/gin#Context.GetPostFormMap) to support `type map[string]string` as query string or form parameters, see [#1383](https://github.com/gin-gonic/gin/pull/1383)
|
||||
- [NEW] Add [`func (*Context) AsciiJSON`](https://godoc.org/github.com/gin-gonic/gin#Context.AsciiJSON), see [#1358](https://github.com/gin-gonic/gin/pull/1358)
|
||||
@ -231,7 +265,7 @@
|
||||
- [PERFORMANCE] Improve context's memory locality, reduce CPU cache faults.
|
||||
- [NEW] Flexible rendering API
|
||||
- [NEW] Add Context.File()
|
||||
- [NEW] Add shorcut RunTLS() for http.ListenAndServeTLS
|
||||
- [NEW] Add shortcut RunTLS() for http.ListenAndServeTLS
|
||||
- [FIX] Rename NotFound404() to NoRoute()
|
||||
- [FIX] Errors in context are purged
|
||||
- [FIX] Adds HEAD method in Static file serving
|
||||
@ -254,7 +288,7 @@
|
||||
- [NEW] New Bind() and BindWith() methods for parsing request body.
|
||||
- [NEW] Add Content.Copy()
|
||||
- [NEW] Add context.LastError()
|
||||
- [NEW] Add shorcut for OPTIONS HTTP method
|
||||
- [NEW] Add shortcut for OPTIONS HTTP method
|
||||
- [FIX] Tons of README fixes
|
||||
- [FIX] Header is written before body
|
||||
- [FIX] BasicAuth() and changes API a little bit
|
||||
|
29
README.md
29
README.md
@ -11,7 +11,7 @@
|
||||
[](https://www.codetriage.com/gin-gonic/gin)
|
||||
[](https://github.com/gin-gonic/gin/releases)
|
||||
|
||||
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.
|
||||
Gin is a web framework written in Go (Golang). It features a martini-like API with performance that is up to 40 times faster thanks to [httprouter](https://github.com/julienschmidt/httprouter). If you need performance and good productivity, you will love Gin.
|
||||
|
||||
|
||||
## Contents
|
||||
@ -70,7 +70,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
|
||||
|
||||
To install Gin package, you need to install Go and set your Go workspace first.
|
||||
|
||||
1. The first need [Go](https://golang.org/) installed (**version 1.10+ is required**), then you can use the below Go command to install Gin.
|
||||
1. The first need [Go](https://golang.org/) installed (**version 1.11+ is required**), then you can use the below Go command to install Gin.
|
||||
|
||||
```sh
|
||||
$ go get -u github.com/gin-gonic/gin
|
||||
@ -101,6 +101,12 @@ $ go get github.com/kardianos/govendor
|
||||
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
|
||||
```
|
||||
|
||||
If you are on a Mac and you're installing Go 1.8 (released: Feb 2017) or later, GOPATH is automatically determined by the Go toolchain for you. It defaults to $HOME/go on macOS so you can create your project like this
|
||||
|
||||
```sh
|
||||
$ mkdir -p $HOME/go/src/github.com/myusername/project && cd "$_"
|
||||
```
|
||||
|
||||
3. Vendor init your project and add gin
|
||||
|
||||
```sh
|
||||
@ -139,12 +145,12 @@ func main() {
|
||||
"message": "pong",
|
||||
})
|
||||
})
|
||||
r.Run() // listen and serve on 0.0.0.0:8080
|
||||
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
# run example.go and visit 0.0.0.0:8080/ping on browser
|
||||
# run example.go and visit 0.0.0.0:8080/ping (for windows "localhost:8080/ping") on browser
|
||||
$ go run example.go
|
||||
```
|
||||
|
||||
@ -1143,7 +1149,7 @@ func main() {
|
||||
|
||||
#### AsciiJSON
|
||||
|
||||
Using AsciiJSON to Generates ASCII-only JSON with escaped non-ASCII chracters.
|
||||
Using AsciiJSON to Generates ASCII-only JSON with escaped non-ASCII characters.
|
||||
|
||||
```go
|
||||
func main() {
|
||||
@ -1672,11 +1678,19 @@ func main() {
|
||||
}
|
||||
|
||||
g.Go(func() error {
|
||||
return server01.ListenAndServe()
|
||||
err := server01.ListenAndServe()
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
g.Go(func() error {
|
||||
return server02.ListenAndServe()
|
||||
err := server02.ListenAndServe()
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
if err := g.Wait(); err != nil {
|
||||
@ -1793,6 +1807,7 @@ func main() {
|
||||
func loadTemplate() (*template.Template, error) {
|
||||
t := template.New("")
|
||||
for name, file := range Assets.Files {
|
||||
defer file.Close()
|
||||
if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {
|
||||
continue
|
||||
}
|
||||
|
@ -1,72 +0,0 @@
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin/testdata/protoexample"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
func TestBindingBody(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
binding BindingBody
|
||||
body string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "JSON binding",
|
||||
binding: JSON,
|
||||
body: `{"foo":"FOO"}`,
|
||||
},
|
||||
{
|
||||
name: "XML binding",
|
||||
binding: XML,
|
||||
body: `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root>
|
||||
<foo>FOO</foo>
|
||||
</root>`,
|
||||
},
|
||||
{
|
||||
name: "MsgPack binding",
|
||||
binding: MsgPack,
|
||||
body: msgPackBody(t),
|
||||
},
|
||||
{
|
||||
name: "YAML binding",
|
||||
binding: YAML,
|
||||
body: `foo: FOO`,
|
||||
},
|
||||
} {
|
||||
t.Logf("testing: %s", tt.name)
|
||||
req := requestWithBody("POST", "/", tt.body)
|
||||
form := FooStruct{}
|
||||
body, _ := ioutil.ReadAll(req.Body)
|
||||
assert.NoError(t, tt.binding.BindBody(body, &form))
|
||||
assert.Equal(t, FooStruct{"FOO"}, form)
|
||||
}
|
||||
}
|
||||
|
||||
func msgPackBody(t *testing.T) string {
|
||||
test := FooStruct{"FOO"}
|
||||
h := new(codec.MsgpackHandle)
|
||||
buf := bytes.NewBuffer(nil)
|
||||
assert.NoError(t, codec.NewEncoder(buf, h).Encode(test))
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestBindingBodyProto(t *testing.T) {
|
||||
test := protoexample.Test{
|
||||
Label: proto.String("FOO"),
|
||||
}
|
||||
data, _ := proto.Marshal(&test)
|
||||
req := requestWithBody("POST", "/", string(data))
|
||||
form := protoexample.Test{}
|
||||
body, _ := ioutil.ReadAll(req.Body)
|
||||
assert.NoError(t, ProtoBuf.BindBody(body, &form))
|
||||
assert.Equal(t, test, form)
|
||||
}
|
@ -64,6 +64,10 @@ type FooStructUseNumber struct {
|
||||
Foo interface{} `json:"foo" binding:"required"`
|
||||
}
|
||||
|
||||
type FooStructDisallowUnknownFields struct {
|
||||
Foo interface{} `json:"foo" binding:"required"`
|
||||
}
|
||||
|
||||
type FooBarStructForTimeType struct {
|
||||
TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_utc:"1" time_location:"Asia/Chongqing"`
|
||||
TimeBar time.Time `form:"time_bar" time_format:"2006-01-02" time_utc:"1"`
|
||||
@ -194,6 +198,12 @@ func TestBindingJSONUseNumber2(t *testing.T) {
|
||||
`{"foo": 123}`, `{"bar": "foo"}`)
|
||||
}
|
||||
|
||||
func TestBindingJSONDisallowUnknownFields(t *testing.T) {
|
||||
testBodyBindingDisallowUnknownFields(t, JSON,
|
||||
"/", "/",
|
||||
`{"foo": "bar"}`, `{"foo": "bar", "what": "this"}`)
|
||||
}
|
||||
|
||||
func TestBindingForm(t *testing.T) {
|
||||
testFormBinding(t, "POST",
|
||||
"/", "/",
|
||||
@ -431,7 +441,8 @@ func createFormFilesMultipartRequest(t *testing.T) *http.Request {
|
||||
defer f.Close()
|
||||
fw, err1 := mw.CreateFormFile("file", "form.go")
|
||||
assert.NoError(t, err1)
|
||||
io.Copy(fw, f)
|
||||
_, err = io.Copy(fw, f)
|
||||
assert.NoError(t, err)
|
||||
|
||||
req, err2 := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
|
||||
assert.NoError(t, err2)
|
||||
@ -455,7 +466,8 @@ func createFormFilesMultipartRequestFail(t *testing.T) *http.Request {
|
||||
defer f.Close()
|
||||
fw, err1 := mw.CreateFormFile("file_foo", "form_foo.go")
|
||||
assert.NoError(t, err1)
|
||||
io.Copy(fw, f)
|
||||
_, err = io.Copy(fw, f)
|
||||
assert.NoError(t, err)
|
||||
|
||||
req, err2 := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", body)
|
||||
assert.NoError(t, err2)
|
||||
@ -544,7 +556,8 @@ func TestBindingFormPostForMapFail(t *testing.T) {
|
||||
func TestBindingFormFilesMultipart(t *testing.T) {
|
||||
req := createFormFilesMultipartRequest(t)
|
||||
var obj FooBarFileStruct
|
||||
FormMultipart.Bind(req, &obj)
|
||||
err := FormMultipart.Bind(req, &obj)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// file from os
|
||||
f, _ := os.Open("form.go")
|
||||
@ -658,9 +671,9 @@ func TestValidationDisabled(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestExistsSucceeds(t *testing.T) {
|
||||
func TestRequiredSucceeds(t *testing.T) {
|
||||
type HogeStruct struct {
|
||||
Hoge *int `json:"hoge" binding:"exists"`
|
||||
Hoge *int `json:"hoge" binding:"required"`
|
||||
}
|
||||
|
||||
var obj HogeStruct
|
||||
@ -669,9 +682,9 @@ func TestExistsSucceeds(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestExistsFails(t *testing.T) {
|
||||
func TestRequiredFails(t *testing.T) {
|
||||
type HogeStruct struct {
|
||||
Hoge *int `json:"foo" binding:"exists"`
|
||||
Hoge *int `json:"foo" binding:"required"`
|
||||
}
|
||||
|
||||
var obj HogeStruct
|
||||
@ -1162,6 +1175,25 @@ func testBodyBindingUseNumber2(t *testing.T, b Binding, name, path, badPath, bod
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func testBodyBindingDisallowUnknownFields(t *testing.T, b Binding, path, badPath, body, badBody string) {
|
||||
EnableDecoderDisallowUnknownFields = true
|
||||
defer func() {
|
||||
EnableDecoderDisallowUnknownFields = false
|
||||
}()
|
||||
|
||||
obj := FooStructDisallowUnknownFields{}
|
||||
req := requestWithBody("POST", path, body)
|
||||
err := b.Bind(req, &obj)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "bar", obj.Foo)
|
||||
|
||||
obj = FooStructDisallowUnknownFields{}
|
||||
req = requestWithBody("POST", badPath, badBody)
|
||||
err = JSON.Bind(req, &obj)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "what")
|
||||
}
|
||||
|
||||
func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
||||
assert.Equal(t, name, b.Name())
|
||||
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/go-playground/validator.v8"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
)
|
||||
|
||||
type defaultValidator struct {
|
||||
@ -45,7 +45,7 @@ func (v *defaultValidator) Engine() interface{} {
|
||||
|
||||
func (v *defaultValidator) lazyinit() {
|
||||
v.once.Do(func() {
|
||||
config := &validator.Config{TagName: "binding"}
|
||||
v.validate = validator.New(config)
|
||||
v.validate = validator.New()
|
||||
v.validate.SetTagName("binding")
|
||||
})
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
"github.com/gin-gonic/gin/internal/json"
|
||||
)
|
||||
|
||||
var errUnknownType = errors.New("Unknown type")
|
||||
var errUnknownType = errors.New("unknown type")
|
||||
|
||||
func mapUri(ptr interface{}, m map[string][]string) error {
|
||||
return mapFormByTag(ptr, m, "uri")
|
||||
@ -51,6 +51,10 @@ func mappingByPtr(ptr interface{}, setter setter, tag string) error {
|
||||
}
|
||||
|
||||
func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {
|
||||
if field.Tag.Get(tag) == "-" { // just ignoring this field
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var vKind = value.Kind()
|
||||
|
||||
if vKind == reflect.Ptr {
|
||||
@ -112,9 +116,6 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter
|
||||
tagValue = field.Tag.Get(tag)
|
||||
tagValue, opts := head(tagValue, ",")
|
||||
|
||||
if tagValue == "-" { // just ignoring this field
|
||||
return false, nil
|
||||
}
|
||||
if tagValue == "" { // default value is FieldName
|
||||
tagValue = field.Name
|
||||
}
|
||||
|
@ -32,7 +32,10 @@ type structFull struct {
|
||||
func BenchmarkMapFormFull(b *testing.B) {
|
||||
var s structFull
|
||||
for i := 0; i < b.N; i++ {
|
||||
mapForm(&s, form)
|
||||
err := mapForm(&s, form)
|
||||
if err != nil {
|
||||
b.Fatalf("Error on a form mapping")
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
|
||||
@ -52,7 +55,10 @@ type structName struct {
|
||||
func BenchmarkMapFormName(b *testing.B) {
|
||||
var s structName
|
||||
for i := 0; i < b.N; i++ {
|
||||
mapForm(&s, form)
|
||||
err := mapForm(&s, form)
|
||||
if err != nil {
|
||||
b.Fatalf("Error on a form mapping")
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
|
||||
|
@ -269,3 +269,13 @@ func TestMappingMapField(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, map[string]int{"one": 1}, s.M)
|
||||
}
|
||||
|
||||
func TestMappingIgnoredCircularRef(t *testing.T) {
|
||||
type S struct {
|
||||
S *S `form:"-"`
|
||||
}
|
||||
var s S
|
||||
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
@ -18,6 +18,12 @@ import (
|
||||
// interface{} as a Number instead of as a float64.
|
||||
var EnableDecoderUseNumber = false
|
||||
|
||||
// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
|
||||
// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
|
||||
// return an error when the destination is a struct and the input contains object
|
||||
// keys which do not match any non-ignored, exported fields in the destination.
|
||||
var EnableDecoderDisallowUnknownFields = false
|
||||
|
||||
type jsonBinding struct{}
|
||||
|
||||
func (jsonBinding) Name() string {
|
||||
@ -40,6 +46,9 @@ func decodeJSON(r io.Reader, obj interface{}) error {
|
||||
if EnableDecoderUseNumber {
|
||||
decoder.UseNumber()
|
||||
}
|
||||
if EnableDecoderDisallowUnknownFields {
|
||||
decoder.DisallowUnknownFields()
|
||||
}
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
return err
|
||||
}
|
||||
|
21
binding/json_test.go
Normal file
21
binding/json_test.go
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestJSONBindingBindBody(t *testing.T) {
|
||||
var s struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO"}`), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
32
binding/msgpack_test.go
Normal file
32
binding/msgpack_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
func TestMsgpackBindingBindBody(t *testing.T) {
|
||||
type teststruct struct {
|
||||
Foo string `msgpack:"foo"`
|
||||
}
|
||||
var s teststruct
|
||||
err := msgpackBinding{}.BindBody(msgpackBody(t, teststruct{"FOO"}), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
||||
|
||||
func msgpackBody(t *testing.T, obj interface{}) []byte {
|
||||
var bs bytes.Buffer
|
||||
h := &codec.MsgpackHandle{}
|
||||
err := codec.NewEncoder(&bs, h).Encode(obj)
|
||||
require.NoError(t, err)
|
||||
return bs.Bytes()
|
||||
}
|
@ -6,12 +6,11 @@ package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/go-playground/validator.v8"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
)
|
||||
|
||||
type testInterface interface {
|
||||
@ -200,15 +199,8 @@ type structCustomValidation struct {
|
||||
Integer int `binding:"notone"`
|
||||
}
|
||||
|
||||
// notOne is a custom validator meant to be used with `validator.v8` library.
|
||||
// The method signature for `v9` is significantly different and this function
|
||||
// would need to be changed for tests to pass after upgrade.
|
||||
// See https://github.com/gin-gonic/gin/pull/1015.
|
||||
func notOne(
|
||||
v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
|
||||
field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
|
||||
) bool {
|
||||
if val, ok := field.Interface().(int); ok {
|
||||
func notOne(f1 validator.FieldLevel) bool {
|
||||
if val, ok := f1.Field().Interface().(int); ok {
|
||||
return val != 1
|
||||
}
|
||||
return false
|
||||
|
25
binding/xml_test.go
Normal file
25
binding/xml_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestXMLBindingBindBody(t *testing.T) {
|
||||
var s struct {
|
||||
Foo string `xml:"foo"`
|
||||
}
|
||||
xmlBody := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root>
|
||||
<foo>FOO</foo>
|
||||
</root>`
|
||||
err := xmlBinding{}.BindBody([]byte(xmlBody), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
21
binding/yaml_test.go
Normal file
21
binding/yaml_test.go
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestYAMLBindingBindBody(t *testing.T) {
|
||||
var s struct {
|
||||
Foo string `yaml:"foo"`
|
||||
}
|
||||
err := yamlBinding{}.BindBody([]byte("foo: FOO"), &s)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "FOO", s.Foo)
|
||||
}
|
20
context.go
20
context.go
@ -393,8 +393,7 @@ func (c *Context) QueryArray(key string) []string {
|
||||
|
||||
func (c *Context) getQueryCache() {
|
||||
if c.queryCache == nil {
|
||||
c.queryCache = make(url.Values)
|
||||
c.queryCache, _ = url.ParseQuery(c.Request.URL.RawQuery)
|
||||
c.queryCache = c.Request.URL.Query()
|
||||
}
|
||||
}
|
||||
|
||||
@ -491,13 +490,8 @@ func (c *Context) PostFormMap(key string) map[string]string {
|
||||
// GetPostFormMap returns a map for a given form key, plus a boolean value
|
||||
// whether at least one value exists for the given key.
|
||||
func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {
|
||||
req := c.Request
|
||||
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
|
||||
if err != http.ErrNotMultipart {
|
||||
debugPrint("error on parse multipart form map: %v", err)
|
||||
}
|
||||
}
|
||||
return c.get(req.PostForm, key)
|
||||
c.getFormCache()
|
||||
return c.get(c.formCache, key)
|
||||
}
|
||||
|
||||
// get is an internal method and returns a map which satisfy conditions.
|
||||
@ -522,7 +516,11 @@ func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
_, fh, err := c.Request.FormFile(name)
|
||||
f, fh, err := c.Request.FormFile(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.Close()
|
||||
return fh, err
|
||||
}
|
||||
|
||||
@ -750,7 +748,7 @@ func bodyAllowedForStatus(status int) bool {
|
||||
|
||||
// Status sets the HTTP response code.
|
||||
func (c *Context) Status(code int) {
|
||||
c.writermem.WriteHeader(code)
|
||||
c.Writer.WriteHeader(code)
|
||||
}
|
||||
|
||||
// Header is a intelligent shortcut for c.Writer.Header().Set(key, value).
|
||||
|
@ -1799,6 +1799,23 @@ func TestContextRenderDataFromReader(t *testing.T) {
|
||||
assert.Equal(t, extraHeaders["Content-Disposition"], w.Header().Get("Content-Disposition"))
|
||||
}
|
||||
|
||||
func TestContextRenderDataFromReaderNoHeaders(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
body := "#!PNG some raw data"
|
||||
reader := strings.NewReader(body)
|
||||
contentLength := int64(len(body))
|
||||
contentType := "image/png"
|
||||
|
||||
c.DataFromReader(http.StatusOK, contentLength, contentType, reader, nil)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, body, w.Body.String())
|
||||
assert.Equal(t, contentType, w.Header().Get("Content-Type"))
|
||||
assert.Equal(t, fmt.Sprintf("%d", contentLength), w.Header().Get("Content-Length"))
|
||||
}
|
||||
|
||||
type TestResponseRecorder struct {
|
||||
*httptest.ResponseRecorder
|
||||
closeChannel chan bool
|
||||
|
2
debug.go
2
debug.go
@ -67,7 +67,7 @@ func getMinVer(v string) (uint64, error) {
|
||||
|
||||
func debugPrintWARNINGDefault() {
|
||||
if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer {
|
||||
debugPrint(`[WARNING] Now Gin requires Go 1.10 or later and Go 1.11 will be required soon.
|
||||
debugPrint(`[WARNING] Now Gin requires Go 1.11 or later and Go 1.12 will be required soon.
|
||||
|
||||
`)
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ func TestDebugPrintWARNINGDefault(t *testing.T) {
|
||||
})
|
||||
m, e := getMinVer(runtime.Version())
|
||||
if e == nil && m <= ginSupportMinGoVer {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.10 or later and Go 1.11 will be required soon.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.11 or later and Go 1.12 will be required soon.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
} else {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
|
16
gin.go
16
gin.go
@ -30,7 +30,7 @@ type HandlerFunc func(*Context)
|
||||
// HandlersChain defines a HandlerFunc array.
|
||||
type HandlersChain []HandlerFunc
|
||||
|
||||
// Last returns the last handler in the chain. ie. the last handler is the main own.
|
||||
// Last returns the last handler in the chain. ie. the last handler is the main one.
|
||||
func (c HandlersChain) Last() HandlerFunc {
|
||||
if length := len(c); length > 0 {
|
||||
return c[length-1]
|
||||
@ -320,7 +320,10 @@ func (engine *Engine) RunUnix(file string) (err error) {
|
||||
return
|
||||
}
|
||||
defer listener.Close()
|
||||
os.Chmod(file, 0777)
|
||||
err = os.Chmod(file, 0777)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = http.Serve(listener, engine)
|
||||
return
|
||||
}
|
||||
@ -338,6 +341,15 @@ func (engine *Engine) RunFd(fd int) (err error) {
|
||||
return
|
||||
}
|
||||
defer listener.Close()
|
||||
err = engine.RunListener(listener)
|
||||
return
|
||||
}
|
||||
|
||||
// RunListener attaches the router to a http.Server and starts listening and serving HTTP requests
|
||||
// through the specified net.Listener
|
||||
func (engine *Engine) RunListener(listener net.Listener) (err error) {
|
||||
debugPrint("Listening and serving HTTP on listener what's bind with address@%s", listener.Addr())
|
||||
defer func() { debugPrintError(err) }()
|
||||
err = http.Serve(listener, engine)
|
||||
return
|
||||
}
|
||||
|
@ -90,7 +90,8 @@ func TestPusher(t *testing.T) {
|
||||
go func() {
|
||||
router.GET("/pusher", func(c *Context) {
|
||||
if pusher := c.Writer.Pusher(); pusher != nil {
|
||||
pusher.Push("/assets/app.js", nil)
|
||||
err := pusher.Push("/assets/app.js", nil)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
c.String(http.StatusOK, "it worked")
|
||||
})
|
||||
@ -207,6 +208,43 @@ func TestBadFileDescriptor(t *testing.T) {
|
||||
assert.Error(t, router.RunFd(0))
|
||||
}
|
||||
|
||||
func TestListener(t *testing.T) {
|
||||
router := New()
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
assert.NoError(t, err)
|
||||
listener, err := net.ListenTCP("tcp", addr)
|
||||
assert.NoError(t, err)
|
||||
go func() {
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.RunListener(listener))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
c, err := net.Dial("tcp", listener.Addr().String())
|
||||
assert.NoError(t, err)
|
||||
|
||||
fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
|
||||
scanner := bufio.NewScanner(c)
|
||||
var response string
|
||||
for scanner.Scan() {
|
||||
response += scanner.Text()
|
||||
}
|
||||
assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
|
||||
assert.Contains(t, response, "it worked", "resp body should match")
|
||||
}
|
||||
|
||||
func TestBadListener(t *testing.T) {
|
||||
router := New()
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:10086")
|
||||
assert.NoError(t, err)
|
||||
listener, err := net.ListenTCP("tcp", addr)
|
||||
assert.NoError(t, err)
|
||||
listener.Close()
|
||||
assert.Error(t, router.RunListener(listener))
|
||||
}
|
||||
|
||||
func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
|
||||
router := New()
|
||||
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
|
13
go.mod
13
go.mod
@ -4,14 +4,15 @@ go 1.12
|
||||
|
||||
require (
|
||||
github.com/gin-contrib/sse v0.1.0
|
||||
github.com/golang/protobuf v1.3.1
|
||||
github.com/json-iterator/go v1.1.6
|
||||
github.com/go-playground/locales v0.12.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.16.0 // indirect
|
||||
github.com/golang/protobuf v1.3.2
|
||||
github.com/json-iterator/go v1.1.7
|
||||
github.com/leodido/go-urn v1.1.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.9
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/stretchr/testify v1.3.0
|
||||
github.com/stretchr/testify v1.4.0
|
||||
github.com/ugorji/go/codec v1.1.7
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1
|
||||
gopkg.in/yaml.v2 v2.2.2
|
||||
)
|
||||
|
33
go.sum
33
go.sum
@ -1,22 +1,31 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
|
||||
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
|
||||
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
|
||||
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
|
||||
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
|
||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
|
||||
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
||||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||
@ -27,7 +36,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
8
mode.go
8
mode.go
@ -71,12 +71,18 @@ func DisableBindValidation() {
|
||||
binding.Validator = nil
|
||||
}
|
||||
|
||||
// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumberto to
|
||||
// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to
|
||||
// call the UseNumber method on the JSON Decoder instance.
|
||||
func EnableJsonDecoderUseNumber() {
|
||||
binding.EnableDecoderUseNumber = true
|
||||
}
|
||||
|
||||
// EnableJsonDecoderDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to
|
||||
// call the DisallowUnknownFields method on the JSON Decoder instance.
|
||||
func EnableJsonDecoderDisallowUnknownFields() {
|
||||
binding.EnableDecoderDisallowUnknownFields = true
|
||||
}
|
||||
|
||||
// Mode returns currently gin mode.
|
||||
func Mode() string {
|
||||
return modeName
|
||||
|
14
mode_test.go
14
mode_test.go
@ -40,8 +40,22 @@ func TestSetMode(t *testing.T) {
|
||||
assert.Panics(t, func() { SetMode("unknown") })
|
||||
}
|
||||
|
||||
func TestDisableBindValidation(t *testing.T) {
|
||||
v := binding.Validator
|
||||
assert.NotNil(t, binding.Validator)
|
||||
DisableBindValidation()
|
||||
assert.Nil(t, binding.Validator)
|
||||
binding.Validator = v
|
||||
}
|
||||
|
||||
func TestEnableJsonDecoderUseNumber(t *testing.T) {
|
||||
assert.False(t, binding.EnableDecoderUseNumber)
|
||||
EnableJsonDecoderUseNumber()
|
||||
assert.True(t, binding.EnableDecoderUseNumber)
|
||||
}
|
||||
|
||||
func TestEnableJsonDecoderDisallowUnknownFields(t *testing.T) {
|
||||
assert.False(t, binding.EnableDecoderDisallowUnknownFields)
|
||||
EnableJsonDecoderDisallowUnknownFields()
|
||||
assert.True(t, binding.EnableDecoderDisallowUnknownFields)
|
||||
}
|
||||
|
@ -22,6 +22,9 @@ type Reader struct {
|
||||
func (r Reader) Render(w http.ResponseWriter) (err error) {
|
||||
r.WriteContentType(w)
|
||||
if r.ContentLength >= 0 {
|
||||
if r.Headers == nil {
|
||||
r.Headers = map[string]string{}
|
||||
}
|
||||
r.Headers["Content-Length"] = strconv.FormatInt(r.ContentLength, 10)
|
||||
}
|
||||
r.writeHeaders(w, r.Headers)
|
||||
|
23
render/reader_test.go
Normal file
23
render/reader_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright 2019 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestReaderRenderNoHeaders(t *testing.T) {
|
||||
content := "test"
|
||||
r := Reader{
|
||||
ContentLength: int64(len(content)),
|
||||
Reader: strings.NewReader(content),
|
||||
}
|
||||
err := r.Render(httptest.NewRecorder())
|
||||
require.NoError(t, err)
|
||||
}
|
@ -347,7 +347,20 @@ func TestRenderRedirect(t *testing.T) {
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
assert.Panics(t, func() { assert.NoError(t, data2.Render(w)) })
|
||||
assert.PanicsWithValue(t, "Cannot redirect with status code 200", func() {
|
||||
err := data2.Render(w)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
data3 := Redirect{
|
||||
Code: http.StatusCreated,
|
||||
Request: req,
|
||||
Location: "/new/location",
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
err = data3.Render(w)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// only improve coverage
|
||||
data2.WriteContentType(w)
|
||||
|
@ -29,38 +29,38 @@ func init() {
|
||||
}
|
||||
|
||||
func TestResponseWriterReset(t *testing.T) {
|
||||
testWritter := httptest.NewRecorder()
|
||||
testWriter := httptest.NewRecorder()
|
||||
writer := &responseWriter{}
|
||||
var w ResponseWriter = writer
|
||||
|
||||
writer.reset(testWritter)
|
||||
writer.reset(testWriter)
|
||||
assert.Equal(t, -1, writer.size)
|
||||
assert.Equal(t, http.StatusOK, writer.status)
|
||||
assert.Equal(t, testWritter, writer.ResponseWriter)
|
||||
assert.Equal(t, testWriter, writer.ResponseWriter)
|
||||
assert.Equal(t, -1, w.Size())
|
||||
assert.Equal(t, http.StatusOK, w.Status())
|
||||
assert.False(t, w.Written())
|
||||
}
|
||||
|
||||
func TestResponseWriterWriteHeader(t *testing.T) {
|
||||
testWritter := httptest.NewRecorder()
|
||||
testWriter := httptest.NewRecorder()
|
||||
writer := &responseWriter{}
|
||||
writer.reset(testWritter)
|
||||
writer.reset(testWriter)
|
||||
w := ResponseWriter(writer)
|
||||
|
||||
w.WriteHeader(http.StatusMultipleChoices)
|
||||
assert.False(t, w.Written())
|
||||
assert.Equal(t, http.StatusMultipleChoices, w.Status())
|
||||
assert.NotEqual(t, http.StatusMultipleChoices, testWritter.Code)
|
||||
assert.NotEqual(t, http.StatusMultipleChoices, testWriter.Code)
|
||||
|
||||
w.WriteHeader(-1)
|
||||
assert.Equal(t, http.StatusMultipleChoices, w.Status())
|
||||
}
|
||||
|
||||
func TestResponseWriterWriteHeadersNow(t *testing.T) {
|
||||
testWritter := httptest.NewRecorder()
|
||||
testWriter := httptest.NewRecorder()
|
||||
writer := &responseWriter{}
|
||||
writer.reset(testWritter)
|
||||
writer.reset(testWriter)
|
||||
w := ResponseWriter(writer)
|
||||
|
||||
w.WriteHeader(http.StatusMultipleChoices)
|
||||
@ -68,7 +68,7 @@ func TestResponseWriterWriteHeadersNow(t *testing.T) {
|
||||
|
||||
assert.True(t, w.Written())
|
||||
assert.Equal(t, 0, w.Size())
|
||||
assert.Equal(t, http.StatusMultipleChoices, testWritter.Code)
|
||||
assert.Equal(t, http.StatusMultipleChoices, testWriter.Code)
|
||||
|
||||
writer.size = 10
|
||||
w.WriteHeaderNow()
|
||||
@ -76,30 +76,30 @@ func TestResponseWriterWriteHeadersNow(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResponseWriterWrite(t *testing.T) {
|
||||
testWritter := httptest.NewRecorder()
|
||||
testWriter := httptest.NewRecorder()
|
||||
writer := &responseWriter{}
|
||||
writer.reset(testWritter)
|
||||
writer.reset(testWriter)
|
||||
w := ResponseWriter(writer)
|
||||
|
||||
n, err := w.Write([]byte("hola"))
|
||||
assert.Equal(t, 4, n)
|
||||
assert.Equal(t, 4, w.Size())
|
||||
assert.Equal(t, http.StatusOK, w.Status())
|
||||
assert.Equal(t, http.StatusOK, testWritter.Code)
|
||||
assert.Equal(t, "hola", testWritter.Body.String())
|
||||
assert.Equal(t, http.StatusOK, testWriter.Code)
|
||||
assert.Equal(t, "hola", testWriter.Body.String())
|
||||
assert.NoError(t, err)
|
||||
|
||||
n, err = w.Write([]byte(" adios"))
|
||||
assert.Equal(t, 6, n)
|
||||
assert.Equal(t, 10, w.Size())
|
||||
assert.Equal(t, "hola adios", testWritter.Body.String())
|
||||
assert.Equal(t, "hola adios", testWriter.Body.String())
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestResponseWriterHijack(t *testing.T) {
|
||||
testWritter := httptest.NewRecorder()
|
||||
testWriter := httptest.NewRecorder()
|
||||
writer := &responseWriter{}
|
||||
writer.reset(testWritter)
|
||||
writer.reset(testWriter)
|
||||
w := ResponseWriter(writer)
|
||||
|
||||
assert.Panics(t, func() {
|
||||
|
@ -193,13 +193,15 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
|
||||
|
||||
file := c.Param("filepath")
|
||||
// Check if file exists and/or if we have permission to access it
|
||||
if _, err := fs.Open(file); err != nil {
|
||||
f, err := fs.Open(file)
|
||||
if err != nil {
|
||||
c.Writer.WriteHeader(http.StatusNotFound)
|
||||
c.handlers = group.engine.noRoute
|
||||
// Reset index
|
||||
c.index = -1
|
||||
return
|
||||
}
|
||||
f.Close()
|
||||
|
||||
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||
}
|
||||
|
5
tree.go
5
tree.go
@ -65,10 +65,9 @@ func min(a, b int) int {
|
||||
func countParams(path string) uint8 {
|
||||
var n uint
|
||||
for i := 0; i < len(path); i++ {
|
||||
if path[i] != ':' && path[i] != '*' {
|
||||
continue
|
||||
if path[i] == ':' || path[i] == '*' {
|
||||
n++
|
||||
}
|
||||
n++
|
||||
}
|
||||
if n >= 255 {
|
||||
return 255
|
||||
|
48
vendor/vendor.json
vendored
48
vendor/vendor.json
vendored
@ -18,6 +18,28 @@
|
||||
"version": "v0.1",
|
||||
"versionExact": "v0.1.0"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "b4DmyMT9bicTRVJw1hJXHLhIH+0=",
|
||||
"path": "github.com/go-playground/locales",
|
||||
"revision": "f63010822830b6fe52288ee52d5a1151088ce039",
|
||||
"revisionTime": "2018-03-23T16:04:04Z",
|
||||
"version": "v0.12",
|
||||
"versionExact": "v0.12.1"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "JgF260rC9YpWyY5WEljjimWLUXs=",
|
||||
"path": "github.com/go-playground/locales/currency",
|
||||
"revision": "630ebbb602847eba93e75ae38bbc7bb7abcf1ff3",
|
||||
"revisionTime": "2019-04-30T15:33:29Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "9pKcUHBaVS+360X6h4IowhmOPjk=",
|
||||
"path": "github.com/go-playground/universal-translator",
|
||||
"revision": "b32fa301c9fe55953584134cb6853a13c87ec0a1",
|
||||
"revisionTime": "2017-02-09T16:11:52Z",
|
||||
"version": "v0.16",
|
||||
"versionExact": "v0.16.0"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "Y2MOwzNZfl4NRNDbLCZa6sgx7O0=",
|
||||
"path": "github.com/golang/protobuf/proto",
|
||||
@ -26,6 +48,14 @@
|
||||
"version": "v1.3",
|
||||
"versionExact": "v1.3.0"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "zNo6yGy/bCJuzkEcP70oEBtOB2M=",
|
||||
"path": "github.com/leodido/go-urn",
|
||||
"revision": "70078a794e8ea4b497ba7c19a78cd60f90ccf0f4",
|
||||
"revisionTime": "2018-05-24T03:26:21Z",
|
||||
"version": "v1.1",
|
||||
"versionExact": "v1.1.0"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "TB2vxux9xQbvsTHOVt4aRTuvSn4=",
|
||||
"path": "github.com/json-iterator/go",
|
||||
@ -82,6 +112,12 @@
|
||||
"version": "v1.2",
|
||||
"versionExact": "v1.2.2"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "wnEANt4k5X/KGwoFyfSSnpxULm4=",
|
||||
"path": "github.com/stretchr/testify/require",
|
||||
"revision": "f35b8ab0b5a2cef36673838d662e249dd9c94686",
|
||||
"revisionTime": "2018-05-06T18:05:49Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "S4ei9eSqVThDio0Jn2sav6yUbvg=",
|
||||
"path": "github.com/ugorji/go/codec",
|
||||
@ -97,12 +133,12 @@
|
||||
"revisionTime": "2019-05-02T15:41:39Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "P/k5ZGf0lEBgpKgkwy++F7K1PSg=",
|
||||
"path": "gopkg.in/go-playground/validator.v8",
|
||||
"revision": "5f1438d3fca68893a817e4a66806cea46a9e4ebf",
|
||||
"revisionTime": "2017-07-30T05:02:35Z",
|
||||
"version": "v8.18.2",
|
||||
"versionExact": "v8.18.2"
|
||||
"checksumSHA1": "ACzc7AkwLtNgKhqtj8V7SGUJgnw=",
|
||||
"path": "gopkg.in/go-playground/validator.v9",
|
||||
"revision": "46b4b1e301c24cac870ffcb4ba5c8a703d1ef475",
|
||||
"revisionTime": "2019-03-31T13:31:25Z",
|
||||
"version": "v9.28",
|
||||
"versionExact": "v9.28.0"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "QqDq2x8XOU7IoOR98Cx1eiV5QY8=",
|
||||
|
@ -5,4 +5,4 @@
|
||||
package gin
|
||||
|
||||
// Version is the current gin framework's version.
|
||||
const Version = "v1.4.0-dev"
|
||||
const Version = "v1.5.0"
|
||||
|
Loading…
x
Reference in New Issue
Block a user