Repository navigation
Expand file tree
/
Copy pathblockpath_test.go
More file actions
157 lines (144 loc) · 3.6 KB
/
Copy pathblockpath_test.go
File metadata and controls
157 lines (144 loc) · 3.6 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
147
148
149
150
151
152
153
154
155
156
157
package plugin_blockpath
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestNew(t *testing.T) {
tests := []struct {
desc string
regexps []string
cfg *Config
next http.Handler
expErr bool
}{
{
desc: "should return no error",
regexps: []string{`^/foo/(.*)`},
cfg: nil,
next: http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}),
expErr: false,
},
{
desc: "should return an error",
regexps: []string{"*"},
cfg: nil,
next: http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}),
expErr: true,
},
{
desc: "should allow nil config",
regexps: nil,
cfg: nil,
next: http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}),
expErr: false,
},
{
desc: "should return an error on nil next",
regexps: []string{`^/foo/(.*)`},
cfg: nil,
next: nil,
expErr: true,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := test.cfg
if cfg == nil && test.regexps != nil {
cfg = &Config{
Regex: test.regexps,
}
}
_, err := New(context.Background(), test.next, cfg, "name")
if test.expErr && err == nil {
t.Errorf("expected constructor to return an error")
} else if !test.expErr && err != nil {
t.Fatalf("unexpected constructor error: %v", err)
}
})
}
}
func TestServeHTTP(t *testing.T) {
tests := []struct {
desc string
regexps []string
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",
regexps: nil,
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: "should block decoded path when request is percent encoded",
regexps: []string{`^/foo bar$`},
reqPath: "/foo%20bar",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
Regex: test.regexps,
}
nextCall := false
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
nextCall = true
})
handler, err := New(context.Background(), next, cfg, "blockpath")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://localhost"+test.reqPath, http.NoBody)
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)
}
})
}
}