mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
Merge branch 'master' into change_isterm_method
This commit is contained in:
commit
bdd6c78d5b
17
.travis.yml
17
.travis.yml
@ -1,22 +1,19 @@
|
|||||||
language: go
|
language: go
|
||||||
sudo: false
|
|
||||||
go:
|
|
||||||
- 1.6.x
|
|
||||||
- 1.7.x
|
|
||||||
- 1.8.x
|
|
||||||
- 1.9.x
|
|
||||||
- 1.10.x
|
|
||||||
- 1.11.x
|
|
||||||
- 1.12.x
|
|
||||||
- master
|
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
fast_finish: true
|
fast_finish: true
|
||||||
include:
|
include:
|
||||||
|
- go: 1.6.x
|
||||||
|
- go: 1.7.x
|
||||||
|
- go: 1.8.x
|
||||||
|
- go: 1.9.x
|
||||||
|
- go: 1.10.x
|
||||||
- go: 1.11.x
|
- go: 1.11.x
|
||||||
env: GO111MODULE=on
|
env: GO111MODULE=on
|
||||||
- go: 1.12.x
|
- go: 1.12.x
|
||||||
env: GO111MODULE=on
|
env: GO111MODULE=on
|
||||||
|
- go: master
|
||||||
|
env: GO111MODULE=on
|
||||||
|
|
||||||
git:
|
git:
|
||||||
depth: 10
|
depth: 10
|
||||||
|
@ -1430,3 +1430,31 @@ func TestBindingTimeDuration(t *testing.T) {
|
|||||||
err = Form.Bind(req, &s)
|
err = Form.Bind(req, &s)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBindingArray(t *testing.T) {
|
||||||
|
var s struct {
|
||||||
|
Nums [2]int `form:"nums,default=4"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// default
|
||||||
|
req := formPostRequest("", "")
|
||||||
|
err := Form.Bind(req, &s)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, [2]int{0, 0}, s.Nums)
|
||||||
|
|
||||||
|
// ok
|
||||||
|
req = formPostRequest("", "nums=3&nums=8")
|
||||||
|
err = Form.Bind(req, &s)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, [2]int{3, 8}, s.Nums)
|
||||||
|
|
||||||
|
// not enough vals
|
||||||
|
req = formPostRequest("", "nums=3")
|
||||||
|
err = Form.Bind(req, &s)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
// error
|
||||||
|
req = formPostRequest("", "nums=3&nums=wrong")
|
||||||
|
err = Form.Bind(req, &s)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ package binding
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -118,6 +119,14 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, form map[stri
|
|||||||
vs = []string{defaultValue}
|
vs = []string{defaultValue}
|
||||||
}
|
}
|
||||||
return true, setSlice(vs, value, field)
|
return true, setSlice(vs, value, field)
|
||||||
|
case reflect.Array:
|
||||||
|
if !ok {
|
||||||
|
vs = []string{defaultValue}
|
||||||
|
}
|
||||||
|
if len(vs) != value.Len() {
|
||||||
|
return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String())
|
||||||
|
}
|
||||||
|
return true, setArray(vs, value, field)
|
||||||
default:
|
default:
|
||||||
var val string
|
var val string
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -256,14 +265,22 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSlice(vals []string, value reflect.Value, field reflect.StructField) error {
|
func setArray(vals []string, value reflect.Value, field reflect.StructField) error {
|
||||||
slice := reflect.MakeSlice(value.Type(), len(vals), len(vals))
|
|
||||||
for i, s := range vals {
|
for i, s := range vals {
|
||||||
err := setWithProperType(s, slice.Index(i), field)
|
err := setWithProperType(s, value.Index(i), field)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setSlice(vals []string, value reflect.Value, field reflect.StructField) error {
|
||||||
|
slice := reflect.MakeSlice(value.Type(), len(vals), len(vals))
|
||||||
|
err := setArray(vals, slice, field)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
value.Set(slice)
|
value.Set(slice)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
# Gin Examples
|
# Gin Examples
|
||||||
|
|
||||||
⚠️ **NOTICE:** All gin examples has moved as alone repository to [here](https://github.com/gin-gonic/examples).
|
⚠️ **NOTICE:** All gin examples have been moved as standalone repository to [here](https://github.com/gin-gonic/examples).
|
||||||
|
@ -225,7 +225,6 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
|
|||||||
assert.Equal(t, "GET", gotParam.Method)
|
assert.Equal(t, "GET", gotParam.Method)
|
||||||
assert.Equal(t, "/example?a=100", gotParam.Path)
|
assert.Equal(t, "/example?a=100", gotParam.Path)
|
||||||
assert.Empty(t, gotParam.ErrorMessage)
|
assert.Empty(t, gotParam.ErrorMessage)
|
||||||
assert.Empty(t, gotParam.ErrorMessage)
|
|
||||||
assert.Equal(t, gotKeys, gotParam.Keys)
|
assert.Equal(t, gotKeys, gotParam.Keys)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -195,7 +195,7 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
|
|||||||
// Check if file exists and/or if we have permission to access it
|
// Check if file exists and/or if we have permission to access it
|
||||||
if _, err := fs.Open(file); err != nil {
|
if _, err := fs.Open(file); err != nil {
|
||||||
c.Writer.WriteHeader(http.StatusNotFound)
|
c.Writer.WriteHeader(http.StatusNotFound)
|
||||||
c.handlers = group.engine.allNoRoute
|
c.handlers = group.engine.noRoute
|
||||||
// Reset index
|
// Reset index
|
||||||
c.index = -1
|
c.index = -1
|
||||||
return
|
return
|
||||||
|
@ -429,7 +429,6 @@ func TestRouterNotFound(t *testing.T) {
|
|||||||
|
|
||||||
func TestRouterStaticFSNotFound(t *testing.T) {
|
func TestRouterStaticFSNotFound(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
|
|
||||||
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
||||||
router.NoRoute(func(c *Context) {
|
router.NoRoute(func(c *Context) {
|
||||||
c.String(404, "non existent")
|
c.String(404, "non existent")
|
||||||
@ -452,6 +451,27 @@ func TestRouterStaticFSFileNotFound(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reproduction test for the bug of issue #1805
|
||||||
|
func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) {
|
||||||
|
router := New()
|
||||||
|
|
||||||
|
// Middleware must be called just only once by per request.
|
||||||
|
middlewareCalledNum := 0
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
middlewareCalledNum += 1
|
||||||
|
})
|
||||||
|
|
||||||
|
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
||||||
|
|
||||||
|
// First access
|
||||||
|
performRequest(router, "GET", "/nonexistent")
|
||||||
|
assert.Equal(t, 1, middlewareCalledNum)
|
||||||
|
|
||||||
|
// Second access
|
||||||
|
performRequest(router, "HEAD", "/nonexistent")
|
||||||
|
assert.Equal(t, 2, middlewareCalledNum)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRouteRawPath(t *testing.T) {
|
func TestRouteRawPath(t *testing.T) {
|
||||||
route := New()
|
route := New()
|
||||||
route.UseRawPath = true
|
route.UseRawPath = true
|
||||||
|
BIN
testdata/assets/console.png
vendored
Normal file
BIN
testdata/assets/console.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 58 KiB |
Loading…
x
Reference in New Issue
Block a user