mirror of
https://github.com/labstack/echo.git
synced 2025-04-23 12:18:53 +02:00
Adding NewContext function to facilitate testing
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
04cdefb2a6
commit
b416efc71c
13
context.go
13
context.go
@ -19,6 +19,17 @@ type (
|
|||||||
store map[string]interface{}
|
store map[string]interface{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewContext(req *http.Request, res *Response, e *Echo) *Context {
|
||||||
|
return &Context{
|
||||||
|
Request: req,
|
||||||
|
Response: res,
|
||||||
|
echo: e,
|
||||||
|
pnames: make([]string, e.maxParam),
|
||||||
|
pvalues: make([]string, e.maxParam),
|
||||||
|
store: make(store),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// P returns path parameter by index.
|
// P returns path parameter by index.
|
||||||
func (c *Context) P(i uint8) (value string) {
|
func (c *Context) P(i uint8) (value string) {
|
||||||
l := uint8(len(c.pnames))
|
l := uint8(len(c.pnames))
|
||||||
@ -114,7 +125,7 @@ func (c *Context) Redirect(code int, url string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) reset(w http.ResponseWriter, r *http.Request, e *Echo) {
|
func (c *Context) reset(w http.ResponseWriter, r *http.Request, e *Echo) {
|
||||||
c.Response.reset(w)
|
|
||||||
c.Request = r
|
c.Request = r
|
||||||
|
c.Response.reset(w)
|
||||||
c.echo = e
|
c.echo = e
|
||||||
}
|
}
|
||||||
|
@ -26,13 +26,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) *HTTPError
|
|||||||
func TestContext(t *testing.T) {
|
func TestContext(t *testing.T) {
|
||||||
b, _ := json.Marshal(u1)
|
b, _ := json.Marshal(u1)
|
||||||
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
|
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
|
||||||
c := &Context{
|
c := NewContext(r, &Response{Writer: httptest.NewRecorder()}, New())
|
||||||
Response: &Response{Writer: httptest.NewRecorder()},
|
|
||||||
Request: r,
|
|
||||||
pvalues: make([]string, 5),
|
|
||||||
store: make(store),
|
|
||||||
echo: New(),
|
|
||||||
}
|
|
||||||
|
|
||||||
//------
|
//------
|
||||||
// Bind
|
// Bind
|
||||||
|
7
echo.go
7
echo.go
@ -113,12 +113,7 @@ func New() (e *Echo) {
|
|||||||
}
|
}
|
||||||
e.Router = NewRouter(e)
|
e.Router = NewRouter(e)
|
||||||
e.pool.New = func() interface{} {
|
e.pool.New = func() interface{} {
|
||||||
return &Context{
|
return NewContext(nil, new(Response), e)
|
||||||
Response: &Response{},
|
|
||||||
pnames: make([]string, e.maxParam),
|
|
||||||
pvalues: make([]string, e.maxParam),
|
|
||||||
store: make(store),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------
|
//----------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user