mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Added new APIs
Context.Query - Read query param Context.Form - Read form param Fixed #124 Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
a3e74772e7
commit
8f489d47f3
15
context.go
15
context.go
@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/websocket"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -18,6 +19,7 @@ type (
|
||||
socket *websocket.Conn
|
||||
pnames []string
|
||||
pvalues []string
|
||||
query url.Values
|
||||
store store
|
||||
echo *Echo
|
||||
}
|
||||
@ -71,6 +73,19 @@ func (c *Context) Param(name string) (value string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Query returns query parameter by name.
|
||||
func (c *Context) Query(name string) string {
|
||||
if c.query == nil {
|
||||
c.query = c.request.URL.Query()
|
||||
}
|
||||
return c.query.Get(name)
|
||||
}
|
||||
|
||||
// Form returns form parameter by name.
|
||||
func (c *Context) Form(name string) string {
|
||||
return c.request.FormValue(name)
|
||||
}
|
||||
|
||||
// Get retrieves data from the context.
|
||||
func (c *Context) Get(key string) interface{} {
|
||||
return c.store[key]
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -38,16 +39,12 @@ func TestContext(t *testing.T) {
|
||||
// Socket
|
||||
assert.Nil(t, c.Socket())
|
||||
|
||||
//-------
|
||||
// Param
|
||||
//-------
|
||||
|
||||
// By id
|
||||
// Param by id
|
||||
c.pnames = []string{"id"}
|
||||
c.pvalues = []string{"1"}
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
|
||||
// By name
|
||||
// Param by name
|
||||
assert.Equal(t, "1", c.Param("id"))
|
||||
|
||||
// Store
|
||||
@ -145,6 +142,35 @@ func TestContext(t *testing.T) {
|
||||
c.reset(req, NewResponse(httptest.NewRecorder()), New())
|
||||
}
|
||||
|
||||
func TestContextQuery(t *testing.T) {
|
||||
q := make(url.Values)
|
||||
q.Set("name", "joe")
|
||||
q.Set("email", "joe@labstack.com")
|
||||
|
||||
req, err := http.NewRequest(GET, "/", nil)
|
||||
assert.NoError(t, err)
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
c := NewContext(req, nil, New())
|
||||
assert.Equal(t, "joe", c.Query("name"))
|
||||
assert.Equal(t, "joe@labstack.com", c.Query("email"))
|
||||
|
||||
}
|
||||
|
||||
func TestContextForm(t *testing.T) {
|
||||
f := make(url.Values)
|
||||
f.Set("name", "joe")
|
||||
f.Set("email", "joe@labstack.com")
|
||||
|
||||
req, err := http.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
||||
assert.NoError(t, err)
|
||||
req.Header.Add(ContentType, ApplicationForm)
|
||||
|
||||
c := NewContext(req, nil, New())
|
||||
assert.Equal(t, "joe", c.Form("name"))
|
||||
assert.Equal(t, "joe@labstack.com", c.Form("email"))
|
||||
}
|
||||
|
||||
func testBind(t *testing.T, c *Context, ct string) {
|
||||
c.request.Header.Set(ContentType, ct)
|
||||
u := new(user)
|
||||
|
@ -16,8 +16,8 @@ func upload(c *echo.Context) error {
|
||||
// req.ParseMultipartForm(16 << 20) // Max memory 16 MiB
|
||||
|
||||
// Read form fields
|
||||
name := req.FormValue("name")
|
||||
email := req.FormValue("email")
|
||||
name := c.Form("name")
|
||||
email := c.Form("email")
|
||||
|
||||
// Read files
|
||||
files := req.MultipartForm.File["files"]
|
||||
|
@ -29,8 +29,8 @@ func upload(c *echo.Context) error {
|
||||
// req.ParseMultipartForm(16 << 20) // Max memory 16 MiB
|
||||
|
||||
// Read form fields
|
||||
name := req.FormValue("name")
|
||||
email := req.FormValue("email")
|
||||
name := c.Form("name")
|
||||
email := c.Form("email")
|
||||
|
||||
// Read files
|
||||
files := req.MultipartForm.File["files"]
|
||||
|
Loading…
Reference in New Issue
Block a user