forked from antchfx/antch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookies_test.go
49 lines (43 loc) · 1.2 KB
/
cookies_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 antch
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestCookiesHandler(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cookie, err := r.Cookie("Flavor"); err != nil {
http.SetCookie(w, &http.Cookie{Name: "Flavor", Value: "Chocolate Chip"})
} else {
cookie.Value = "Oatmeal Raisin"
http.SetCookie(w, cookie)
}
}))
defer ts.Close()
handler := CookiesMiddleware()(defaultMessageHandler())
req, _ := http.NewRequest("GET", ts.URL, nil)
resp, err := handler.Send(req)
if err != nil {
t.Fatal(err)
}
// After 1st request.
if len(resp.Cookies()) == 0 {
t.Fatal("no cookies data after 1st request")
}
cookie := resp.Cookies()[0]
if g, e := (cookie.Name + "=" + cookie.Value), "Flavor=Chocolate Chip"; e != g {
t.Errorf("expected %s; got %s after 1st request", e, g)
}
resp, err = handler.Send(req)
if err != nil {
t.Fatal(err)
}
// After 2nd request.
if len(resp.Cookies()) == 0 {
t.Fatal("no cookies data after 2nd request")
}
cookie = resp.Cookies()[0]
if g, e := (cookie.Name + "=" + cookie.Value), "Flavor=Oatmeal Raisin"; e != g {
t.Errorf("expected %s; got %s after 2nd request", e, g)
}
}