Skip to content

Commit c44b2df

Browse files
authored
Merge pull request #6 from golang-io/dev
update: add test case and some comment
2 parents cd770df + f9848d2 commit c44b2df

16 files changed

+2089
-206
lines changed

api_test.go

Lines changed: 195 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,207 @@
1-
package requests_test
1+
package requests
22

33
import (
44
"io"
55
"net/http"
66
"net/http/httptest"
7-
"os"
7+
"net/url"
8+
"strings"
89
"testing"
910
)
1011

11-
var ss = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12-
_, _ = io.Copy(w, r.Body)
13-
}))
12+
func setupTestServer() *httptest.Server {
13+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14+
w.Header().Set("Content-Type", "application/json")
15+
w.Header().Set("X-Request-Method", r.Method)
16+
w.Header().Set("X-Request-URL", r.URL.String())
1417

15-
func TestMain(m *testing.M) {
18+
if r.Method == "HEAD" {
19+
w.WriteHeader(http.StatusOK)
20+
return
21+
}
1622

17-
os.Exit(m.Run())
23+
body, _ := io.ReadAll(r.Body)
24+
w.WriteHeader(http.StatusOK)
25+
w.Write([]byte(`{"method":"` + r.Method + `","url":"` + r.URL.String() + `","body":"` + string(body) + `"}`))
26+
}))
27+
}
28+
29+
func TestGet(t *testing.T) {
30+
server := setupTestServer()
31+
defer server.Close()
32+
33+
resp, err := Get(server.URL + "/get?param=value")
34+
if err != nil {
35+
t.Fatalf("Get 请求失败: %v", err)
36+
}
37+
defer resp.Body.Close()
38+
39+
if resp.StatusCode != http.StatusOK {
40+
t.Errorf("期望状态码 200,实际为 %d", resp.StatusCode)
41+
}
42+
43+
if method := resp.Header.Get("X-Request-Method"); method != "GET" {
44+
t.Errorf("期望请求方法为 GET,实际为 %s", method)
45+
}
46+
47+
body, err := io.ReadAll(resp.Body)
48+
if err != nil {
49+
t.Fatalf("读取响应体失败: %v", err)
50+
}
51+
52+
if !strings.Contains(string(body), `"method":"GET"`) {
53+
t.Errorf("响应体中应包含请求方法 GET,实际为: %s", string(body))
54+
}
55+
}
56+
57+
func TestPost(t *testing.T) {
58+
server := setupTestServer()
59+
defer server.Close()
60+
61+
body := strings.NewReader(`{"key":"value"}`)
62+
resp, err := Post(server.URL+"/post", "application/json", body)
63+
if err != nil {
64+
t.Fatalf("Post 请求失败: %v", err)
65+
}
66+
defer resp.Body.Close()
67+
68+
if resp.StatusCode != http.StatusOK {
69+
t.Errorf("期望状态码 200,实际为 %d", resp.StatusCode)
70+
}
71+
72+
if method := resp.Header.Get("X-Request-Method"); method != "POST" {
73+
t.Errorf("期望请求方法为 POST,实际为 %s", method)
74+
}
75+
76+
respBody, err := io.ReadAll(resp.Body)
77+
if err != nil {
78+
t.Fatalf("读取响应体失败: %v", err)
79+
}
80+
81+
if !strings.Contains(string(respBody), `"method":"POST"`) {
82+
t.Errorf("响应体中应包含请求方法 POST,实际为: %s", string(respBody))
83+
}
84+
85+
if !strings.Contains(string(respBody), `"body":"{"key":"value"}"`) {
86+
t.Errorf("响应体中应包含请求体,实际为: %s", string(respBody))
87+
}
88+
}
89+
90+
func TestPUT(t *testing.T) {
91+
server := setupTestServer()
92+
defer server.Close()
93+
94+
body := strings.NewReader(`{"key":"updated"}`)
95+
resp, err := PUT(server.URL+"/put", "application/json", body)
96+
if err != nil {
97+
t.Fatalf("PUT 请求失败: %v", err)
98+
}
99+
defer resp.Body.Close()
100+
101+
if resp.StatusCode != http.StatusOK {
102+
t.Errorf("期望状态码 200,实际为 %d", resp.StatusCode)
103+
}
104+
105+
if method := resp.Header.Get("X-Request-Method"); method != "PUT" {
106+
t.Errorf("期望请求方法为 PUT,实际为 %s", method)
107+
}
108+
109+
respBody, err := io.ReadAll(resp.Body)
110+
if err != nil {
111+
t.Fatalf("读取响应体失败: %v", err)
112+
}
113+
114+
if !strings.Contains(string(respBody), `"method":"PUT"`) {
115+
t.Errorf("响应体中应包含请求方法 PUT,实际为: %s", string(respBody))
116+
}
117+
}
118+
119+
func TestDelete(t *testing.T) {
120+
server := setupTestServer()
121+
defer server.Close()
122+
123+
body := strings.NewReader(`{"id":123}`)
124+
resp, err := Delete(server.URL+"/delete", "application/json", body)
125+
if err != nil {
126+
t.Fatalf("Delete 请求失败: %v", err)
127+
}
128+
defer resp.Body.Close()
129+
130+
if resp.StatusCode != http.StatusOK {
131+
t.Errorf("期望状态码 200,实际为 %d", resp.StatusCode)
132+
}
133+
134+
if method := resp.Header.Get("X-Request-Method"); method != "DELETE" {
135+
t.Errorf("期望请求方法为 DELETE,实际为 %s", method)
136+
}
137+
138+
respBody, err := io.ReadAll(resp.Body)
139+
if err != nil {
140+
t.Fatalf("读取响应体失败: %v", err)
141+
}
142+
143+
if !strings.Contains(string(respBody), `"method":"DELETE"`) {
144+
t.Errorf("响应体中应包含请求方法 DELETE,实际为: %s", string(respBody))
145+
}
146+
}
147+
148+
func TestHead(t *testing.T) {
149+
server := setupTestServer()
150+
defer server.Close()
151+
152+
resp, err := Head(server.URL + "/head")
153+
if err != nil {
154+
t.Fatalf("Head 请求失败: %v", err)
155+
}
156+
defer resp.Body.Close()
157+
158+
if resp.StatusCode != http.StatusOK {
159+
t.Errorf("期望状态码 200,实际为 %d", resp.StatusCode)
160+
}
161+
162+
if method := resp.Header.Get("X-Request-Method"); method != "HEAD" {
163+
t.Errorf("期望请求方法为 HEAD,实际为 %s", method)
164+
}
165+
166+
body, err := io.ReadAll(resp.Body)
167+
if err != nil {
168+
t.Fatalf("读取响应体失败: %v", err)
169+
}
170+
171+
if len(body) > 0 {
172+
t.Errorf("HEAD 请求不应返回响应体,实际返回: %s", string(body))
173+
}
174+
}
175+
176+
func TestPostForm(t *testing.T) {
177+
server := setupTestServer()
178+
defer server.Close()
179+
180+
form := url.Values{}
181+
form.Add("username", "testuser")
182+
form.Add("password", "testpass")
183+
184+
resp, err := PostForm(server.URL+"/form", form)
185+
if err != nil {
186+
t.Fatalf("PostForm 请求失败: %v", err)
187+
}
188+
defer resp.Body.Close()
189+
190+
if resp.StatusCode != http.StatusOK {
191+
t.Errorf("期望状态码 200,实际为 %d", resp.StatusCode)
192+
}
193+
194+
if method := resp.Header.Get("X-Request-Method"); method != "POST" {
195+
t.Errorf("期望请求方法为 POST,实际为 %s", method)
196+
}
197+
198+
respBody, err := io.ReadAll(resp.Body)
199+
if err != nil {
200+
t.Fatalf("读取响应体失败: %v", err)
201+
}
18202

203+
expectedFormData := `{"method":"POST","url":"/form","body":"password=testpass&username=testuser"}`
204+
if !strings.Contains(string(respBody), expectedFormData) {
205+
t.Errorf("响应体中应包含表单数据 %s,实际为: %s", expectedFormData, string(respBody))
206+
}
19207
}

0 commit comments

Comments
 (0)