Skip to content

Commit fc51b33

Browse files
feat(enhancement): improve buildCurlRequest for unit test (#884)
Co-authored-by: yaziedda <[email protected]>
1 parent bea44b7 commit fc51b33

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

curl_cmd_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package resty
22

33
import (
4+
"bytes"
45
"io"
56
"net/http"
7+
"net/http/cookiejar"
68
"os"
79
"strings"
810
"testing"
@@ -134,3 +136,112 @@ func captureStderr() (getOutput func() string, restore func()) {
134136
}
135137
return getOutput, restore
136138
}
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&param2=value2",
176+
expected: "curl -X GET 'http://example.com?param1=value1&param2=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+
}

util_curl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ func buildCurlRequest(req *http.Request, httpCookiejar http.CookieJar) (curl str
2727
if cookieJar, ok := httpCookiejar.(*cookiejar.Jar); ok {
2828
cookies := cookieJar.Cookies(req.URL)
2929
if len(cookies) > 0 {
30-
curl += ` -H ` + shellescape.Quote(dumpCurlCookies(cookies)) + " "
30+
curl += `-H ` + shellescape.Quote(dumpCurlCookies(cookies)) + " "
3131
}
3232
}
3333

3434
// 3. Generate curl body
3535
if req.Body != nil {
3636
buf, _ := io.ReadAll(req.Body)
3737
req.Body = io.NopCloser(bytes.NewBuffer(buf)) // important!!
38-
curl += `-d ` + shellescape.Quote(string(buf))
38+
curl += `-d ` + shellescape.Quote(string(buf)) + " "
3939
}
4040

4141
urlString := shellescape.Quote(req.URL.String())
4242
if urlString == "''" {
4343
urlString = "'http://unexecuted-request'"
4444
}
45-
curl += " " + urlString
45+
curl += urlString
4646
return curl
4747
}
4848

0 commit comments

Comments
 (0)