|
1 | 1 | package resty |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "io" |
5 | 6 | "net/http" |
| 7 | + "net/http/cookiejar" |
6 | 8 | "os" |
7 | 9 | "strings" |
8 | 10 | "testing" |
@@ -134,3 +136,112 @@ func captureStderr() (getOutput func() string, restore func()) { |
134 | 136 | } |
135 | 137 | return getOutput, restore |
136 | 138 | } |
| 139 | + |
| 140 | +func TestBuildCurlCommand(t *testing.T) { |
| 141 | + tests := []struct { |
| 142 | + name string |
| 143 | + method string |
| 144 | + url string |
| 145 | + headers map[string]string |
| 146 | + body string |
| 147 | + cookies []*http.Cookie |
| 148 | + expected string |
| 149 | + }{ |
| 150 | + { |
| 151 | + name: "With Headers", |
| 152 | + method: "GET", |
| 153 | + url: "http://example.com", |
| 154 | + headers: map[string]string{"Content-Type": "application/json", "Authorization": "Bearer token"}, |
| 155 | + expected: "curl -X GET -H 'Authorization: Bearer token' -H 'Content-Type: application/json' http://example.com", |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "With Body", |
| 159 | + method: "POST", |
| 160 | + url: "http://example.com", |
| 161 | + headers: map[string]string{"Content-Type": "application/json"}, |
| 162 | + body: `{"key":"value"}`, |
| 163 | + expected: "curl -X POST -H 'Content-Type: application/json' -d '{\"key\":\"value\"}' http://example.com", |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "With Empty Body", |
| 167 | + method: "POST", |
| 168 | + url: "http://example.com", |
| 169 | + headers: map[string]string{"Content-Type": "application/json"}, |
| 170 | + expected: "curl -X POST -H 'Content-Type: application/json' http://example.com", |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "With Query Params", |
| 174 | + method: "GET", |
| 175 | + url: "http://example.com?param1=value1¶m2=value2", |
| 176 | + expected: "curl -X GET 'http://example.com?param1=value1¶m2=value2'", |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "With Special Characters in URL", |
| 180 | + method: "GET", |
| 181 | + url: "http://example.com/path with spaces", |
| 182 | + expected: "curl -X GET http://example.com/path%20with%20spaces", |
| 183 | + }, |
| 184 | + { |
| 185 | + name: "With Cookies", |
| 186 | + method: "GET", |
| 187 | + url: "http://example.com", |
| 188 | + cookies: []*http.Cookie{{Name: "session_id", Value: "abc123"}}, |
| 189 | + expected: "curl -X GET -H 'Cookie: session_id=abc123' http://example.com", |
| 190 | + }, |
| 191 | + { |
| 192 | + name: "Without Cookies", |
| 193 | + method: "GET", |
| 194 | + url: "http://example.com", |
| 195 | + expected: "curl -X GET http://example.com", |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "With Multiple Cookies", |
| 199 | + method: "GET", |
| 200 | + url: "http://example.com", |
| 201 | + cookies: []*http.Cookie{{Name: "session_id", Value: "abc123"}, {Name: "user_id", Value: "user456"}}, |
| 202 | + expected: "curl -X GET -H 'Cookie: session_id=abc123&user_id=user456' http://example.com", |
| 203 | + }, |
| 204 | + { |
| 205 | + name: "With Empty Cookie Jar", |
| 206 | + method: "GET", |
| 207 | + url: "http://example.com", |
| 208 | + expected: "curl -X GET http://example.com", |
| 209 | + }, |
| 210 | + } |
| 211 | + |
| 212 | + for _, tt := range tests { |
| 213 | + t.Run(tt.name, func(t *testing.T) { |
| 214 | + // Setup request |
| 215 | + var ( |
| 216 | + req *http.Request |
| 217 | + err error |
| 218 | + ) |
| 219 | + |
| 220 | + if tt.body != "" { |
| 221 | + req, err = http.NewRequest(tt.method, tt.url, bytes.NewBufferString(tt.body)) |
| 222 | + } else { |
| 223 | + req, err = http.NewRequest(tt.method, tt.url, nil) |
| 224 | + } |
| 225 | + |
| 226 | + if err != nil { |
| 227 | + t.Fatalf("failed to create request: %v", err) |
| 228 | + } |
| 229 | + |
| 230 | + for k, v := range tt.headers { |
| 231 | + req.Header.Set(k, v) |
| 232 | + } |
| 233 | + |
| 234 | + // Setup cookie jar |
| 235 | + cookieJar, _ := cookiejar.New(nil) |
| 236 | + if len(tt.cookies) > 0 { |
| 237 | + cookieJar.SetCookies(req.URL, tt.cookies) |
| 238 | + } |
| 239 | + |
| 240 | + // Generate curl command |
| 241 | + curl := buildCurlRequest(req, cookieJar) |
| 242 | + |
| 243 | + // Assert |
| 244 | + assertEqual(t, tt.expected, curl) |
| 245 | + }) |
| 246 | + } |
| 247 | +} |
0 commit comments