-
Notifications
You must be signed in to change notification settings - Fork 1
/
compose_test.go
73 lines (59 loc) · 1.75 KB
/
compose_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
package mux
import (
"github.com/nbio/st"
"gopkg.in/vinxi/utils.v0"
"net/http"
"net/url"
"testing"
)
func TestMuxComposeIfMatches(t *testing.T) {
mx := New()
mx.Use(If(Method("GET"), Host("foo.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
wrt := utils.NewWriterStub()
req := newRequest()
req.URL.Host = "foo.com"
mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "bar")
}
func TestMuxComposeIfUnmatch(t *testing.T) {
mx := New()
mx.Use(If(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
wrt := utils.NewWriterStub()
req := newRequest()
req.URL.Host = "foo.com"
mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "")
}
func TestMuxComposeOrMatch(t *testing.T) {
mx := New()
mx.Use(Or(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
wrt := utils.NewWriterStub()
req := newRequest()
req.URL.Host = "foo.com"
mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "bar")
}
func TestMuxComposeOrUnMatch(t *testing.T) {
mx := New()
mx.Use(Or(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
wrt := utils.NewWriterStub()
req := newRequest()
req.URL.Host = "foo.com"
mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "bar")
}
func newRequest() *http.Request {
return &http.Request{URL: &url.URL{}, Header: make(http.Header), Method: "GET"}
}