forked from rcrowley/go-tigertonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
82 lines (71 loc) · 2.11 KB
/
middleware_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
package tigertonic
import (
"errors"
"net/http"
"testing"
)
func TestFirst1(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
First(NotFoundHandler{}).ServeHTTP(w, r)
if http.StatusNotFound != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestFirst2(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
First(noopHandler{}, NotFoundHandler{}).ServeHTTP(w, r)
if http.StatusNotFound != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestFirst3(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
First(noopHandler{}, noopHandler{}, NotFoundHandler{}).ServeHTTP(w, r)
if http.StatusNotFound != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestFirst4(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
First(NotFoundHandler{}, &fatalHandler{t}).ServeHTTP(w, r)
if http.StatusNotFound != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestIfFalse(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
If(func(r *http.Request) (http.Header, error) {
return http.Header{
"WWW-Authenticate": []string{"Basic realm=\"Tiger Tonic\""},
}, Unauthorized{errors.New("Unauthorized")}
}, NotFoundHandler{}).ServeHTTP(w, r)
if http.StatusUnauthorized != w.StatusCode {
t.Fatal(w.StatusCode)
}
if wwwAuthenticate := w.Header().Get("WWW-Authenticate"); "Basic realm=\"Tiger Tonic\"" != wwwAuthenticate {
t.Fatal(w.Header())
}
}
func TestIfTrue(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/", nil)
If(func(r *http.Request) (http.Header, error) {
return nil, nil
}, NotFoundHandler{}).ServeHTTP(w, r)
if http.StatusNotFound != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
type fatalHandler struct {
t *testing.T
}
func (fh *fatalHandler) ServeHTTP(http.ResponseWriter, *http.Request) {
fh.t.Fatal("fatalHandler")
}
type noopHandler struct{}
func (noopHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}