@marcel91200 / caddy Mirror · read-only / commits / ad3d4080
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

Add some tests and fix vet warning

Matthew Holt committed May 7, 2019 at 10:15 UTC ad3d408067ee155a75e33a59ef02f547a943b94e
2 files changed +146 -38
internal/caddyscript/lib/regex.go
+1 -1
@@ -45,6 +45,6 @@ func (r Regexp) MatchString(thread *starlark.Thread, fn *starlark.Builtin, args
45
46 func (r Regexp) Freeze() {}
47 func (r Regexp) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: Regexp") }
48 -func (r Regexp) String() string { return fmt.Sprint(r) }
48 +func (r Regexp) String() string { return "Regexp" }
49 func (r Regexp) Type() string { return "Regexp" }
50 func (r Regexp) Truth() starlark.Bool { return true }
modules/caddyhttp/caddyhttp_test.go
+145 -37
@@ -5,73 +5,181 @@ import (
5 "testing"
6 )
7
8 +func TestSplitListenerAddr(t *testing.T) {
9 + for i, tc := range []struct {
10 + input string
11 + expectNetwork string
12 + expectHost string
13 + expectPort string
14 + expectErr bool
15 + }{
16 + {
17 + input: "",
18 + expectErr: true,
19 + },
20 + {
21 + input: "foo",
22 + expectErr: true,
23 + },
24 + {
25 + input: "foo:1234",
26 + expectHost: "foo",
27 + expectPort: "1234",
28 + },
29 + {
30 + input: "foo:1234-5678",
31 + expectHost: "foo",
32 + expectPort: "1234-5678",
33 + },
34 + {
35 + input: "udp/foo:1234",
36 + expectNetwork: "udp",
37 + expectHost: "foo",
38 + expectPort: "1234",
39 + },
40 + {
41 + input: "tcp6/foo:1234-5678",
42 + expectNetwork: "tcp6",
43 + expectHost: "foo",
44 + expectPort: "1234-5678",
45 + },
46 + {
47 + input: "udp/",
48 + expectNetwork: "udp",
49 + expectErr: true,
50 + },
51 + } {
52 + actualNetwork, actualHost, actualPort, err := splitListenAddr(tc.input)
53 + if tc.expectErr && err == nil {
54 + t.Errorf("Test %d: Expected error but got: %v", i, err)
55 + }
56 + if !tc.expectErr && err != nil {
57 + t.Errorf("Test %d: Expected no error but got: %v", i, err)
58 + }
59 + if actualNetwork != tc.expectNetwork {
60 + t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork)
61 + }
62 + if actualHost != tc.expectHost {
63 + t.Errorf("Test %d: Expected host '%s' but got '%s'", i, tc.expectHost, actualHost)
64 + }
65 + if actualPort != tc.expectPort {
66 + t.Errorf("Test %d: Expected port '%s' but got '%s'", i, tc.expectPort, actualPort)
67 + }
68 + }
69 +}
70 +
71 +func TestJoinListenerAddr(t *testing.T) {
72 + for i, tc := range []struct {
73 + network, host, port string
74 + expect string
75 + }{
76 + {
77 + network: "", host: "", port: "",
78 + expect: "",
79 + },
80 + {
81 + network: "tcp", host: "", port: "",
82 + expect: "tcp/",
83 + },
84 + {
85 + network: "", host: "foo", port: "",
86 + expect: "foo",
87 + },
88 + {
89 + network: "", host: "", port: "1234",
90 + expect: ":1234",
91 + },
92 + {
93 + network: "", host: "", port: "1234-5678",
94 + expect: ":1234-5678",
95 + },
96 + {
97 + network: "", host: "foo", port: "1234",
98 + expect: "foo:1234",
99 + },
100 + {
101 + network: "udp", host: "foo", port: "1234",
102 + expect: "udp/foo:1234",
103 + },
104 + {
105 + network: "udp", host: "", port: "1234",
106 + expect: "udp/:1234",
107 + },
108 + } {
109 + actual := joinListenAddr(tc.network, tc.host, tc.port)
110 + if actual != tc.expect {
111 + t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual)
112 + }
113 + }
114 +}
115 +
116 func TestParseListenerAddr(t *testing.T) {
117 for i, tc := range []struct {
10 - input string
11 - expectProto string
12 - expectAddrs []string
13 - expectErr bool
118 + input string
119 + expectNetwork string
120 + expectAddrs []string
121 + expectErr bool
122 }{
123 {
16 - input: "",
17 - expectProto: "tcp",
18 - expectErr: true,
124 + input: "",
125 + expectNetwork: "tcp",
126 + expectErr: true,
127 },
128 {
21 - input: ":",
22 - expectProto: "tcp",
23 - expectErr: true,
129 + input: ":",
130 + expectNetwork: "tcp",
131 + expectErr: true,
132 },
133 {
26 - input: ":1234",
27 - expectProto: "tcp",
28 - expectAddrs: []string{":1234"},
134 + input: ":1234",
135 + expectNetwork: "tcp",
136 + expectAddrs: []string{":1234"},
137 },
138 {
31 - input: "tcp/:1234",
32 - expectProto: "tcp",
33 - expectAddrs: []string{":1234"},
139 + input: "tcp/:1234",
140 + expectNetwork: "tcp",
141 + expectAddrs: []string{":1234"},
142 },
143 {
36 - input: "tcp6/:1234",
37 - expectProto: "tcp6",
38 - expectAddrs: []string{":1234"},
144 + input: "tcp6/:1234",
145 + expectNetwork: "tcp6",
146 + expectAddrs: []string{":1234"},
147 },
148 {
41 - input: "tcp4/localhost:1234",
42 - expectProto: "tcp4",
43 - expectAddrs: []string{"localhost:1234"},
149 + input: "tcp4/localhost:1234",
150 + expectNetwork: "tcp4",
151 + expectAddrs: []string{"localhost:1234"},
152 },
153 {
46 - input: "unix/localhost:1234-1236",
47 - expectProto: "unix",
48 - expectAddrs: []string{"localhost:1234", "localhost:1235", "localhost:1236"},
154 + input: "unix/localhost:1234-1236",
155 + expectNetwork: "unix",
156 + expectAddrs: []string{"localhost:1234", "localhost:1235", "localhost:1236"},
157 },
158 {
51 - input: "localhost:1234-1234",
52 - expectProto: "tcp",
53 - expectAddrs: []string{"localhost:1234"},
159 + input: "localhost:1234-1234",
160 + expectNetwork: "tcp",
161 + expectAddrs: []string{"localhost:1234"},
162 },
163 {
56 - input: "localhost:2-1",
57 - expectProto: "tcp",
58 - expectErr: true,
164 + input: "localhost:2-1",
165 + expectNetwork: "tcp",
166 + expectErr: true,
167 },
168 {
61 - input: "localhost:0",
62 - expectProto: "tcp",
63 - expectAddrs: []string{"localhost:0"},
169 + input: "localhost:0",
170 + expectNetwork: "tcp",
171 + expectAddrs: []string{"localhost:0"},
172 },
173 } {
66 - actualProto, actualAddrs, err := parseListenAddr(tc.input)
174 + actualNetwork, actualAddrs, err := parseListenAddr(tc.input)
175 if tc.expectErr && err == nil {
176 t.Errorf("Test %d: Expected error but got: %v", i, err)
177 }
178 if !tc.expectErr && err != nil {
179 t.Errorf("Test %d: Expected no error but got: %v", i, err)
180 }
73 - if actualProto != tc.expectProto {
74 - t.Errorf("Test %d: Expeceted protocol '%s' but got '%s'", i, tc.expectProto, actualProto)
181 + if actualNetwork != tc.expectNetwork {
182 + t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork)
183 }
184 if !reflect.DeepEqual(tc.expectAddrs, actualAddrs) {
185 t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddrs, actualAddrs)