-
Notifications
You must be signed in to change notification settings - Fork 15
/
handler_test.go
60 lines (54 loc) · 1.43 KB
/
handler_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
50
51
52
53
54
55
56
57
58
59
60
package siesta
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestCompose(t *testing.T) {
key := "i"
stack := Compose(
func(c Context, w http.ResponseWriter, r *http.Request, quit func()) {
r.Header.Set(key, r.Header.Get(key)+"a")
i, _ := c.Get(key).(int)
c.Set(key, i+2)
},
func(c Context, w http.ResponseWriter, r *http.Request) {
r.Header.Set(key, r.Header.Get(key)+"b")
i, _ := c.Get(key).(int)
c.Set(key, i+4)
},
func(w http.ResponseWriter, r *http.Request, quit func()) {
r.Header.Set(key, r.Header.Get(key)+"c")
},
func(w http.ResponseWriter, r *http.Request) {
r.Header.Set(key, r.Header.Get(key)+"d")
},
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set(key, r.Header.Get(key)+"e")
}),
)
c := NewSiestaContext()
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
stack.ServeHTTPInContext(c, w, r)
i, _ := c.Get(key).(int)
if want, got := 6, i; want != got {
t.Errorf("expected %d got %d", want, got)
}
if want, got := "abcde", r.Header.Get(key); want != got {
t.Errorf("expected %s got %s", want, got)
}
}
func TestToContextHandlerUnsupportedHandler(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("expected a panic")
}
err, _ := r.(error)
if want, got := ErrUnsupportedHandler, err; want != got {
t.Fatalf("expected %v got %v", want, got)
}
}()
_ = ToContextHandler(func() {})
}