More unit tests for form binding

This commit is contained in:
Manu Mtz-Almeida 2015-07-08 04:26:37 +02:00
parent 5f2f8d9cb4
commit 0873992f38
2 changed files with 16 additions and 9 deletions

View File

@ -56,7 +56,6 @@ func mapForm(ptr interface{}, form map[string][]string) error {
return err
}
}
}
return nil
}

View File

@ -42,6 +42,9 @@ func createMultipartRequest() *http.Request {
must(mw.SetBoundary(boundary))
must(mw.WriteField("foo", "bar"))
must(mw.WriteField("bar", "foo"))
must(mw.WriteField("bar", "foo2"))
must(mw.WriteField("array", "first"))
must(mw.WriteField("array", "second"))
req, err := http.NewRequest("POST", "/", body)
must(err)
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
@ -184,8 +187,8 @@ func TestContextQuery(t *testing.T) {
func TestContextQueryAndPostForm(t *testing.T) {
c, _, _ := createTestContext()
body := bytes.NewBufferString("foo=bar&page=11&both=POST")
c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main", body)
body := bytes.NewBufferString("foo=bar&page=11&both=POST&foo=second")
c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body)
c.Request.Header.Add("Content-Type", MIMEPOSTForm)
assert.Equal(t, c.DefaultPostForm("foo", "none"), "bar")
@ -212,12 +215,14 @@ func TestContextQueryAndPostForm(t *testing.T) {
ID string `form:"id"`
Page string `form:"page"`
Both string `form:"both"`
Array []string `form:"array[]"`
}
assert.NoError(t, c.Bind(&obj))
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.ID, "main")
assert.Equal(t, obj.Page, "11")
assert.Equal(t, obj.Both, "POST")
assert.Equal(t, obj.Array, []string{"first", "second"})
}
func TestContextPostFormMultipart(t *testing.T) {
@ -227,14 +232,17 @@ func TestContextPostFormMultipart(t *testing.T) {
var obj struct {
Foo string `form:"foo"`
Bar string `form:"bar"`
Array []string `form:"array"`
}
assert.NoError(t, c.Bind(&obj))
assert.Equal(t, obj.Bar, "foo")
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.Array, []string{"first", "second"})
assert.Empty(t, c.Query("foo"))
assert.Empty(t, c.Query("bar"))
assert.Equal(t, c.PostForm("foo"), "bar")
assert.Equal(t, c.PostForm("array"), "first")
assert.Equal(t, c.PostForm("bar"), "foo")
}