-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp_test.go
More file actions
248 lines (226 loc) · 5.77 KB
/
http_test.go
File metadata and controls
248 lines (226 loc) · 5.77 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package executor_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"yapi.run/cli/internal/config"
"yapi.run/cli/internal/executor"
)
func TestHTTPExecutor_URLBuilding(t *testing.T) {
tests := []struct {
name string
yaml string
expectedPath string
expectedQuery url.Values
}{
{
name: "basic URL with path",
yaml: `
yapi: v1
url: https://example.com
path: /api/test
method: GET`,
expectedPath: "/api/test",
expectedQuery: url.Values{},
},
{
name: "URL without path",
yaml: `
yapi: v1
url: https://example.com/
method: GET`,
expectedPath: "/",
expectedQuery: url.Values{},
},
{
name: "URL with query string",
yaml: `
yapi: v1
url: https://example.com
path: /api
method: GET
query:
foo: bar
baz: qux`,
expectedPath: "/api",
expectedQuery: url.Values{
"foo": {"bar"},
"baz": {"qux"},
},
},
{
name: "URL with special characters in query requiring encoding",
yaml: `
yapi: v1
url: https://example.com
path: /search
method: GET
query:
q: "fish in:name"
sort: stars`,
expectedPath: "/search",
expectedQuery: url.Values{
"q": {"fish in:name"},
"sort": {"stars"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := config.LoadFromString(tt.yaml)
if err != nil {
t.Fatalf("LoadFromString failed: %v", err)
}
req := res.Request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != tt.expectedPath {
t.Errorf("Expected path %q, got %q", tt.expectedPath, r.URL.Path)
}
if r.URL.Query().Encode() != tt.expectedQuery.Encode() {
t.Errorf("Expected query %q, got %q", tt.expectedQuery.Encode(), r.URL.Query().Encode())
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
// Parse the original URL to extract path and query
parsedURL, err := url.Parse(req.URL)
if err != nil {
t.Fatalf("Failed to parse URL: %v", err)
}
// Replace with test server URL + path + query
req.URL = srv.URL + parsedURL.Path
if parsedURL.RawQuery != "" {
req.URL += "?" + parsedURL.RawQuery
}
client := &http.Client{}
execFn := executor.HTTPTransport(client)
resp, err := execFn(context.Background(), req)
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
if resp == nil {
t.Fatal("Execute returned nil response")
}
})
}
}
func TestHTTPTransport_BodyAndJSON(t *testing.T) {
tests := []struct {
name string
yaml string
expectedBody string
expectedStatus int
}{
{
name: "POST with simple JSON body",
yaml: `
yapi: v1
url: ""
method: POST
body:
name: test
value: 123`,
expectedBody: `{"name":"test","value":123}`,
expectedStatus: http.StatusOK,
},
{
name: "POST with raw JSON string",
yaml: `
yapi: v1
url: ""
method: POST
json: '{"status":"active","code":42}'`,
expectedBody: `{"status":"active","code":42}`,
expectedStatus: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := config.LoadFromString(tt.yaml)
if err != nil {
t.Fatalf("LoadFromString failed: %v", err)
}
req := res.Request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected POST method, got %s", r.Method)
}
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("Expected Content-Type application/json, got %s", r.Header.Get("Content-Type"))
}
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to read request body: %v", err)
}
var actual, expected any
if err := json.Unmarshal(bodyBytes, &actual); err != nil {
t.Fatalf("Failed to unmarshal actual request body: %v, body: %s", err, string(bodyBytes))
}
if err := json.Unmarshal([]byte(tt.expectedBody), &expected); err != nil {
t.Fatalf("Failed to unmarshal expected request body: %v, body: %s", err, tt.expectedBody)
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected request body %v, got %v", expected, actual)
}
w.WriteHeader(tt.expectedStatus)
w.Write([]byte(`{"status":"received"}`))
}))
defer srv.Close()
req.URL = srv.URL
client := &http.Client{}
execFn := executor.HTTPTransport(client)
resp, err := execFn(context.Background(), req)
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
expectedResponse := `{"status":"received"}`
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
if string(bodyBytes) != expectedResponse {
t.Errorf("Expected response %s, got %s", expectedResponse, string(bodyBytes))
}
})
}
}
func TestHTTPTransport_InsecureTLS(t *testing.T) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
insecureRes, err := config.LoadFromString(`
yapi: v1
url: https://example.com
method: GET
insecure: true`)
if err != nil {
t.Fatalf("LoadFromString failed: %v", err)
}
insecureReq := insecureRes.Request
insecureReq.URL = srv.URL
client := &http.Client{}
execFn := executor.HTTPTransport(client)
resp, err := execFn(context.Background(), insecureReq)
if err != nil {
t.Fatalf("Execute failed with insecure TLS: %v", err)
}
_ = resp.Body.Close()
secureRes, err := config.LoadFromString(`
yapi: v1
url: https://example.com
method: GET`)
if err != nil {
t.Fatalf("LoadFromString failed: %v", err)
}
secureReq := secureRes.Request
secureReq.URL = srv.URL
if _, err := execFn(context.Background(), secureReq); err == nil {
t.Fatalf("expected TLS verification error without insecure flag")
}
}