-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsrebinding_test.go
81 lines (60 loc) · 1.92 KB
/
dnsrebinding_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
package dnsrebinding
import (
"net/http"
"net/http/httptest"
"testing"
)
// a simple handler
var testHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.Write([]byte("test"))
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
})
func assertResponse(t *testing.T, res *httptest.ResponseRecorder, responseCode int) {
if responseCode != res.Code {
t.Errorf("expected response code to be %d but got %d. ", responseCode, res.Code)
}
}
func TestBadHost(t *testing.T) {
f := Filter(http.StatusNotImplemented, "example.com")
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
req.Header.Add("Host", "malicius.com")
f(testHandler).ServeHTTP(res, req)
assertResponse(t, res, http.StatusNotImplemented)
}
func TestGoodHost(t *testing.T) {
f := Filter(http.StatusNotImplemented, "example.com")
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
req.Header.Add("Host", "example.com")
f(testHandler).ServeHTTP(res, req)
assertResponse(t, res, 200)
}
func TestSomeGoodHost(t *testing.T) {
f := Filter(http.StatusNotImplemented, "example.com", "foo.com", "bar.com")
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://foo.com/foo", nil)
req.Header.Add("Host", "foo.com")
f(testHandler).ServeHTTP(res, req)
assertResponse(t, res, 200)
}
func TestSomeBadHost(t *testing.T) {
f := Filter(http.StatusNotImplemented, "example.com", "foo.com", "bar.com")
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://foo.com/bar", nil)
req.Header.Add("Host", "malicius.com")
f(testHandler).ServeHTTP(res, req)
assertResponse(t, res, http.StatusNotImplemented)
}
func TestNoHost(t *testing.T) {
defer func() {
if r := recover(); r == nil {
// panic it's ok
t.Log("Recover from panic due to empty hostname")
}
}()
Filter(http.StatusNotImplemented, "")
}