-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhitelist_test.go
More file actions
54 lines (40 loc) · 986 Bytes
/
whitelist_test.go
File metadata and controls
54 lines (40 loc) · 986 Bytes
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
package main
import (
"net/http"
"testing"
"github.com/zpencerq/goproxy"
)
func AssertDidNotPanic(t *testing.T, name string, f func()) {
defer func() {
if r := recover(); r != nil {
t.Errorf("%s did panic!", name)
}
}()
f()
}
func DummyReq(t *testing.T) *http.Request {
req, err := http.NewRequest("GET", "", nil)
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = "127.0.0.1:57862"
return req
}
func HandlerInput(r *http.Request) (*http.Request, *goproxy.ProxyCtx) {
return r, &goproxy.ProxyCtx{Req: r}
}
func TestReqHandler(t *testing.T) {
source := NewMemoryWhitelistLoader(
[]Entry{NewEntry("https://google.com")},
)
wm, _ := NewWhitelistManager(source)
r := DummyReq(t)
r.URL = nil
handler := wm.ReqHandler()
run := func() { handler(HandlerInput(r)) }
AssertDidNotPanic(t, "ReqHandler with nil URL", run)
_, resp := handler(HandlerInput(r))
if resp.StatusCode != 400 {
t.Errorf("nil URL response is not 400: %d", resp.StatusCode)
}
}