Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance buildCurlRequest for unit test #884

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions util_curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ func buildCurlRequest(req *http.Request, httpCookiejar http.CookieJar) (curl str
if cookieJar, ok := httpCookiejar.(*cookiejar.Jar); ok {
cookies := cookieJar.Cookies(req.URL)
if len(cookies) > 0 {
curl += ` -H ` + shellescape.Quote(dumpCurlCookies(cookies)) + " "
curl += `-H ` + shellescape.Quote(dumpCurlCookies(cookies)) + " "
}
}

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

urlString := shellescape.Quote(req.URL.String())
if urlString == "''" {
urlString = "'http://unexecuted-request'"
}
curl += " " + urlString
curl += urlString
return curl
}

Expand Down
111 changes: 111 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"bytes"
"errors"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"testing"
)

Expand Down Expand Up @@ -105,3 +107,112 @@ func TestRestyErrorFuncs(t *testing.T) {
e = wrapErrors(nil, nie1)
assertEqual(t, "inner error 1", e.Error())
}

func TestBuildCurlCommand(t *testing.T) {
tests := []struct {
name string
method string
url string
headers map[string]string
body string
cookies []*http.Cookie
expected string
}{
{
name: "With Headers",
method: "GET",
url: "http://example.com",
headers: map[string]string{"Content-Type": "application/json", "Authorization": "Bearer token"},
expected: "curl -X GET -H 'Authorization: Bearer token' -H 'Content-Type: application/json' http://example.com",
},
{
name: "With Body",
method: "POST",
url: "http://example.com",
headers: map[string]string{"Content-Type": "application/json"},
body: `{"key":"value"}`,
expected: "curl -X POST -H 'Content-Type: application/json' -d '{\"key\":\"value\"}' http://example.com",
},
{
name: "With Empty Body",
method: "POST",
url: "http://example.com",
headers: map[string]string{"Content-Type": "application/json"},
expected: "curl -X POST -H 'Content-Type: application/json' http://example.com",
},
{
name: "With Query Params",
method: "GET",
url: "http://example.com?param1=value1&param2=value2",
expected: "curl -X GET 'http://example.com?param1=value1&param2=value2'",
},
{
name: "With Special Characters in URL",
method: "GET",
url: "http://example.com/path with spaces",
expected: "curl -X GET http://example.com/path%20with%20spaces",
},
{
name: "With Cookies",
method: "GET",
url: "http://example.com",
cookies: []*http.Cookie{{Name: "session_id", Value: "abc123"}},
expected: "curl -X GET -H 'Cookie: session_id=abc123' http://example.com",
},
{
name: "Without Cookies",
method: "GET",
url: "http://example.com",
expected: "curl -X GET http://example.com",
},
{
name: "With Multiple Cookies",
method: "GET",
url: "http://example.com",
cookies: []*http.Cookie{{Name: "session_id", Value: "abc123"}, {Name: "user_id", Value: "user456"}},
expected: "curl -X GET -H 'Cookie: session_id=abc123&user_id=user456' http://example.com",
},
{
name: "With Empty Cookie Jar",
method: "GET",
url: "http://example.com",
expected: "curl -X GET http://example.com",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup request
var (
req *http.Request
err error
)

if tt.body != "" {
req, err = http.NewRequest(tt.method, tt.url, bytes.NewBufferString(tt.body))
} else {
req, err = http.NewRequest(tt.method, tt.url, nil)
}

if err != nil {
t.Fatalf("failed to create request: %v", err)
}

for k, v := range tt.headers {
req.Header.Set(k, v)
}

// Setup cookie jar
cookieJar, _ := cookiejar.New(nil)
if len(tt.cookies) > 0 {
cookieJar.SetCookies(req.URL, tt.cookies)
}

// Generate curl command
curl := buildCurlRequest(req, cookieJar)

// Assert
assertEqual(t, tt.expected, curl)
})
}
}