1
0
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:
Vishal Rana 2015-05-08 11:52:06 -07:00
parent 04cdefb2a6
commit b416efc71c
3 changed files with 14 additions and 14 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)
}
//----------