1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00
echo/context_test.go
Vishal Rana e8d7d88c4e Coverage bumped to 80%
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-03-30 20:54:38 -07:00

50 lines
923 B
Go

package echo
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestContext(t *testing.T) {
e := New()
e.Put("/users/:id", func(c *Context) {
u := new(user)
// Param
if c.Param("id") != "1" {
t.Error("param by name, id should be 1")
}
if c.P(0) != "1" {
t.Error("param by index, id should be 1")
}
// Store
c.Set("user", u.Name)
n := c.Get("user")
if n != u.Name {
t.Error("user name should be Joe")
}
// Bind & JSON
if c.Bind(u) {
c.JSON(http.StatusCreated, u)
}
// TODO: fix me later
c.Redirect(http.StatusMovedPermanently, "")
})
b, _ := json.Marshal(u)
w := httptest.NewRecorder()
r, _ := http.NewRequest("PUT", "/users/1", bytes.NewReader(b))
r.Header.Add(HeaderContentType, MIMEJSON)
e.ServeHTTP(w, r)
if w.Code != http.StatusCreated {
t.Errorf("status code should be 201, found %d", w.Code)
}
verifyUser(w.Body, t)
}