-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handle_mask_test.go
81 lines (62 loc) · 1.48 KB
/
handle_mask_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 main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestHandlerMask(t *testing.T) {
tests := []struct {
path string
resp string
}{
{
path: "/1.2.3.4/32",
resp: `1.2.3.4/32
1. 2. 3. 4
00000001.00000010.00000011.00000100
XXXXXXXX.XXXXXXXX.XXXXXXXX.XXXXXXXX
1.2.3.4
`,
},
{
path: "/1.2.3.4/30",
resp: `1.2.3.4/30
1. 2. 3. 4
00000001.00000010.00000011.00000100
XXXXXXXX.XXXXXXXX.XXXXXXXX.XXXXXX
1.2.3.4
1.2.3.5
1.2.3.6
1.2.3.7
`,
},
}
srv := server{router: http.NewServeMux()}
srv.routes()
for _, tt := range tests {
req := httptest.NewRequest("GET", tt.path, nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, tt.resp, w.Body.String())
}
}
func TestHandlerMaskInvalidMask(t *testing.T) {
srv := server{router: http.NewServeMux()}
srv.routes()
req := httptest.NewRequest("GET", "/1.2.3.4/33", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
require.Equal(t, http.StatusBadRequest, w.Code)
require.Equal(t, "invalid mask: 33\n", w.Body.String())
}
func TestHandlerMaskInvalidCIDR(t *testing.T) {
srv := server{router: http.NewServeMux()}
srv.routes()
req := httptest.NewRequest("GET", "/256.0.0.0/32", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
require.Equal(t, http.StatusBadRequest, w.Code)
require.Equal(t, "invalid ip: 256.0.0.0\n", w.Body.String())
}