From 865ea804c98ff56161a698e748c90708a0e811b4 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Tue, 26 May 2015 12:08:33 +0200 Subject: [PATCH] Renames new API for query/postform and params. --- context.go | 38 +++++++++++++------------------------- context_test.go | 15 ++++++++++----- routes_test.go | 4 ---- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/context.go b/context.go index 5ff2e73b..f77424f8 100644 --- a/context.go +++ b/context.go @@ -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 { diff --git a/context_test.go b/context_test.go index 8898c3e1..49aad52c 100644 --- a/context_test.go +++ b/context_test.go @@ -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 diff --git a/routes_test.go b/routes_test.go index 3d9cf889..4bd0682e 100644 --- a/routes_test.go +++ b/routes_test.go @@ -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")