mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +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{}
|
||||
)
|
||||
|
||||
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.
|
||||
func (c *Context) P(i uint8) (value string) {
|
||||
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) {
|
||||
c.Response.reset(w)
|
||||
c.Request = r
|
||||
c.Response.reset(w)
|
||||
c.echo = e
|
||||
}
|
||||
|
@ -26,13 +26,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) *HTTPError
|
||||
func TestContext(t *testing.T) {
|
||||
b, _ := json.Marshal(u1)
|
||||
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
|
||||
c := &Context{
|
||||
Response: &Response{Writer: httptest.NewRecorder()},
|
||||
Request: r,
|
||||
pvalues: make([]string, 5),
|
||||
store: make(store),
|
||||
echo: New(),
|
||||
}
|
||||
c := NewContext(r, &Response{Writer: httptest.NewRecorder()}, New())
|
||||
|
||||
//------
|
||||
// Bind
|
||||
|
7
echo.go
7
echo.go
@ -113,12 +113,7 @@ func New() (e *Echo) {
|
||||
}
|
||||
e.Router = NewRouter(e)
|
||||
e.pool.New = func() interface{} {
|
||||
return &Context{
|
||||
Response: &Response{},
|
||||
pnames: make([]string, e.maxParam),
|
||||
pvalues: make([]string, e.maxParam),
|
||||
store: make(store),
|
||||
}
|
||||
return NewContext(nil, new(Response), e)
|
||||
}
|
||||
|
||||
//----------
|
||||
|
Loading…
Reference in New Issue
Block a user