From 824660ed785cd9e77070952c653bbe2f0f48da3a Mon Sep 17 00:00:00 2001 From: yaziedda Date: Tue, 8 Oct 2024 16:03:31 +0700 Subject: [PATCH 1/2] Enhance buildCurlRequest for unit test --- util_curl.go | 6 +-- util_test.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/util_curl.go b/util_curl.go index 2bd91270..073d0492 100644 --- a/util_curl.go +++ b/util_curl.go @@ -27,7 +27,7 @@ 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)) + " " } } @@ -35,14 +35,14 @@ func buildCurlRequest(req *http.Request, httpCookiejar http.CookieJar) (curl str 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 } diff --git a/util_test.go b/util_test.go index 4d0a888d..d01f890f 100644 --- a/util_test.go +++ b/util_test.go @@ -8,6 +8,8 @@ import ( "bytes" "errors" "mime/multipart" + "net/http" + "net/http/cookiejar" "testing" ) @@ -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¶m2=value2", + expected: "curl -X GET 'http://example.com?param1=value1¶m2=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) + }) + } +} From 87c933d74a90f4d885012d1c303614381064c8b5 Mon Sep 17 00:00:00 2001 From: yaziedda Date: Wed, 9 Oct 2024 14:05:16 +0700 Subject: [PATCH 2/2] Move tests from util_test.go to curl_cmd_test. --- curl_cmd_test.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++ util_test.go | 111 ----------------------------------------------- 2 files changed, 111 insertions(+), 111 deletions(-) diff --git a/curl_cmd_test.go b/curl_cmd_test.go index 55cac02b..3ec54f7b 100644 --- a/curl_cmd_test.go +++ b/curl_cmd_test.go @@ -1,8 +1,10 @@ package resty import ( + "bytes" "io" "net/http" + "net/http/cookiejar" "os" "strings" "testing" @@ -134,3 +136,112 @@ func captureStderr() (getOutput func() string, restore func()) { } return getOutput, restore } + +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¶m2=value2", + expected: "curl -X GET 'http://example.com?param1=value1¶m2=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) + }) + } +} diff --git a/util_test.go b/util_test.go index d01f890f..4d0a888d 100644 --- a/util_test.go +++ b/util_test.go @@ -8,8 +8,6 @@ import ( "bytes" "errors" "mime/multipart" - "net/http" - "net/http/cookiejar" "testing" ) @@ -107,112 +105,3 @@ 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¶m2=value2", - expected: "curl -X GET 'http://example.com?param1=value1¶m2=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) - }) - } -}