-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
blackwords_test.go
84 lines (68 loc) · 1.75 KB
/
blackwords_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
83
84
package rest
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBlackwords(t *testing.T) {
tbl := []struct {
inp string
code int
}{
{"", 200},
{"blah blah body", 200},
{"blah blah body bad1", 403},
{"blah blah body bad", 200},
{"blah bad2 body bad", 403},
{`{"fld": 123, "aa": {"$where": {"aaa": 567}}}`, 403},
}
bwMiddleware := BlackWords("bad1", "bad2", "$where")
ts := httptest.NewServer(bwMiddleware(getTestHandlerBlah()))
defer ts.Close()
u := fmt.Sprintf("%s%s", ts.URL, "/something")
client := http.Client{Timeout: 5 * time.Second}
for n, tt := range tbl {
tt := tt
t.Run(fmt.Sprintf("test-%d", n), func(t *testing.T) {
req, err := http.NewRequest("GET", u, bytes.NewBuffer([]byte(tt.inp)))
assert.Nil(t, err)
r, err := client.Do(req)
assert.Nil(t, err)
assert.Equal(t, tt.code, r.StatusCode)
})
}
}
func TestBlackwordsFn(t *testing.T) {
tbl := []struct {
inp string
code int
}{
{"", 200},
{"blah blah body", 200},
{"blah blah body bad1", 403},
{"blah blah body bad", 200},
{"blah bad2 body bad", 403},
{`{"fld": 123, "aa": {"$where": {"aaa": 567}}}`, 403},
}
bwMiddleware := BlackWordsFn(func() []string {
return []string{"bad1", "bad2", "$where"}
})
ts := httptest.NewServer(bwMiddleware(getTestHandlerBlah()))
defer ts.Close()
u := fmt.Sprintf("%s%s", ts.URL, "/something")
client := http.Client{Timeout: 5 * time.Second}
for n, tt := range tbl {
tt := tt
t.Run(fmt.Sprintf("test-%d", n), func(t *testing.T) {
req, err := http.NewRequest("GET", u, bytes.NewBuffer([]byte(tt.inp)))
assert.Nil(t, err)
r, err := client.Do(req)
assert.Nil(t, err)
assert.Equal(t, tt.code, r.StatusCode)
})
}
}