Skip to content

Commit 87c933d

Browse files
committed
Move tests from util_test.go to curl_cmd_test.
1 parent 824660e commit 87c933d

File tree

2 files changed

+111
-111
lines changed

2 files changed

+111
-111
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_test.go

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"bytes"
99
"errors"
1010
"mime/multipart"
11-
"net/http"
12-
"net/http/cookiejar"
1311
"testing"
1412
)
1513

@@ -107,112 +105,3 @@ func TestRestyErrorFuncs(t *testing.T) {
107105
e = wrapErrors(nil, nie1)
108106
assertEqual(t, "inner error 1", e.Error())
109107
}
110-
111-
func TestBuildCurlCommand(t *testing.T) {
112-
tests := []struct {
113-
name string
114-
method string
115-
url string
116-
headers map[string]string
117-
body string
118-
cookies []*http.Cookie
119-
expected string
120-
}{
121-
{
122-
name: "With Headers",
123-
method: "GET",
124-
url: "http://example.com",
125-
headers: map[string]string{"Content-Type": "application/json", "Authorization": "Bearer token"},
126-
expected: "curl -X GET -H 'Authorization: Bearer token' -H 'Content-Type: application/json' http://example.com",
127-
},
128-
{
129-
name: "With Body",
130-
method: "POST",
131-
url: "http://example.com",
132-
headers: map[string]string{"Content-Type": "application/json"},
133-
body: `{"key":"value"}`,
134-
expected: "curl -X POST -H 'Content-Type: application/json' -d '{\"key\":\"value\"}' http://example.com",
135-
},
136-
{
137-
name: "With Empty Body",
138-
method: "POST",
139-
url: "http://example.com",
140-
headers: map[string]string{"Content-Type": "application/json"},
141-
expected: "curl -X POST -H 'Content-Type: application/json' http://example.com",
142-
},
143-
{
144-
name: "With Query Params",
145-
method: "GET",
146-
url: "http://example.com?param1=value1&param2=value2",
147-
expected: "curl -X GET 'http://example.com?param1=value1&param2=value2'",
148-
},
149-
{
150-
name: "With Special Characters in URL",
151-
method: "GET",
152-
url: "http://example.com/path with spaces",
153-
expected: "curl -X GET http://example.com/path%20with%20spaces",
154-
},
155-
{
156-
name: "With Cookies",
157-
method: "GET",
158-
url: "http://example.com",
159-
cookies: []*http.Cookie{{Name: "session_id", Value: "abc123"}},
160-
expected: "curl -X GET -H 'Cookie: session_id=abc123' http://example.com",
161-
},
162-
{
163-
name: "Without Cookies",
164-
method: "GET",
165-
url: "http://example.com",
166-
expected: "curl -X GET http://example.com",
167-
},
168-
{
169-
name: "With Multiple Cookies",
170-
method: "GET",
171-
url: "http://example.com",
172-
cookies: []*http.Cookie{{Name: "session_id", Value: "abc123"}, {Name: "user_id", Value: "user456"}},
173-
expected: "curl -X GET -H 'Cookie: session_id=abc123&user_id=user456' http://example.com",
174-
},
175-
{
176-
name: "With Empty Cookie Jar",
177-
method: "GET",
178-
url: "http://example.com",
179-
expected: "curl -X GET http://example.com",
180-
},
181-
}
182-
183-
for _, tt := range tests {
184-
t.Run(tt.name, func(t *testing.T) {
185-
// Setup request
186-
var (
187-
req *http.Request
188-
err error
189-
)
190-
191-
if tt.body != "" {
192-
req, err = http.NewRequest(tt.method, tt.url, bytes.NewBufferString(tt.body))
193-
} else {
194-
req, err = http.NewRequest(tt.method, tt.url, nil)
195-
}
196-
197-
if err != nil {
198-
t.Fatalf("failed to create request: %v", err)
199-
}
200-
201-
for k, v := range tt.headers {
202-
req.Header.Set(k, v)
203-
}
204-
205-
// Setup cookie jar
206-
cookieJar, _ := cookiejar.New(nil)
207-
if len(tt.cookies) > 0 {
208-
cookieJar.SetCookies(req.URL, tt.cookies)
209-
}
210-
211-
// Generate curl command
212-
curl := buildCurlRequest(req, cookieJar)
213-
214-
// Assert
215-
assertEqual(t, tt.expected, curl)
216-
})
217-
}
218-
}

0 commit comments

Comments
 (0)