This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_test.go
More file actions
162 lines (145 loc) · 4.69 KB
/
request_test.go
File metadata and controls
162 lines (145 loc) · 4.69 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
package swifthttp
import (
"net/http"
"reflect"
"testing"
)
func TestNewRequest(t *testing.T) {
req := NewRequest()
if req == nil {
t.Fatal("NewRequest returned nil")
}
if req.Method != RequestTypeGet {
t.Errorf("Expected default method to be GET, got %s", req.Method)
}
if req.Header == nil {
t.Errorf("Expected default Header to be non-nil, but got nil")
}
if len(req.Header) != 0 {
t.Errorf("Expected default Header to be empty, but got %v", req.Header)
}
}
func TestWithMethod(t *testing.T) {
req := NewRequest(WithMethod(RequestTypePost))
if req.Method != RequestTypePost {
t.Errorf("Expected method POST, got %s", req.Method)
}
}
func TestWithCustomMethod(t *testing.T) {
customMethod := "CUSTOM"
req := NewRequest(WithCustomMethod(customMethod))
if string(req.Method) != customMethod {
t.Errorf("Expected method %s, got %s", customMethod, req.Method)
}
}
func TestWithBody(t *testing.T) {
body := []byte("test body")
contentType := "text/plain"
req := NewRequest(WithBody(body, contentType))
if !reflect.DeepEqual(req.Body, body) {
t.Errorf("Expected body %s, got %s", body, req.Body)
}
if req.ContentType != contentType {
t.Errorf("Expected contentType %s, got %s", contentType, req.ContentType)
}
}
func TestWithHeaders(t *testing.T) {
headers := make(http.Header)
headers.Set("X-Test", "value")
req := NewRequest(WithHeaders(headers))
if req.Header.Get("X-Test") != "value" {
t.Errorf("Expected header 'X-Test' to be 'value', got '%s'", req.Header.Get("X-Test"))
}
headers.Set("X-Another", "new_value")
headers.Add("X-Test", "another_value")
if req.Header.Get("X-Another") != "" {
t.Errorf("Modification to original header map should not affect request's headers, but 'X-Another' was found with value '%s'", req.Header.Get("X-Another"))
}
if len(req.Header["X-Test"]) != 1 {
t.Errorf("Expected request to have 1 value for X-Test, but it was modified externally. Got %d values.", len(req.Header["X-Test"]))
}
}
func TestWithSetHeader(t *testing.T) {
req := NewRequest(
WithSetHeader("X-Test", "value1"),
WithSetHeader("X-Test", "value2"),
)
if req.Header == nil {
t.Fatal("Header is nil after WithSetHeader")
}
if val := req.Header.Get("X-Test"); val != "value2" {
t.Errorf("Expected header X-Test to be 'value2', got '%s'", val)
}
if len(req.Header["X-Test"]) != 1 {
t.Errorf("Expected 1 value for X-Test, got %d", len(req.Header["X-Test"]))
}
}
func TestWithAddHeader(t *testing.T) {
req := NewRequest(
WithAddHeader("X-Test-Add", "value1"),
WithAddHeader("X-Test-Add", "value2"),
)
if req.Header == nil {
t.Fatal("Header is nil after WithAddHeader")
}
vals := req.Header["X-Test-Add"]
expectedVals := []string{"value1", "value2"}
if !reflect.DeepEqual(vals, expectedVals) {
t.Errorf("Expected header X-Test-Add to be %v, got %v", expectedVals, vals)
}
}
func TestWithContentType(t *testing.T) {
contentType := "application/json"
req := NewRequest(WithContentType(contentType))
if req.ContentType != contentType {
t.Errorf("Expected contentType %s, got %s", contentType, req.ContentType)
}
}
func TestWithBoundary(t *testing.T) {
boundary := "myboundary"
expectedContentType := "multipart/form-data; boundary=" + boundary
req := NewRequest(WithBoundary(boundary))
if req.ContentType != expectedContentType {
t.Errorf("Expected contentType %s, got %s", expectedContentType, req.ContentType)
}
}
func TestWithCustomPath(t *testing.T) {
path := "/custom/path"
req := NewRequest(WithPath(path))
if req.RawPath != path {
t.Errorf("Expected RawPath %s, got %s", path, req.RawPath)
}
}
func TestRequestOptionsChaining(t *testing.T) {
body := []byte("data")
req := NewRequest(
WithMethod(RequestTypePut),
WithBody(body, "app/data"),
WithSetHeader("Authorization", "Bearer token"),
WithPath("/api/v1/resource"),
WithAddHeader("X-Multi", "v1"),
WithAddHeader("X-Multi", "v2"),
)
if req.Method != RequestTypePut {
t.Errorf("Chained: Expected method PUT, got %s", req.Method)
}
if !reflect.DeepEqual(req.Body, body) {
t.Errorf("Chained: Expected body %s, got %s", body, req.Body)
}
if req.ContentType != "app/data" {
t.Errorf("Chained: Expected contentType 'app/data', got %s", req.ContentType)
}
if req.Header == nil {
t.Fatal("Chained: Header is nil")
}
if auth := req.Header.Get("Authorization"); auth != "Bearer token" {
t.Errorf("Chained: Expected Authorization header 'Bearer token', got '%s'", auth)
}
expectedMulti := []string{"v1", "v2"}
if !reflect.DeepEqual(req.Header["X-Multi"], expectedMulti) {
t.Errorf("Chained: Expected X-Multi header %v, got %v", expectedMulti, req.Header["X-Multi"])
}
if req.RawPath != "/api/v1/resource" {
t.Errorf("Chained: Expected RawPath '/api/v1/resource', got %s", req.RawPath)
}
}