mirror of https://github.com/gin-gonic/gin.git
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
|
||||
}
|
||||
|
||||
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 {
|
||||
switch valueKind {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
if val == "" {
|
||||
val = "0"
|
||||
}
|
||||
intVal, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
structField.SetInt(int64(intVal))
|
||||
}
|
||||
case reflect.Int:
|
||||
return setIntField(val, 0, structField)
|
||||
case reflect.Int8:
|
||||
return setIntField(val, 8, structField)
|
||||
case reflect.Int16:
|
||||
return setIntField(val, 16, structField)
|
||||
case reflect.Int32:
|
||||
return setIntField(val, 32, structField)
|
||||
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:
|
||||
if val == "" {
|
||||
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) {
|
||||
r := New()
|
||||
|
||||
|
|
Loading…
Reference in New Issue