mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
parent
2ad06ce67e
commit
9d11990cbb
@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,6 +20,7 @@ type (
|
|||||||
// Context represents context for the current request. It holds request and
|
// Context represents context for the current request. It holds request and
|
||||||
// response objects, path parameters, data and registered handler.
|
// response objects, path parameters, data and registered handler.
|
||||||
Context struct {
|
Context struct {
|
||||||
|
context.Context
|
||||||
request *http.Request
|
request *http.Request
|
||||||
response *Response
|
response *Response
|
||||||
socket *websocket.Conn
|
socket *websocket.Conn
|
||||||
|
@ -10,6 +10,8 @@ import (
|
|||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
@ -271,6 +273,12 @@ func TestContextForm(t *testing.T) {
|
|||||||
assert.Equal(t, "joe@labstack.com", c.Form("email"))
|
assert.Equal(t, "joe@labstack.com", c.Form("email"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextNetContext(t *testing.T) {
|
||||||
|
c := new(Context)
|
||||||
|
c.Context = context.WithValue(nil, "key", "val")
|
||||||
|
assert.Equal(t, "val", c.Value("key"))
|
||||||
|
}
|
||||||
|
|
||||||
func testBind(t *testing.T, c *Context, ct string) {
|
func testBind(t *testing.T, c *Context, ct string) {
|
||||||
c.request.Header.Set(ContentType, ct)
|
c.request.Header.Set(ContentType, ct)
|
||||||
u := new(user)
|
u := new(user)
|
||||||
|
@ -8,7 +8,8 @@ menu:
|
|||||||
|
|
||||||
### Handler path
|
### Handler path
|
||||||
|
|
||||||
`Context.Path()` returns the registered path for a handler, it can be used in the middleware for logging purpose.
|
`Context#Path()` returns the registered path for a handler, it can be used in the
|
||||||
|
middleware for logging purpose.
|
||||||
|
|
||||||
*Example*
|
*Example*
|
||||||
|
|
||||||
@ -22,10 +23,27 @@ e.Get("/users/:name", func(c *echo.Context) error) {
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### golang.org/x/net/context
|
||||||
|
|
||||||
|
`echo.Context` embeds `context.Context` interface, so all it's properties
|
||||||
|
are available right from `echo.Context`.
|
||||||
|
|
||||||
|
*Example*
|
||||||
|
|
||||||
|
```go
|
||||||
|
e.Get("/users/:name", func(c *echo.Context) error) {
|
||||||
|
c.Context = context.WithValue(nil, "key", "val")
|
||||||
|
// Pass it down...
|
||||||
|
// Use it...
|
||||||
|
println(c.Value("key"))
|
||||||
|
return c.String(http.StatusOK, name)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
### Path parameter
|
### Path parameter
|
||||||
|
|
||||||
Path parameter can be retrieved either by name `Context.Param(name string) string`
|
Path parameter can be retrieved either by name `Context#Param(name string) string`
|
||||||
or by index `Context.P(i int) string`. Getting parameter by index gives a slightly
|
or by index `Context#P(i int) string`. Getting parameter by index gives a slightly
|
||||||
better performance.
|
better performance.
|
||||||
|
|
||||||
*Example*
|
*Example*
|
||||||
@ -48,7 +66,7 @@ $ curl http://localhost:1323/users/joe
|
|||||||
|
|
||||||
### Query parameter
|
### Query parameter
|
||||||
|
|
||||||
Query parameter can be retrieved by name using `Context.Query(name string)`.
|
Query parameter can be retrieved by name using `Context#Query(name string)`.
|
||||||
|
|
||||||
*Example*
|
*Example*
|
||||||
|
|
||||||
@ -65,7 +83,7 @@ $ curl -G -d "name=joe" http://localhost:1323/users
|
|||||||
|
|
||||||
### Form parameter
|
### Form parameter
|
||||||
|
|
||||||
Form parameter can be retrieved by name using `Context.Form(name string)`.
|
Form parameter can be retrieved by name using `Context#Form(name string)`.
|
||||||
|
|
||||||
*Example*
|
*Example*
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user