Skip to content

Commit

Permalink
Adding NewContext function to facilitate testing
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed May 8, 2015
1 parent 04cdefb commit b416efc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
13 changes: 12 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
8 changes: 1 addition & 7 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

//----------
Expand Down

0 comments on commit b416efc

Please sign in to comment.