-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
309 lines (291 loc) · 7.16 KB
/
main_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"mini-http/static"
"net/http"
"os"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var port = 32000
var currentDir string
type testRequest struct {
url string
response string
status int
allowErr bool
}
type testCase struct {
label string
enableSelfSignedSSL bool
ca string
cert string
key string
args []string
requests []testRequest
}
func (c *testCase) Run(t *testing.T) {
fmt.Println("================== Start Test " + c.label + " ==================")
t.Run(c.label, func(t *testing.T) {
var ports []string
var httpPort int
var httpsPort int
port++
httpPort = port
ports = append(ports, "--port", fmt.Sprintf("%d", httpPort))
if c.cert != "" && c.key != "" {
port++
httpsPort = port
ports = append(ports, "--https-port", fmt.Sprintf("%d", httpsPort), "--cert", c.cert, "--key", c.key)
} else if c.enableSelfSignedSSL {
port++
httpsPort = port
ports = append(ports, "--https-port", fmt.Sprintf("%d", httpsPort))
}
args := append(ports, c.args...)
err := static.RunServer(args)
if err != nil {
t.Fatal(err)
}
for _, request := range c.requests {
url := fmt.Sprintf(request.url, httpPort)
if strings.Contains(request.url, "https") {
url = fmt.Sprintf(request.url, httpsPort)
}
content, status, err := get(url, c.ca)
if err != nil {
if request.allowErr {
continue
}
t.Error(err)
continue
}
if request.response != "" {
assert.Equal(t, request.response, content)
}
if request.status != 0 {
assert.Equal(t, request.status, status)
}
}
})
fmt.Println("================== End Test " + c.label + " ==================")
}
func getSelfSignedCertDir() string {
userDir, err := os.UserConfigDir()
if err != nil {
userDir = "/tmp/"
}
dir := path.Join(userDir, "ikrong/mini-http")
return dir
}
func get(url string, cert string) (content string, status int, err error) {
transport := &http.Transport{}
if strings.Contains(url, "https") {
if cert == "" {
cert = path.Join(getSelfSignedCertDir(), "root.crt")
}
var certContent []byte
certContent, err = os.ReadFile(cert)
if err == nil {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(certContent)
transport.TLSClientConfig = &tls.Config{RootCAs: caCertPool}
}
}
client := &http.Client{Transport: transport}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
response, err := client.Do(request)
if err != nil {
return
}
defer response.Body.Close()
status = response.StatusCode
body, err := io.ReadAll(response.Body)
if err != nil {
return
}
content = string(body)
return
}
func Test(t *testing.T) {
var testCaseList = []testCase{
{
label: "Test Default Server",
args: []string{
"--root", fmt.Sprintf("%s/assets/", currentDir),
"--not-found", fmt.Sprintf("%s/assets/domain/example.net/index.html", currentDir),
},
requests: []testRequest{
{
url: "http://localhost:%d",
status: http.StatusOK,
},
{
url: "http://localhost:%d/404/",
status: http.StatusNotFound,
response: "example.net",
},
},
},
{
label: "Test Domain Server",
args: []string{
"--domain", "localhost",
"--root", fmt.Sprintf("%s/assets/domain/localhost/", currentDir),
},
requests: []testRequest{
{
url: "http://localhost:%d",
status: http.StatusOK,
response: "localhost",
},
{
url: "http://127.0.0.1:%d",
status: http.StatusOK,
response: "localhost",
},
},
},
{
label: "Test Https Server",
args: []string{
"--domain", "localhost",
"--root", fmt.Sprintf("%s/assets/domain/localhost/", currentDir),
"--cert", fmt.Sprintf("%s/assets/cert/server_cert.crt", currentDir),
"--key", fmt.Sprintf("%s/assets/cert/server_cert.key", currentDir),
},
cert: fmt.Sprintf("%s/assets/cert/server_cert.crt", currentDir),
key: fmt.Sprintf("%s/assets/cert/server_cert.key", currentDir),
ca: fmt.Sprintf("%s/assets/cert/root_ca.crt", currentDir),
requests: []testRequest{
{
url: "https://localhost:%d",
status: http.StatusOK,
response: "localhost",
},
},
},
{
label: "Test Gzip",
args: []string{
"--domain", "localhost",
"--root", fmt.Sprintf("%s/assets/domain/localhost/", currentDir),
},
requests: []testRequest{
{
url: "http://localhost:%d/gzip.html",
status: http.StatusOK,
response: "gzip",
},
},
},
{
label: "Test Single Page Routing",
args: []string{
"--domain", "localhost",
"--root", fmt.Sprintf("%s/assets/domain/localhost/", currentDir),
"--mode", "history",
},
requests: []testRequest{
{
url: "http://localhost:%d/",
status: http.StatusOK,
response: "localhost",
},
{
url: "http://localhost:%d/a/b/c",
status: http.StatusOK,
response: "localhost",
},
},
},
{
label: "Test Proxy",
args: []string{
"--domain", "localhost",
"--root", fmt.Sprintf("%s/assets/domain/localhost/", currentDir),
"--proxy", "/proxy/gen_204:http://connectivitycheck.gstatic.com/generate_204",
"--proxy", "/proxy/another/a/b/c/gen_204:http://connectivitycheck.gstatic.com/generate_204",
"--proxy", "/proxy/gstatic:http://connectivitycheck.gstatic.com",
},
requests: []testRequest{
{
url: "http://localhost:%d/proxy/gen_204",
status: http.StatusNoContent,
response: "",
},
{
url: "http://localhost:%d/proxy/another/a/b/c/gen_204",
status: http.StatusNoContent,
response: "",
},
{
url: "http://localhost:%d/proxy/gstatic/generate_204",
status: http.StatusNoContent,
response: "",
},
},
},
{
label: "Test Multiple Domains",
args: []string{
"--domain", "example.net",
"--root", fmt.Sprintf("%s/assets/domain/example.net/", currentDir),
"--domain", "localhost",
"--root", fmt.Sprintf("%s/assets/domain/localhost/", currentDir),
},
requests: []testRequest{
{
url: "http://127.0.0.1:%d",
status: http.StatusOK,
response: "example.net",
},
{
url: "http://localhost:%d",
status: http.StatusOK,
response: "localhost",
},
},
},
{
label: "Test SelfSigned Https Server",
enableSelfSignedSSL: true,
args: []string{
"--domain", "localhost",
"--root", fmt.Sprintf("%s/assets/domain/localhost/", currentDir),
},
requests: []testRequest{
{
// 首次访问,CA证书还没生成,所以客户端无法受信,会出错
url: "https://localhost:%d",
allowErr: true,
},
{
url: "https://localhost:%d",
status: http.StatusOK,
response: "localhost",
},
},
},
}
for _, c := range testCaseList {
c.Run(t)
}
}
func TestMain(m *testing.M) {
// 获取当前文件夹
currentDir, _ = os.Getwd()
selfSignedCertDir := getSelfSignedCertDir()
if _, err := os.Stat(selfSignedCertDir); err == nil {
os.RemoveAll(selfSignedCertDir)
}
os.Exit(m.Run())
}