-
Notifications
You must be signed in to change notification settings - Fork 5
/
regexpswitch_test.go
62 lines (54 loc) · 1.77 KB
/
regexpswitch_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
package httptools
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestRegexpSwitch_Routing(t *testing.T) {
rs := NewRegexpSwitch(map[string]http.Handler{
"/([a-z]+)/?": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Flag", "SET")
}),
})
rr := httptest.NewRecorder()
rs.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "/testpath/", nil)))
expected := []string{"SET"}
got := rr.HeaderMap["X-Flag"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
rr = httptest.NewRecorder()
rs.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "/!!!/", nil)))
expectedCode := http.StatusNotFound
if rr.Code != expectedCode {
t.Fatalf("Unexpected status code. Expected %d, got %d", expectedCode, rr.Code)
}
}
func TestRegexpSwitch_PatternPrecedence(t *testing.T) {
rs := NewRegexpSwitch(map[string]http.Handler{
"/.+": http.HandlerFunc(handlerA),
"/some/.+": http.HandlerFunc(handlerB),
})
rr := httptest.NewRecorder()
rs.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "/some/thing/bla", nil)))
expected := []string{"b"}
got := rr.HeaderMap["Handler"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}
func TestRegexpSwitch_TrailingSlash(t *testing.T) {
rs := NewRegexpSwitch(map[string]http.Handler{
"/(.+)": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Flag", "SET")
}),
})
rr := httptest.NewRecorder()
rs.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "/some/thing/", nil)))
expected := []string{"SET"}
got := rr.HeaderMap["X-Flag"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}