-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
472 lines (419 loc) · 12.6 KB
/
Copy pathintegration_test.go
File metadata and controls
472 lines (419 loc) · 12.6 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package http2
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
"strings"
"testing"
)
// TestConvertHTTPRequest tests request conversion from http.Request to http2.Request
func TestConvertHTTPRequest(t *testing.T) {
tests := []struct {
name string
method string
urlStr string
headers map[string]string
body string
wantMethod string
wantPath string
wantScheme string
}{
{
name: "Simple GET",
method: "GET",
urlStr: "http://example.com/path",
wantMethod: "GET",
wantPath: "/path",
wantScheme: "http",
},
{
name: "GET with query",
method: "GET",
urlStr: "https://api.example.com/users?id=123&name=test",
wantMethod: "GET",
wantPath: "/users?id=123&name=test",
wantScheme: "https",
},
{
name: "POST with body",
method: "POST",
urlStr: "http://example.com/api/data",
body: `{"key":"value"}`,
wantMethod: "POST",
wantPath: "/api/data",
wantScheme: "http",
},
{
name: "Root path",
method: "GET",
urlStr: "http://example.com",
wantMethod: "GET",
wantPath: "/",
wantScheme: "http",
},
{
name: "PUT request",
method: "PUT",
urlStr: "https://example.com/resource/123",
wantMethod: "PUT",
wantPath: "/resource/123",
wantScheme: "https",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var bodyReader io.Reader
if tt.body != "" {
bodyReader = strings.NewReader(tt.body)
}
req, err := http.NewRequest(tt.method, tt.urlStr, bodyReader)
if err != nil {
t.Fatalf("NewRequest() error = %v", err)
}
if tt.headers != nil {
for k, v := range tt.headers {
req.Header.Set(k, v)
}
}
http2Req, err := ConvertHTTPRequest(req)
if err != nil {
t.Fatalf("ConvertHTTPRequest() error = %v", err)
}
if http2Req.Method != tt.wantMethod {
t.Errorf("Method = %s, want %s", http2Req.Method, tt.wantMethod)
}
if http2Req.Path != tt.wantPath {
t.Errorf("Path = %s, want %s", http2Req.Path, tt.wantPath)
}
if http2Req.Scheme != tt.wantScheme {
t.Errorf("Scheme = %s, want %s", http2Req.Scheme, tt.wantScheme)
}
if tt.body != "" && string(http2Req.Body) != tt.body {
t.Errorf("Body = %s, want %s", http2Req.Body, tt.body)
}
})
}
}
// TestConvertHTTPRequestHeaders tests header conversion
func TestConvertHTTPRequestHeaders(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com/test", nil)
req.Header.Set("User-Agent", "test-agent/1.0")
req.Header.Set("Accept", "application/json")
req.Header.Set("Connection", "keep-alive") // Should be filtered out
http2Req, err := ConvertHTTPRequest(req)
if err != nil {
t.Fatalf("ConvertHTTPRequest() error = %v", err)
}
// Headers should be lowercase
if http2Req.Headers["user-agent"] != "test-agent/1.0" {
t.Errorf("user-agent header not converted properly")
}
if http2Req.Headers["accept"] != "application/json" {
t.Errorf("accept header not converted properly")
}
// Connection-specific headers should be filtered
if _, exists := http2Req.Headers["connection"]; exists {
t.Error("connection header should be filtered out")
}
}
// TestConvertHTTPResponse tests response conversion
func TestConvertHTTPResponse(t *testing.T) {
originalReq, _ := http.NewRequest("GET", "http://example.com/test", nil)
tests := []struct {
name string
statusCode int
headers map[string]string
body []byte
wantStatus string
wantStatusCode int
}{
{
name: "200 OK",
statusCode: 200,
body: []byte("success"),
wantStatus: "200 OK",
wantStatusCode: 200,
},
{
name: "404 Not Found",
statusCode: 404,
body: []byte("not found"),
wantStatus: "404 Not Found",
wantStatusCode: 404,
},
{
name: "500 Internal Server Error",
statusCode: 500,
body: []byte("error"),
wantStatus: "500 Internal Server Error",
wantStatusCode: 500,
},
{
name: "204 No Content",
statusCode: 204,
body: []byte{},
wantStatus: "204 No Content",
wantStatusCode: 204,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
http2Resp := &Response{
StatusCode: tt.statusCode,
Headers: tt.headers,
Body: tt.body,
}
httpResp, err := ConvertHTTPResponse(http2Resp, originalReq)
if err != nil {
t.Fatalf("ConvertHTTPResponse() error = %v", err)
}
if httpResp.StatusCode != tt.wantStatusCode {
t.Errorf("StatusCode = %d, want %d", httpResp.StatusCode, tt.wantStatusCode)
}
if httpResp.Status != tt.wantStatus {
t.Errorf("Status = %s, want %s", httpResp.Status, tt.wantStatus)
}
if httpResp.Proto != "HTTP/2.0" {
t.Errorf("Proto = %s, want HTTP/2.0", httpResp.Proto)
}
if httpResp.ProtoMajor != 2 {
t.Errorf("ProtoMajor = %d, want 2", httpResp.ProtoMajor)
}
if httpResp.ProtoMinor != 0 {
t.Errorf("ProtoMinor = %d, want 0", httpResp.ProtoMinor)
}
// Read and verify body
bodyBytes, _ := io.ReadAll(httpResp.Body)
if !bytes.Equal(bodyBytes, tt.body) {
t.Errorf("Body = %s, want %s", bodyBytes, tt.body)
}
})
}
}
// TestConvertHTTPResponseHeaders tests response header conversion
func TestConvertHTTPResponseHeaders(t *testing.T) {
originalReq, _ := http.NewRequest("GET", "http://example.com/test", nil)
http2Resp := &Response{
StatusCode: 200,
Headers: map[string]string{
"content-type": "application/json",
"cache-control": "no-cache",
":status": "200", // Pseudo-header should be filtered
},
Body: []byte{},
}
httpResp, err := ConvertHTTPResponse(http2Resp, originalReq)
if err != nil {
t.Fatalf("ConvertHTTPResponse() error = %v", err)
}
// Regular headers should be present
if httpResp.Header.Get("content-type") != "application/json" {
t.Error("content-type header missing or incorrect")
}
if httpResp.Header.Get("cache-control") != "no-cache" {
t.Error("cache-control header missing or incorrect")
}
// Pseudo-headers should be filtered
if httpResp.Header.Get(":status") != "" {
t.Error("pseudo-header :status should be filtered out")
}
}
// TestIsConnectionSpecificHeader tests connection-specific header detection
func TestIsConnectionSpecificHeader(t *testing.T) {
tests := []struct {
header string
expected bool
}{
{"connection", true},
{"keep-alive", true},
{"proxy-connection", true},
{"transfer-encoding", true},
{"upgrade", true},
{"content-type", false},
{"user-agent", false},
{"accept", false},
}
for _, tt := range tests {
t.Run(tt.header, func(t *testing.T) {
result := isConnectionSpecific(tt.header)
if result != tt.expected {
t.Errorf("isConnectionSpecific(%s) = %v, want %v", tt.header, result, tt.expected)
}
})
}
}
// TestNewHTTPRequest tests HTTP request creation helpers
func TestNewHTTPRequest(t *testing.T) {
tests := []struct {
name string
method string
url string
body string
}{
{"GET", "GET", "http://example.com", ""},
{"POST", "POST", "http://example.com/api", `{"data":"test"}`},
{"PUT", "PUT", "http://example.com/resource", "update data"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var bodyReader io.Reader
if tt.body != "" {
bodyReader = strings.NewReader(tt.body)
}
req, err := NewHTTPRequest(tt.method, tt.url, bodyReader)
if err != nil {
t.Fatalf("NewHTTPRequest() error = %v", err)
}
if req.Method != tt.method {
t.Errorf("Method = %s, want %s", req.Method, tt.method)
}
if req.URL.String() != tt.url {
t.Errorf("URL = %s, want %s", req.URL.String(), tt.url)
}
})
}
}
// TestNewHTTPRequestWithContext tests context-aware request creation
func TestNewHTTPRequestWithContext(t *testing.T) {
ctx := context.Background()
req, err := NewHTTPRequestWithContext(ctx, "GET", "http://example.com", nil)
if err != nil {
t.Fatalf("NewHTTPRequestWithContext() error = %v", err)
}
if req.Context() != ctx {
t.Error("Request context not set correctly")
}
}
// TestNewGetRequest tests GET request helper
func TestNewGetRequest(t *testing.T) {
req, err := NewGetRequest("http://example.com/api/users")
if err != nil {
t.Fatalf("NewGetRequest() error = %v", err)
}
if req.Method != "GET" {
t.Errorf("Method = %s, want GET", req.Method)
}
if req.Body != nil {
t.Error("GET request should have nil body")
}
}
// TestNewPostRequest tests POST request helper
func TestNewPostRequest(t *testing.T) {
jsonBody := []byte(`{"name":"test","value":123}`)
req, err := NewPostRequest("http://example.com/api/data", jsonBody)
if err != nil {
t.Fatalf("NewPostRequest() error = %v", err)
}
if req.Method != "POST" {
t.Errorf("Method = %s, want POST", req.Method)
}
if req.Header.Get("Content-Type") != "application/json" {
t.Error("Content-Type should be application/json")
}
bodyBytes, _ := io.ReadAll(req.Body)
if !bytes.Equal(bodyBytes, jsonBody) {
t.Errorf("Body = %s, want %s", bodyBytes, jsonBody)
}
}
// TestNewPostFormRequest tests form POST request helper
func TestNewPostFormRequest(t *testing.T) {
formData := url.Values{
"username": []string{"testuser"},
"password": []string{"testpass"},
}
req, err := NewPostFormRequest("http://example.com/login", formData)
if err != nil {
t.Fatalf("NewPostFormRequest() error = %v", err)
}
if req.Method != "POST" {
t.Errorf("Method = %s, want POST", req.Method)
}
if req.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Error("Content-Type should be application/x-www-form-urlencoded")
}
bodyBytes, _ := io.ReadAll(req.Body)
bodyStr := string(bodyBytes)
if !strings.Contains(bodyStr, "username=testuser") {
t.Error("Form data should contain username")
}
if !strings.Contains(bodyStr, "password=testpass") {
t.Error("Form data should contain password")
}
}
// TestConvertRequestWithMultipleHeaderValues tests header value joining
func TestConvertRequestWithMultipleHeaderValues(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Add("Accept", "text/html")
req.Header.Add("Accept", "application/json")
http2Req, err := ConvertHTTPRequest(req)
if err != nil {
t.Fatalf("ConvertHTTPRequest() error = %v", err)
}
// Multiple values should be joined with comma
acceptHeader := http2Req.Headers["accept"]
if !strings.Contains(acceptHeader, "text/html") || !strings.Contains(acceptHeader, "application/json") {
t.Errorf("Accept header = %s, should contain both values", acceptHeader)
}
}
// TestConvertResponseWithMultipleHeaderValues tests header value splitting
func TestConvertResponseWithMultipleHeaderValues(t *testing.T) {
originalReq, _ := http.NewRequest("GET", "http://example.com", nil)
http2Resp := &Response{
StatusCode: 200,
Headers: map[string]string{
"set-cookie": "session=abc123, token=xyz789",
},
Body: []byte{},
}
httpResp, err := ConvertHTTPResponse(http2Resp, originalReq)
if err != nil {
t.Fatalf("ConvertHTTPResponse() error = %v", err)
}
// Multiple values should be split
cookies := httpResp.Header.Values("set-cookie")
if len(cookies) != 2 {
t.Errorf("Expected 2 set-cookie headers, got %d", len(cookies))
}
}
// TestConvertRequestBodyRestoration tests that request body is restored after reading
func TestConvertRequestBodyRestoration(t *testing.T) {
bodyContent := "test body content"
req, _ := http.NewRequest("POST", "http://example.com", strings.NewReader(bodyContent))
_, err := ConvertHTTPRequest(req)
if err != nil {
t.Fatalf("ConvertHTTPRequest() error = %v", err)
}
// Body should be restored for retries
restoredBody, _ := io.ReadAll(req.Body)
if string(restoredBody) != bodyContent {
t.Errorf("Body not restored: got %s, want %s", restoredBody, bodyContent)
}
}
// BenchmarkConvertHTTPRequest benchmarks request conversion
func BenchmarkConvertHTTPRequest(b *testing.B) {
req, _ := http.NewRequest("POST", "http://example.com/api/users", strings.NewReader(`{"name":"test"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "test/1.0")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ConvertHTTPRequest(req)
}
}
// BenchmarkConvertHTTPResponse benchmarks response conversion
func BenchmarkConvertHTTPResponse(b *testing.B) {
originalReq, _ := http.NewRequest("GET", "http://example.com", nil)
http2Resp := &Response{
StatusCode: 200,
Headers: map[string]string{
"content-type": "application/json",
"cache-control": "no-cache",
},
Body: []byte(`{"result":"success"}`),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ConvertHTTPResponse(http2Resp, originalReq)
}
}