-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
146 lines (110 loc) · 3.27 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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"tastien.com/chat-bot/bot"
)
func TestPingRoute(t *testing.T) {
cfg := loadTestConfig(t)
_, r := CreateApp(cfg)
// 创建一个模拟的 HTTP 请求
req, err := http.NewRequest(http.MethodGet, "/ping", nil)
if err != nil {
t.Fatal(err)
}
// 使用 httptest 包创建一个 ResponseRecorder 来记录响应
w := httptest.NewRecorder()
// 使用 Gin 的 ServeHTTP 方法来处理请求和记录响应
r.ServeHTTP(w, req)
// 检查响应状态码
assert.Equal(t, http.StatusOK, w.Code)
// 检查响应体
assert.Equal(t, "{\"message\":\"pong\"}", w.Body.String())
}
func TestWebhookRoute(t *testing.T) {
// 加载测试数据文件
file, err := os.Open("message.example.json")
if err != nil {
t.Fatal(err)
}
defer file.Close()
cfg := loadTestConfig(t)
_, r := CreateApp(cfg)
// 创建一个模拟的 HTTP 请求
req, err := http.NewRequest(http.MethodPost, "/webhook/event", file)
if err != nil {
t.Fatal(err)
}
// 使用 httptest 包创建一个 ResponseRecorder 来记录响应
w := httptest.NewRecorder()
// 使用 Gin 的 ServeHTTP 方法来处理请求和记录响应
r.ServeHTTP(w, req)
// 检查响应状态码
assert.Equal(t, http.StatusOK, w.Code)
// 检查响应体
assert.Equal(t, "{\"msg\":\"success\"}", w.Body.String())
}
func TestWebhookRouteChallenge(t *testing.T) {
cfg := loadTestConfig(t)
_, r := CreateApp(cfg)
body := map[string]string{
"challenge": "ajls384kdjx98XX", // 应用需要在响应中原样返回的值
"token": cfg.FeishuAppVerificationToken, // 即 Verification Token
"type": "url_verification", // 表示这是一个验证请求
}
data, _ := json.Marshal(body)
// 创建一个模拟的 HTTP 请求
req, err := http.NewRequest(http.MethodPost, "/webhook/event", strings.NewReader(string(data)))
if err != nil {
t.Fatal(err)
}
// 使用 httptest 包创建一个 ResponseRecorder 来记录响应
w := httptest.NewRecorder()
// 使用 Gin 的 ServeHTTP 方法来处理请求和记录响应
r.ServeHTTP(w, req)
// 检查响应状态码
assert.Equal(t, http.StatusOK, w.Code)
// 检查响应体
assert.Equal(t, "{\"challenge\":\"ajls384kdjx98XX\"}", w.Body.String())
}
func loadTestConfig(t *testing.T) *bot.Config {
cfg := &bot.Config{}
cfgFile, err := os.Open("config.test.json")
if err != nil {
t.Fatal(err)
}
defer cfgFile.Close()
if data, err := ioutil.ReadAll(cfgFile); err == nil {
json.Unmarshal(data, cfg)
}
return cfg
}
func TestWebHookAddUser(t *testing.T) {
// 加载测试数据文件
file, err := os.Open("userAdded.example.json")
if err != nil {
t.Fatal(err)
}
defer file.Close()
cfg := loadTestConfig(t)
_, r := CreateApp(cfg)
// 创建一个模拟的 HTTP 请求
req, err := http.NewRequest(http.MethodPost, "/webhook/event", file)
if err != nil {
t.Fatal(err)
}
// 使用 httptest 包创建一个 ResponseRecorder 来记录响应
w := httptest.NewRecorder()
// 使用 Gin 的 ServeHTTP 方法来处理请求和记录响应
r.ServeHTTP(w, req)
// 检查响应状态码
assert.Equal(t, http.StatusOK, w.Code)
// 检查响应体
assert.Equal(t, "{\"msg\":\"success\"}", w.Body.String())
}