2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
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, "")
|
|
|
|
})
|
2015-03-21 23:59:09 +02:00
|
|
|
|
2015-03-31 05:54:38 +02:00
|
|
|
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)
|
2015-03-21 23:59:09 +02:00
|
|
|
}
|