Merge branch 'develop' of github.com:gin-gonic/gin into develop
This commit is contained in:
commit
51599d3c0a
@ -114,18 +114,54 @@ func mapForm(ptr interface{}, form map[string][]string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setIntField(val string, bitSize int, structField reflect.Value) error {
|
||||||
|
if val == "" {
|
||||||
|
val = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
intVal, err := strconv.ParseInt(val, 10, bitSize)
|
||||||
|
if err == nil {
|
||||||
|
structField.SetInt(intVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func setUintField(val string, bitSize int, structField reflect.Value) error {
|
||||||
|
if val == "" {
|
||||||
|
val = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
uintVal, err := strconv.ParseUint(val, 10, bitSize)
|
||||||
|
if err == nil {
|
||||||
|
structField.SetUint(uintVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
|
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
|
||||||
switch valueKind {
|
switch valueKind {
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int:
|
||||||
if val == "" {
|
return setIntField(val, 0, structField)
|
||||||
val = "0"
|
case reflect.Int8:
|
||||||
}
|
return setIntField(val, 8, structField)
|
||||||
intVal, err := strconv.Atoi(val)
|
case reflect.Int16:
|
||||||
if err != nil {
|
return setIntField(val, 16, structField)
|
||||||
return err
|
case reflect.Int32:
|
||||||
} else {
|
return setIntField(val, 32, structField)
|
||||||
structField.SetInt(int64(intVal))
|
case reflect.Int64:
|
||||||
}
|
return setIntField(val, 64, structField)
|
||||||
|
case reflect.Uint:
|
||||||
|
return setUintField(val, 0, structField)
|
||||||
|
case reflect.Uint8:
|
||||||
|
return setUintField(val, 8, structField)
|
||||||
|
case reflect.Uint16:
|
||||||
|
return setUintField(val, 16, structField)
|
||||||
|
case reflect.Uint32:
|
||||||
|
return setUintField(val, 32, structField)
|
||||||
|
case reflect.Uint64:
|
||||||
|
return setUintField(val, 64, structField)
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
if val == "" {
|
if val == "" {
|
||||||
val = "false"
|
val = "false"
|
||||||
|
@ -441,6 +441,42 @@ func TestBindingJSONMalformed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBindingForm(t *testing.T) {
|
||||||
|
|
||||||
|
body := bytes.NewBuffer([]byte("foo=bar&num=123&unum=1234567890"))
|
||||||
|
|
||||||
|
r := New()
|
||||||
|
r.POST("/binding/form", func(c *Context) {
|
||||||
|
var body struct {
|
||||||
|
Foo string `form:"foo"`
|
||||||
|
Num int `form:"num"`
|
||||||
|
Unum uint `form:"unum"`
|
||||||
|
}
|
||||||
|
if c.Bind(&body) {
|
||||||
|
c.JSON(200, H{"foo": body.Foo, "num": body.Num, "unum": body.Unum})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("POST", "/binding/form", body)
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != 200 {
|
||||||
|
t.Errorf("Response code should be Ok, was: %d", w.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := "{\"foo\":\"bar\",\"num\":123,\"unum\":1234567890}\n"
|
||||||
|
if w.Body.String() != expected {
|
||||||
|
t.Errorf("Response should be %s, was %s", expected, w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
||||||
|
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestClientIP(t *testing.T) {
|
func TestClientIP(t *testing.T) {
|
||||||
r := New()
|
r := New()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user