-
Notifications
You must be signed in to change notification settings - Fork 43
/
main_test.go
158 lines (128 loc) · 3.41 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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
)
type respData struct {
Code int `json:"code"`
Error string `json:"error"`
Mess string `json:"mess"`
Keywords []string `json:"keywords"`
Text string `json:"text"`
}
func Test_V1_Add_Fail(t *testing.T) {
postUrl := "http://127.0.0.1:8080/v1/balck_words"
resp, err := http.PostForm(postUrl, url.Values{})
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected HTTP status is 200, but get %d\n", resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
data := respData{}
json.Unmarshal(body, &data)
t.Log(string(body))
if data.Code != 0 {
t.Errorf("code: %d, error: %s, mess: %s", data.Code, data.Error, data.Mess)
}
}
func Test_V1_Add_Success(t *testing.T) {
postUrl := "http://127.0.0.1:8080/v1/balck_words"
resp, err := http.PostForm(postUrl, url.Values{"q": {"测试"}})
if err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected HTTP status is 200, but get %d\n", resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
data := respData{}
json.Unmarshal(body, &data)
t.Log(string(body))
if data.Code == 0 {
t.Errorf("code: %d, error: %s, mess: %s", data.Code, data.Error, data.Mess)
}
}
func Test_V1_Del_Success(t *testing.T) {
link := "http://127.0.0.1:8080/v1/balck_words"
r, err := http.NewRequest("DELETE", link, strings.NewReader(`{"q": "测试,test02"}`))
r.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(r)
if err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected HTTP status is 200, but get %d\n", resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
data := respData{}
json.Unmarshal(body, &data)
t.Log(string(body))
if data.Code == 0 {
t.Errorf("code: %d, error: %s, mess: %s", data.Code, data.Error, data.Mess)
}
}
func Test_V1_Query_Success_Without_Black_Words(t *testing.T) {
postUrl := "http://127.0.0.1:8080/v1/query"
resp, err := http.PostForm(postUrl, url.Values{"q": {"完全和谐的文本"}})
if err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected HTTP status is 200, but get %d\n", resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
data := respData{}
json.Unmarshal(body, &data)
if data.Code == 1 {
t.Errorf("code: %d, error: %s, mess: %s", data.Code, data.Error, data.Mess)
}
t.Log(data.Keywords)
t.Log(data.Text)
if len(data.Keywords) != 0 {
t.Errorf("过滤失败")
}
}
func Test_V1_Query_Success_With_Black_Words(t *testing.T) {
postUrl := "http://127.0.0.1:8080/v1/query"
resp, err := http.PostForm(postUrl, url.Values{"q": {"对于1989年诺贝尔和平奖有什么看法"}})
if err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected HTTP status is 200, but get %d\n", resp.StatusCode)
}
body, _ := ioutil.ReadAll(resp.Body)
data := respData{}
json.Unmarshal(body, &data)
if data.Code == 0 {
t.Errorf("code: %d, error: %s, mess: %s", data.Code, data.Error, data.Mess)
}
t.Log(data.Keywords)
t.Log(data.Text)
if data.Keywords[0] != "1989年诺贝尔和平奖" {
t.Errorf("过滤失败")
}
}