Renames new API for query/postform and params.

This commit is contained in:
Manu Mtz-Almeida 2015-05-26 12:08:33 +02:00
parent 40d41bbe96
commit 865ea804c9
3 changed files with 23 additions and 34 deletions

View File

@ -191,58 +191,46 @@ func (c *Context) MustGet(key string) interface{} {
/************************************/
/** Shortcut for c.Request.FormValue(key) */
func (c *Context) FormValue(key string) (va string) {
va, _ = c.formValue(key)
func (c *Context) Query(key string) (va string) {
va, _ = c.query(key)
return
}
/** Shortcut for c.Request.PostFormValue(key) */
func (c *Context) PostFormValue(key string) (va string) {
va, _ = c.postFormValue(key)
func (c *Context) PostForm(key string) (va string) {
va, _ = c.postForm(key)
return
}
/** Shortcut for c.Params.ByName(key) */
func (c *Context) ParamValue(key string) (va string) {
va, _ = c.paramValue(key)
func (c *Context) Param(key string) (va string) {
va, _ = c.Params.Get(key)
return
}
func (c *Context) DefaultPostFormValue(key, defaultValue string) string {
if va, ok := c.postFormValue(key); ok {
func (c *Context) DefaultPostForm(key, defaultValue string) string {
if va, ok := c.postForm(key); ok {
return va
}
return defaultValue
}
func (c *Context) DefaultFormValue(key, defaultValue string) string {
if va, ok := c.formValue(key); ok {
func (c *Context) DefaultQuery(key, defaultValue string) string {
if va, ok := c.query(key); ok {
return va
}
return defaultValue
}
func (c *Context) DefaultParamValue(key, defaultValue string) string {
if va, ok := c.paramValue(key); ok {
return va
}
return defaultValue
}
func (c *Context) paramValue(key string) (string, bool) {
return c.Params.Get(key)
}
func (c *Context) formValue(key string) (string, bool) {
func (c *Context) query(key string) (string, bool) {
req := c.Request
req.ParseForm()
if values, ok := req.Form[key]; ok && len(values) > 0 {
if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
return values[0], true
}
return "", false
}
func (c *Context) postFormValue(key string) (string, bool) {
func (c *Context) postForm(key string) (string, bool) {
req := c.Request
req.ParseForm()
if values, ok := req.PostForm[key]; ok && len(values) > 0 {

View File

@ -139,14 +139,14 @@ func TestContextPostFormParse(t *testing.T) {
assert.Equal(t, c.DefaultPostFormValue("foo", "none"), "bar")
assert.Equal(t, c.PostFormValue("foo"), "bar")
assert.Equal(t, c.FormValue("foo"), "bar")
assert.Empty(t, c.FormValue("foo"))
assert.Equal(t, c.DefaultPostFormValue("page", "0"), "11")
assert.Equal(t, c.PostFormValue("page"), "11")
assert.Equal(t, c.FormValue("page"), "11")
assert.Equal(t, c.DefaultPostForm("page", "0"), "11")
assert.Equal(t, c.PostForm("page"), "11")
assert.Equal(t, c.InputQuery("page"), "")
assert.Equal(t, c.PostFormValue("both"), "POST")
assert.Equal(t, c.FormValue("both"), "POST")
assert.Equal(t, c.FormValue("both"), "GET")
assert.Equal(t, c.FormValue("id"), "main")
assert.Empty(t, c.PostFormValue("id"))
@ -154,6 +154,11 @@ func TestContextPostFormParse(t *testing.T) {
assert.Equal(t, c.DefaultPostFormValue("NoKey", "nada"), "nada")
assert.Empty(t, c.PostFormValue("NoKey"))
assert.Empty(t, c.FormValue("NoKey"))
c.Param("page")
c.Query("page")
c.PostForm("page")
}
// Tests that the response is serialized as JSON

View File

@ -128,10 +128,6 @@ func TestRouteParamsByName(t *testing.T) {
assert.Equal(t, name, c.ParamValue("name"))
assert.Equal(t, lastName, c.ParamValue("last_name"))
assert.Equal(t, name, c.DefaultParamValue("name", "nothing"))
assert.Equal(t, lastName, c.DefaultParamValue("last_name", "nothing"))
assert.Equal(t, c.DefaultParamValue("noKey", "default"), "default")
})
w := performRequest(router, "GET", "/test/john/smith/is/super/great")