forked from goji/goji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_test.go
99 lines (80 loc) · 1.83 KB
/
handle_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package goji
import (
"net/http"
"testing"
"golang.org/x/net/context"
)
func TestHandle(t *testing.T) {
t.Parallel()
m := NewMux()
called := false
fn := func(w http.ResponseWriter, r *http.Request) {
called = true
}
m.Handle(boolPattern(true), http.HandlerFunc(fn))
w, r := wr()
m.ServeHTTPC(context.Background(), w, r)
if !called {
t.Error("expected handler to be called")
}
}
func TestHandleFunc(t *testing.T) {
t.Parallel()
m := NewMux()
called := false
fn := func(w http.ResponseWriter, r *http.Request) {
called = true
}
m.HandleFunc(boolPattern(true), fn)
w, r := wr()
m.ServeHTTPC(context.Background(), w, r)
if !called {
t.Error("expected handler to be called")
}
}
func TestHandleC(t *testing.T) {
t.Parallel()
m := NewMux()
called := false
fn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
called = true
}
m.HandleC(boolPattern(true), HandlerFunc(fn))
w, r := wr()
m.ServeHTTPC(context.Background(), w, r)
if !called {
t.Error("expected handler to be called")
}
}
func TestHandleFuncC(t *testing.T) {
t.Parallel()
m := NewMux()
called := false
fn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
called = true
}
m.HandleFuncC(boolPattern(true), fn)
w, r := wr()
m.ServeHTTPC(context.Background(), w, r)
if !called {
t.Error("expected handler to be called")
}
}
type cHTTP chan string
func (c cHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c <- "http"
}
func (c cHTTP) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) {
c <- "context"
}
func TestHandleUpgrade(t *testing.T) {
t.Parallel()
ch := make(cHTTP, 1)
m := NewMux()
m.Handle(boolPattern(true), ch)
w, r := wr()
m.ServeHTTPC(context.Background(), w, r)
if v := <-ch; v != "context" {
t.Errorf("got %v, expected %v", v, "context")
}
}