-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
132 lines (111 loc) · 4.02 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
initGlobal "github.com/twbworld/proxy/initialize/global"
"github.com/twbworld/proxy/initialize/system"
"github.com/twbworld/proxy/model/common"
"github.com/twbworld/proxy/router"
"github.com/twbworld/proxy/utils"
)
func TestMain(t *testing.T) {
gin.SetMode(gin.TestMode)
initGlobal.New().Start()
if err := system.DbStart(); err != nil {
t.Fatal("数据库连接失败[fsj09]", err)
}
defer func() {
time.Sleep(time.Second * 1) //给足够时间处理数据
system.DbClose()
}()
ginServer := gin.Default()
router.Start(ginServer)
//以下是有执行顺序的, 并且库提前有必要数据
testCases := [...]struct {
method string
postRes common.Response
getRes string
url string
status int
postData interface{}
contentType string
}{
{method: http.MethodGet, url: "http://clash.domain.com/test.html", getRes: `proxies: [{"name":"外网信息复杂_理智分辨真假_www.domain.com_443","type":"vless"`},
{method: http.MethodGet, url: "http://domain.com/test.html", getRes: utils.Base64Encode(`vless://[email protected]:443?encryption=none&headerType=none&sni=www.domain.com&fp=chrome&type=tcp&flow=xtls-rprx-vision&pbk=xxxx&sid=&security=reality#外网信息复杂_理智分辨真假_www.domain.com_443
vless://[email protected]:443?encryption=none&headerType=none&sni=www.domain.com&fp=chrome&type=ws&alpn=h2,http/1.1&host=www.domain.com&path=/vless-ws&security=tls#外网信息复杂_理智分辨真假_x.x.x.x_443
trojan://[email protected]:443?encryption=none&headerType=none&sni=www.domain.com&fp=chrome&type=ws&alpn=h2,http/1.1&host=www.domain.com&path=/trojan-go-ws/&security=tls#外网信息复杂_理智分辨真假_www.domain.com_443`)},
{method: http.MethodGet, url: "http://domain.com/aa.html", status: http.StatusMovedPermanently, getRes: `<a href="/404.html">`},
}
for k, value := range testCases {
t.Run(strconv.FormatInt(int64(k+1), 10)+value.url, func(t *testing.T) {
if value.method == "" {
value.method = http.MethodPost
}
if value.status == 0 {
value.status = 200
}
if value.method == http.MethodPost {
if value.contentType == "" {
value.contentType = "application/json"
}
if value.postRes == (common.Response{}) {
value.postRes.Code = 0
}
}
requestBody := new(bytes.Buffer)
if value.postData != nil {
if v, ok := value.postData.(*bytes.Buffer); ok {
requestBody = v
} else {
jsonVal, err := json.Marshal(value.postData)
if err != nil {
t.Fatal("json出错[godjg]", err)
}
requestBody = bytes.NewBuffer(jsonVal)
}
}
b := time.Now().UnixMilli()
//向注册的路有发起请求
req, err := http.NewRequest(value.method, value.url, requestBody)
if err != nil {
t.Fatal("请求出错[godkojg]", err)
}
if value.method == http.MethodPost {
req.Header.Set("content-type", value.contentType)
}
res := httptest.NewRecorder() // 构造一个记录
ginServer.ServeHTTP(res, req) //模拟http服务处理请求
result := res.Result() //response响应
fmt.Printf("^^^^^^处理用时%d毫秒^^^^^^\n", time.Now().UnixMilli()-b)
assert.Equal(t, value.status, result.StatusCode)
body, err := io.ReadAll(result.Body)
if err != nil {
t.Fatal(err)
}
defer result.Body.Close()
switch value.method {
case http.MethodPost:
var response common.Response
if err := json.Unmarshal(body, &response); err != nil {
t.Fatal("返回错误", err, string(body))
}
assert.Equal(t, value.postRes.Code, response.Code)
case http.MethodGet:
assert.Contains(t, string(body), value.getRes)
}
// fmt.Println("request!!!!!!!!!!", string(jsonVal))
// fmt.Println("response!!!!!!!!!!", utils.Base64Decode(string(body)))
// fmt.Println("response!!!!!!!!!!", string(body))
time.Sleep(time.Millisecond * 500) //!!!!!!!!!!!!!!!!!!
})
}
}