-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_test.go
74 lines (58 loc) · 1.74 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package coco
import (
"net/http/httptest"
"reflect"
"testing"
)
func Test_context_coco(t *testing.T) {
app := &App{}
rc := &context{app: app}
if got := rc.coco(); got != app {
t.Errorf("rcontext.coco() = %v, want %v", got, app)
}
}
func Test_context_next_noHandlers(t *testing.T) {
rc := &context{handlers: []Handler{}}
rw := Response{ww: wrapWriter(httptest.NewRecorder())}
req := &Request{r: httptest.NewRequest("GET", "/", nil)}
rc.next(rw, req)
if len(rc.handlers) != 0 {
t.Error("Expected no handlers to be removed from the slice")
}
}
func Test_context_next_oneHandler(t *testing.T) {
called := false
handler := func(rw Response, req *Request, next NextFunc) {
called = true
}
rc := &context{handlers: []Handler{handler}}
rw := Response{ww: wrapWriter(httptest.NewRecorder())}
req := &Request{r: httptest.NewRequest("GET", "/", nil)}
rc.next(rw, req)
if !called {
t.Error("Expected the handler to be called")
}
if len(rc.handlers) != 0 {
t.Error("Expected the handler to be removed from the slice")
}
}
func Test_context_next_multipleHandlers(t *testing.T) {
var callOrder []int
handler1 := func(rw Response, req *Request, next NextFunc) {
callOrder = append(callOrder, 1)
next(rw, req)
}
handler2 := func(rw Response, req *Request, next NextFunc) {
callOrder = append(callOrder, 2)
}
rc := &context{handlers: []Handler{handler1, handler2}}
rw := Response{ww: wrapWriter(httptest.NewRecorder())}
req := &Request{r: httptest.NewRequest("GET", "/", nil)}
rc.next(rw, req)
if !reflect.DeepEqual(callOrder, []int{1, 2}) {
t.Errorf("Handlers were called in the wrong order: got %v, want %v", callOrder, []int{1, 2})
}
if len(rc.handlers) != 0 {
t.Error("Expected all handlers to be removed from the slice")
}
}