forked from mirror/gin
Adds GetQuery() and GetPostForm() APIs
This commit is contained in:
parent
19b5f726f4
commit
afc499f306
12
context.go
12
context.go
|
@ -191,14 +191,14 @@ func (c *Context) MustGet(key string) interface{} {
|
|||
// c.Query("name") == "Manu"
|
||||
// c.Query("wtf") == ""
|
||||
func (c *Context) Query(key string) string {
|
||||
value, _ := c.query(key)
|
||||
value, _ := c.GetQuery(key)
|
||||
return value
|
||||
}
|
||||
|
||||
// PostForm is a shortcut for c.Request.PostFormValue(key)
|
||||
// It returns an empty string ("") when the value does not exist.
|
||||
func (c *Context) PostForm(key string) string {
|
||||
value, _ := c.postForm(key)
|
||||
value, _ := c.GetPostForm(key)
|
||||
return value
|
||||
}
|
||||
|
||||
|
@ -213,7 +213,7 @@ func (c *Context) Param(key 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 defaultValue
|
||||
|
@ -225,13 +225,13 @@ func (c *Context) DefaultPostForm(key, defaultValue string) string {
|
|||
// c.DefaultQuery("name", "unknown") == "Manu"
|
||||
// c.DefaultQuery("id", "none") == "none"
|
||||
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 defaultValue
|
||||
}
|
||||
|
||||
func (c *Context) query(key string) (string, bool) {
|
||||
func (c *Context) GetQuery(key string) (string, bool) {
|
||||
req := c.Request
|
||||
if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
|
||||
return values[0], true
|
||||
|
@ -239,7 +239,7 @@ func (c *Context) query(key string) (string, bool) {
|
|||
return "", false
|
||||
}
|
||||
|
||||
func (c *Context) postForm(key string) (string, bool) {
|
||||
func (c *Context) GetPostForm(key string) (string, bool) {
|
||||
req := c.Request
|
||||
req.ParseMultipartForm(32 << 20) // 32 MB
|
||||
if values := req.PostForm[key]; len(values) > 0 {
|
||||
|
|
|
@ -33,10 +33,11 @@ func createMultipartRequest() *http.Request {
|
|||
|
||||
must(mw.SetBoundary(boundary))
|
||||
must(mw.WriteField("foo", "bar"))
|
||||
must(mw.WriteField("bar", "foo"))
|
||||
must(mw.WriteField("bar", "10"))
|
||||
must(mw.WriteField("bar", "foo2"))
|
||||
must(mw.WriteField("array", "first"))
|
||||
must(mw.WriteField("array", "second"))
|
||||
must(mw.WriteField("id", ""))
|
||||
req, err := http.NewRequest("POST", "/", body)
|
||||
must(err)
|
||||
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
||||
|
@ -162,24 +163,42 @@ func handlerNameTest(c *Context) {
|
|||
|
||||
func TestContextQuery(t *testing.T) {
|
||||
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.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.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.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) {
|
||||
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.Header.Add("Content-Type", MIMEPOSTForm)
|
||||
|
||||
|
@ -187,33 +206,50 @@ func TestContextQueryAndPostForm(t *testing.T) {
|
|||
assert.Equal(t, c.PostForm("foo"), "bar")
|
||||
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.PostForm("page"), "11")
|
||||
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")
|
||||
|
||||
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.Query("id"), "main")
|
||||
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.DefaultQuery("NoKey", "nothing"), "nothing")
|
||||
assert.Empty(t, c.PostForm("NoKey"))
|
||||
assert.Empty(t, c.Query("NoKey"))
|
||||
|
||||
var obj struct {
|
||||
Foo string `form:"foo"`
|
||||
ID string `form:"id"`
|
||||
Page string `form:"page"`
|
||||
Page int `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.Page, 11)
|
||||
assert.Equal(t, obj.Both, "")
|
||||
assert.Equal(t, obj.Array, []string{"first", "second"})
|
||||
}
|
||||
|
||||
|
@ -222,20 +258,47 @@ func TestContextPostFormMultipart(t *testing.T) {
|
|||
c.Request = createMultipartRequest()
|
||||
|
||||
var obj struct {
|
||||
Foo string `form:"foo"`
|
||||
Bar string `form:"bar"`
|
||||
Array []string `form:"array"`
|
||||
Foo string `form:"foo"`
|
||||
Bar string `form:"bar"`
|
||||
BarAsInt int `form:"bar"`
|
||||
Array []string `form:"array"`
|
||||
Id string `form:"id"`
|
||||
}
|
||||
assert.NoError(t, c.Bind(&obj))
|
||||
assert.Equal(t, obj.Bar, "foo")
|
||||
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.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.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")
|
||||
|
||||
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("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) {
|
||||
|
|
Loading…
Reference in New Issue