-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathblockpath_test.go
More file actions
146 lines (132 loc) · 3.2 KB
/
blockpath_test.go
File metadata and controls
146 lines (132 loc) · 3.2 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package plugin_blockpath
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestNew(t *testing.T) {
tests := []struct {
desc string
regexps []string
code *int
expErr bool
}{
{
desc: "should return no error",
regexps: []string{`^/foo/(.*)`},
expErr: false,
},
{
desc: "should return an error",
regexps: []string{"*"},
expErr: true,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
Regex: test.regexps,
}
if _, err := New(context.Background(), nil, cfg, "name"); test.expErr && err == nil {
t.Errorf("expected error on bad regexp format")
}
})
}
}
func TestServeHTTP(t *testing.T) {
customCode := http.StatusNotFound
tests := []struct {
desc string
regexps []string
code *int
reqPath string
expNextCall bool
expStatusCode int
}{
{
desc: "should return forbidden status",
regexps: []string{"/test"},
reqPath: "/test",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return forbidden status",
regexps: []string{"/test", "/toto"},
reqPath: "/toto",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return ok status",
regexps: []string{"/test", "/toto"},
reqPath: "/plop",
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "should return ok status",
reqPath: "/test",
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "should return forbidden status",
regexps: []string{`^/bar(.*)`},
reqPath: "/bar/foo",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return forbidden status",
regexps: []string{`^/bar(.*)`},
reqPath: "/foo/bar",
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "custom status code (unmatched)",
regexps: []string{"^/api/woof"},
code: &customCode,
reqPath: "/foo/bar",
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "custom status code (matched)",
regexps: []string{"^/api/woof"},
code: &customCode,
reqPath: "/api/woof",
expNextCall: false,
expStatusCode: customCode,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
Regex: test.regexps,
Code: test.code,
}
nextCall := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
nextCall = true
})
handler, err := New(context.Background(), next, cfg, "blockpath")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
url := fmt.Sprintf("http://localhost%s", test.reqPath)
req := httptest.NewRequest(http.MethodGet, url, nil)
handler.ServeHTTP(recorder, req)
if nextCall != test.expNextCall {
t.Errorf("next handler should not be called")
}
if recorder.Result().StatusCode != test.expStatusCode {
t.Errorf("got status code %d, want %d", recorder.Code, test.expStatusCode)
}
})
}
}