Adds GetQuery() and GetPostForm() APIs

This commit is contained in:
Manu Mtz.-Almeida 2016-01-29 02:07:44 +01:00
parent 19b5f726f4
commit afc499f306
2 changed files with 85 additions and 22 deletions

View File

@ -191,14 +191,14 @@ func (c *Context) MustGet(key string) interface{} {
// c.Query("name") == "Manu" // c.Query("name") == "Manu"
// c.Query("wtf") == "" // c.Query("wtf") == ""
func (c *Context) Query(key string) string { func (c *Context) Query(key string) string {
value, _ := c.query(key) value, _ := c.GetQuery(key)
return value return value
} }
// PostForm is a shortcut for c.Request.PostFormValue(key) // PostForm is a shortcut for c.Request.PostFormValue(key)
// It returns an empty string ("") when the value does not exist. // It returns an empty string ("") when the value does not exist.
func (c *Context) PostForm(key string) string { func (c *Context) PostForm(key string) string {
value, _ := c.postForm(key) value, _ := c.GetPostForm(key)
return value return value
} }
@ -213,7 +213,7 @@ func (c *Context) Param(key string) string {
} }
func (c *Context) DefaultPostForm(key, defaultValue string) string { func (c *Context) DefaultPostForm(key, defaultValue string) string {
if value, ok := c.postForm(key); ok { if value, ok := c.GetPostForm(key); ok {
return value return value
} }
return defaultValue return defaultValue
@ -225,13 +225,13 @@ func (c *Context) DefaultPostForm(key, defaultValue string) string {
// c.DefaultQuery("name", "unknown") == "Manu" // c.DefaultQuery("name", "unknown") == "Manu"
// c.DefaultQuery("id", "none") == "none" // c.DefaultQuery("id", "none") == "none"
func (c *Context) DefaultQuery(key, defaultValue string) string { func (c *Context) DefaultQuery(key, defaultValue string) string {
if value, ok := c.query(key); ok { if value, ok := c.GetQuery(key); ok {
return value return value
} }
return defaultValue return defaultValue
} }
func (c *Context) query(key string) (string, bool) { func (c *Context) GetQuery(key string) (string, bool) {
req := c.Request req := c.Request
if values, ok := req.URL.Query()[key]; ok && len(values) > 0 { if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
return values[0], true return values[0], true
@ -239,7 +239,7 @@ func (c *Context) query(key string) (string, bool) {
return "", false return "", false
} }
func (c *Context) postForm(key string) (string, bool) { func (c *Context) GetPostForm(key string) (string, bool) {
req := c.Request req := c.Request
req.ParseMultipartForm(32 << 20) // 32 MB req.ParseMultipartForm(32 << 20) // 32 MB
if values := req.PostForm[key]; len(values) > 0 { if values := req.PostForm[key]; len(values) > 0 {

View File

@ -33,10 +33,11 @@ func createMultipartRequest() *http.Request {
must(mw.SetBoundary(boundary)) must(mw.SetBoundary(boundary))
must(mw.WriteField("foo", "bar")) must(mw.WriteField("foo", "bar"))
must(mw.WriteField("bar", "foo")) must(mw.WriteField("bar", "10"))
must(mw.WriteField("bar", "foo2")) must(mw.WriteField("bar", "foo2"))
must(mw.WriteField("array", "first")) must(mw.WriteField("array", "first"))
must(mw.WriteField("array", "second")) must(mw.WriteField("array", "second"))
must(mw.WriteField("id", ""))
req, err := http.NewRequest("POST", "/", body) req, err := http.NewRequest("POST", "/", body)
must(err) must(err)
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary) req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
@ -162,24 +163,42 @@ func handlerNameTest(c *Context) {
func TestContextQuery(t *testing.T) { func TestContextQuery(t *testing.T) {
c, _, _ := CreateTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10", nil) c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil)
value, ok := c.GetQuery("foo")
assert.True(t, ok)
assert.Equal(t, value, "bar")
assert.Equal(t, c.DefaultQuery("foo", "none"), "bar") assert.Equal(t, c.DefaultQuery("foo", "none"), "bar")
assert.Equal(t, c.Query("foo"), "bar") assert.Equal(t, c.Query("foo"), "bar")
assert.Empty(t, c.PostForm("foo"))
value, ok = c.GetQuery("page")
assert.True(t, ok)
assert.Equal(t, value, "10")
assert.Equal(t, c.DefaultQuery("page", "0"), "10") assert.Equal(t, c.DefaultQuery("page", "0"), "10")
assert.Equal(t, c.Query("page"), "10") assert.Equal(t, c.Query("page"), "10")
assert.Empty(t, c.PostForm("page"))
value, ok = c.GetQuery("id")
assert.True(t, ok)
assert.Empty(t, value)
assert.Equal(t, c.DefaultQuery("id", "nada"), "")
assert.Empty(t, c.Query("id"))
value, ok = c.GetQuery("NoKey")
assert.False(t, ok)
assert.Empty(t, value)
assert.Equal(t, c.DefaultQuery("NoKey", "nada"), "nada") assert.Equal(t, c.DefaultQuery("NoKey", "nada"), "nada")
assert.Empty(t, c.Query("NoKey")) assert.Empty(t, c.Query("NoKey"))
assert.Empty(t, c.PostForm("NoKey"))
// postform should not mess
value, ok = c.GetPostForm("page")
assert.False(t, ok)
assert.Empty(t, value)
assert.Empty(t, c.PostForm("foo"))
} }
func TestContextQueryAndPostForm(t *testing.T) { func TestContextQueryAndPostForm(t *testing.T) {
c, _, _ := CreateTestContext() c, _, _ := CreateTestContext()
body := bytes.NewBufferString("foo=bar&page=11&both=POST&foo=second") body := bytes.NewBufferString("foo=bar&page=11&both=&foo=second")
c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body) c.Request, _ = http.NewRequest("POST", "/?both=GET&id=main&id=omit&array[]=first&array[]=second", body)
c.Request.Header.Add("Content-Type", MIMEPOSTForm) c.Request.Header.Add("Content-Type", MIMEPOSTForm)
@ -187,33 +206,50 @@ func TestContextQueryAndPostForm(t *testing.T) {
assert.Equal(t, c.PostForm("foo"), "bar") assert.Equal(t, c.PostForm("foo"), "bar")
assert.Empty(t, c.Query("foo")) assert.Empty(t, c.Query("foo"))
value, ok := c.GetPostForm("page")
assert.True(t, ok)
assert.Equal(t, value, "11")
assert.Equal(t, c.DefaultPostForm("page", "0"), "11") assert.Equal(t, c.DefaultPostForm("page", "0"), "11")
assert.Equal(t, c.PostForm("page"), "11") assert.Equal(t, c.PostForm("page"), "11")
assert.Equal(t, c.Query("page"), "") assert.Equal(t, c.Query("page"), "")
assert.Equal(t, c.PostForm("both"), "POST") value, ok = c.GetPostForm("both")
assert.True(t, ok)
assert.Empty(t, value)
assert.Empty(t, c.PostForm("both"))
assert.Equal(t, c.DefaultPostForm("both", "nothing"), "")
assert.Equal(t, c.Query("both"), "GET") assert.Equal(t, c.Query("both"), "GET")
value, ok = c.GetQuery("id")
assert.True(t, ok)
assert.Equal(t, value, "main")
assert.Equal(t, c.DefaultPostForm("id", "000"), "000") assert.Equal(t, c.DefaultPostForm("id", "000"), "000")
assert.Equal(t, c.Query("id"), "main") assert.Equal(t, c.Query("id"), "main")
assert.Empty(t, c.PostForm("id")) assert.Empty(t, c.PostForm("id"))
value, ok = c.GetQuery("NoKey")
assert.False(t, ok)
assert.Empty(t, value)
value, ok = c.GetPostForm("NoKey")
assert.False(t, ok)
assert.Empty(t, value)
assert.Equal(t, c.DefaultPostForm("NoKey", "nada"), "nada") assert.Equal(t, c.DefaultPostForm("NoKey", "nada"), "nada")
assert.Equal(t, c.DefaultQuery("NoKey", "nothing"), "nothing")
assert.Empty(t, c.PostForm("NoKey")) assert.Empty(t, c.PostForm("NoKey"))
assert.Empty(t, c.Query("NoKey")) assert.Empty(t, c.Query("NoKey"))
var obj struct { var obj struct {
Foo string `form:"foo"` Foo string `form:"foo"`
ID string `form:"id"` ID string `form:"id"`
Page string `form:"page"` Page int `form:"page"`
Both string `form:"both"` Both string `form:"both"`
Array []string `form:"array[]"` Array []string `form:"array[]"`
} }
assert.NoError(t, c.Bind(&obj)) assert.NoError(t, c.Bind(&obj))
assert.Equal(t, obj.Foo, "bar") assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.ID, "main") assert.Equal(t, obj.ID, "main")
assert.Equal(t, obj.Page, "11") assert.Equal(t, obj.Page, 11)
assert.Equal(t, obj.Both, "POST") assert.Equal(t, obj.Both, "")
assert.Equal(t, obj.Array, []string{"first", "second"}) assert.Equal(t, obj.Array, []string{"first", "second"})
} }
@ -222,20 +258,47 @@ func TestContextPostFormMultipart(t *testing.T) {
c.Request = createMultipartRequest() c.Request = createMultipartRequest()
var obj struct { var obj struct {
Foo string `form:"foo"` Foo string `form:"foo"`
Bar string `form:"bar"` Bar string `form:"bar"`
Array []string `form:"array"` BarAsInt int `form:"bar"`
Array []string `form:"array"`
Id string `form:"id"`
} }
assert.NoError(t, c.Bind(&obj)) assert.NoError(t, c.Bind(&obj))
assert.Equal(t, obj.Bar, "foo")
assert.Equal(t, obj.Foo, "bar") assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.Bar, "10")
assert.Equal(t, obj.BarAsInt, 10)
assert.Equal(t, obj.Array, []string{"first", "second"}) assert.Equal(t, obj.Array, []string{"first", "second"})
assert.Equal(t, obj.Id, "")
assert.Empty(t, c.Query("foo")) value, ok := c.GetQuery("foo")
assert.False(t, ok)
assert.Empty(t, value)
assert.Empty(t, c.Query("bar")) assert.Empty(t, c.Query("bar"))
assert.Equal(t, c.DefaultQuery("id", "nothing"), "nothing")
value, ok = c.GetPostForm("foo")
assert.True(t, ok)
assert.Equal(t, value, "bar")
assert.Equal(t, c.PostForm("foo"), "bar") assert.Equal(t, c.PostForm("foo"), "bar")
value, ok = c.GetPostForm("array")
assert.True(t, ok)
assert.Equal(t, value, "first")
assert.Equal(t, c.PostForm("array"), "first") assert.Equal(t, c.PostForm("array"), "first")
assert.Equal(t, c.PostForm("bar"), "foo")
assert.Equal(t, c.DefaultPostForm("bar", "nothing"), "10")
value, ok = c.GetPostForm("id")
assert.True(t, ok)
assert.Empty(t, value)
assert.Empty(t, c.PostForm("id"))
assert.Empty(t, c.DefaultPostForm("id", "nothing"))
value, ok = c.GetPostForm("nokey")
assert.False(t, ok)
assert.Empty(t, value)
assert.Equal(t, c.DefaultPostForm("nokey", "nothing"), "nothing")
} }
func TestContextSetCookie(t *testing.T) { func TestContextSetCookie(t *testing.T) {