|
1 |
| -package requests_test |
| 1 | +package requests |
2 | 2 |
|
3 | 3 | import (
|
4 | 4 | "io"
|
5 | 5 | "net/http"
|
6 | 6 | "net/http/httptest"
|
7 |
| - "os" |
| 7 | + "net/url" |
| 8 | + "strings" |
8 | 9 | "testing"
|
9 | 10 | )
|
10 | 11 |
|
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()) |
14 | 17 |
|
15 |
| -func TestMain(m *testing.M) { |
| 18 | + if r.Method == "HEAD" { |
| 19 | + w.WriteHeader(http.StatusOK) |
| 20 | + return |
| 21 | + } |
16 | 22 |
|
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 | + } |
18 | 202 |
|
| 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 | + } |
19 | 207 | }
|
0 commit comments