forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
49 lines (42 loc) · 923 Bytes
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
}