Skip to content

Commit b0b50ec

Browse files
committed
Add new dependencies and update file names
1 parent 82b7fd2 commit b0b50ec

11 files changed

+639
-1
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ go 1.21
55
require (
66
github.com/PuerkitoBio/goquery v1.8.1
77
github.com/google/uuid v1.6.0
8+
github.com/stretchr/testify v1.8.1
89
go.uber.org/zap v1.26.0
910
)
1011

1112
require (
1213
github.com/andybalholm/cascadia v1.3.1 // indirect
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
github.com/stretchr/objx v0.5.0 // indirect
1317
go.uber.org/multierr v1.10.0 // indirect
1418
golang.org/x/net v0.19.0 // indirect
19+
gopkg.in/yaml.v3 v3.0.1 // indirect
1520
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAc
22
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
33
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
44
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
5+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
56
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
67
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
78
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
89
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
910
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1011
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
14+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
15+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
16+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1118
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
1219
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1320
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
@@ -48,5 +55,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
4855
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
4956
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
5057
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
58+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
59+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
60+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5161
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5262
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package httpclient
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/mock"
9+
)
10+
11+
// MockAPIHandler is a mock type for the APIHandler interface
12+
type MockAPIHandler struct {
13+
mock.Mock
14+
}
15+
16+
// GetAPIRequestHeaders mocks the GetAPIRequestHeaders method of the APIHandler
17+
func (_m *MockAPIHandler) GetAPIRequestHeaders(endpoint string) map[string]string {
18+
ret := _m.Called(endpoint)
19+
20+
var r0 map[string]string
21+
if rf, ok := ret.Get(0).(func(string) map[string]string); ok {
22+
r0 = rf(endpoint)
23+
} else {
24+
r0 = ret.Get(0).(map[string]string)
25+
}
26+
27+
return r0
28+
}
29+
30+
// TestSetAuthorization tests the SetAuthorization method
31+
func TestSetAuthorization(t *testing.T) {
32+
req, _ := http.NewRequest("GET", "http://example.com", nil)
33+
logger := NewMockLogger() // Assuming you have a mock logger
34+
hm := NewHeaderManager(req, logger, nil, "")
35+
36+
// Test without Bearer prefix
37+
hm.SetAuthorization("token123")
38+
assert.Equal(t, "Bearer token123", req.Header.Get("Authorization"))
39+
40+
// Test with Bearer prefix
41+
hm.SetAuthorization("Bearer token123")
42+
assert.Equal(t, "Bearer token123", req.Header.Get("Authorization"))
43+
}
44+
45+
// TestSetContentType tests the SetContentType method
46+
func TestSetContentType(t *testing.T) {
47+
req, _ := http.NewRequest("GET", "http://example.com", nil)
48+
logger := NewMockLogger() // Assuming you have a mock logger
49+
hm := NewHeaderManager(req, logger, nil, "")
50+
51+
hm.SetContentType("application/json")
52+
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
53+
}
54+
55+
// TestSetAccept tests the SetAccept method
56+
func TestSetAccept(t *testing.T) {
57+
req, _ := http.NewRequest("GET", "http://example.com", nil)
58+
logger := NewMockLogger() // Assuming you have a mock logger
59+
hm := NewHeaderManager(req, logger, nil, "")
60+
61+
hm.SetAccept("application/json")
62+
assert.Equal(t, "application/json", req.Header.Get("Accept"))
63+
}
64+
65+
// TestSetUserAgent tests the SetUserAgent method
66+
func TestSetUserAgent(t *testing.T) {
67+
req, _ := http.NewRequest("GET", "http://example.com", nil)
68+
logger := NewMockLogger() // Assuming you have a mock logger
69+
hm := NewHeaderManager(req, logger, nil, "")
70+
71+
hm.SetUserAgent("CustomUserAgent/1.0")
72+
assert.Equal(t, "CustomUserAgent/1.0", req.Header.Get("User-Agent"))
73+
}
74+
75+
// TestSetRequestHeaders tests the SetRequestHeaders method
76+
func TestSetRequestHeaders(t *testing.T) {
77+
req, _ := http.NewRequest("GET", "http://example.com", nil)
78+
logger := NewMockLogger() // Assuming you have a mock logger
79+
mockAPIHandler := new(MockAPIHandler)
80+
hm := NewHeaderManager(req, logger, mockAPIHandler, "token123")
81+
82+
// Setup expectations
83+
mockAPIHandler.On("GetAPIRequestHeaders", "testEndpoint").Return(map[string]string{
84+
"Authorization": "",
85+
"X-Custom-Header": "CustomValue",
86+
})
87+
88+
hm.SetRequestHeaders("testEndpoint")
89+
90+
// Assertions
91+
assert.Equal(t, "Bearer token123", req.Header.Get("Authorization"))
92+
assert.Equal(t, "CustomValue", req.Header.Get("X-Custom-Header"))
93+
mockAPIHandler.AssertExpectations(t)
94+
}
95+
96+
// TestSetCacheControlHeader tests the SetCacheControlHeader function
97+
func TestSetCacheControlHeader(t *testing.T) {
98+
req, _ := http.NewRequest("GET", "http://example.com", nil)
99+
100+
SetCacheControlHeader(req, "no-cache")
101+
assert.Equal(t, "no-cache", req.Header.Get("Cache-Control"))
102+
}
103+
104+
// TestSetConditionalHeaders tests the SetConditionalHeaders function
105+
func TestSetConditionalHeaders(t *testing.T) {
106+
req, _ := http.NewRequest("GET", "http://example.com", nil)
107+
108+
SetConditionalHeaders(req, "Wed, 21 Oct 2015 07:28:00 GMT", "etagValue")
109+
assert.Equal(t, "Wed, 21 Oct 2015 07:28:00 GMT", req.Header.Get("If-Modified-Since"))
110+
assert.Equal(t, "etagValue", req.Header.Get("If-None-Match"))
111+
}
112+
113+
// TestSetAcceptEncodingHeader tests the SetAcceptEncodingHeader function
114+
func TestSetAcceptEncodingHeader(t *testing.T) {
115+
req, _ := http.NewRequest("GET", "http://example.com", nil)
116+
117+
SetAcceptEncodingHeader(req, "gzip, deflate")
118+
assert.Equal(t, "gzip, deflate", req.Header.Get("Accept-Encoding"))
119+
}
120+
121+
// TestSetRefererHeader tests the SetRefererHeader function
122+
func TestSetRefererHeader(t *testing.T) {
123+
req, _ := http.NewRequest("GET", "http://example.com", nil)
124+
125+
SetRefererHeader(req, "http://referrer.example.com")
126+
assert.Equal(t, "http://referrer.example.com", req.Header.Get("Referer"))
127+
}
128+
129+
// TestSetXForwardedForHeader tests the SetXForwardedForHeader function
130+
func TestSetXForwardedForHeader(t *testing.T) {
131+
req, _ := http.NewRequest("GET", "http://example.com", nil)
132+
133+
SetXForwardedForHeader(req, "client1, proxy1, proxy2")
134+
assert.Equal(t, "client1, proxy1, proxy2", req.Header.Get("X-Forwarded-For"))
135+
}
136+
137+
// TestSetCustomHeader tests the ability to set arbitrary headers
138+
func TestSetCustomHeader(t *testing.T) {
139+
req, _ := http.NewRequest("GET", "http://example.com", nil)
140+
141+
SetCustomHeader(req, "X-Custom-Header", "CustomValue")
142+
assert.Equal(t, "CustomValue", req.Header.Get("X-Custom-Header"))
143+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// http_helpers.go
2+
package httpclient
3+
4+
import (
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// TestParseISO8601Date tests the ParseISO8601Date function with various date strings
12+
func TestParseISO8601Date(t *testing.T) {
13+
tests := []struct {
14+
dateStr string
15+
expectErr bool
16+
expectedTime time.Time // Add an expectedTime field for successful parsing
17+
}{
18+
{
19+
dateStr: "2023-01-02T15:04:05Z",
20+
expectErr: false,
21+
expectedTime: time.Date(2023, time.January, 2, 15, 4, 5, 0, time.UTC),
22+
},
23+
{
24+
dateStr: "2023-01-02T15:04:05-07:00",
25+
expectErr: false,
26+
expectedTime: time.Date(2023, time.January, 2, 15, 4, 5, 0, time.FixedZone("", -7*3600)),
27+
},
28+
{
29+
dateStr: "invalid-date",
30+
expectErr: true,
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.dateStr, func(t *testing.T) {
36+
result, err := ParseISO8601Date(tt.dateStr)
37+
38+
if tt.expectErr {
39+
assert.Error(t, err, "Expected an error for date string: "+tt.dateStr)
40+
} else {
41+
assert.NoError(t, err, "Did not expect an error for date string: "+tt.dateStr)
42+
assert.True(t, result.Equal(tt.expectedTime), "Parsed time should match expected time")
43+
}
44+
})
45+
}
46+
}
47+
48+
// TestRedactSensitiveData tests the RedactSensitiveData function with various scenarios
49+
func TestRedactSensitiveData(t *testing.T) {
50+
tests := []struct {
51+
name string
52+
hideSensitive bool
53+
key string
54+
value string
55+
expectedOutcome string
56+
}{
57+
{"RedactSensitiveKey", true, "AccessToken", "secret-token", "REDACTED"},
58+
{"RedactSensitiveKeyAuthorization", true, "Authorization", "Bearer secret-token", "REDACTED"},
59+
{"DoNotRedactNonSensitiveKey", true, "NonSensitiveKey", "non-sensitive-value", "non-sensitive-value"},
60+
{"DoNotRedactWhenDisabled", false, "AccessToken", "secret-token", "secret-token"},
61+
}
62+
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
client := &Client{
66+
clientConfig: ClientConfig{
67+
ClientOptions: ClientOptions{
68+
HideSensitiveData: tt.hideSensitive,
69+
},
70+
},
71+
}
72+
73+
result := RedactSensitiveData(client, tt.key, tt.value)
74+
assert.Equal(t, tt.expectedOutcome, result, "Redaction outcome should match expected")
75+
})
76+
}
77+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// httpclient_methods_test.go
2+
package httpclient
3+
4+
import (
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// TestIsIdempotentHTTPMethod tests the IsIdempotentHTTPMethod function with various HTTP methods
12+
func TestIsIdempotentHTTPMethod(t *testing.T) {
13+
tests := []struct {
14+
method string
15+
expected bool
16+
}{
17+
{http.MethodGet, true},
18+
{http.MethodPut, true},
19+
{http.MethodDelete, true},
20+
{http.MethodHead, true},
21+
{http.MethodOptions, true},
22+
{http.MethodTrace, true},
23+
// Non-idempotent methods
24+
{http.MethodPost, false},
25+
{http.MethodPatch, false},
26+
{http.MethodConnect, false},
27+
}
28+
29+
for _, tt := range tests {
30+
t.Run(tt.method, func(t *testing.T) {
31+
result := IsIdempotentHTTPMethod(tt.method)
32+
assert.Equal(t, tt.expected, result, "Idempotency status should match expected for method "+tt.method)
33+
})
34+
}
35+
}
36+
37+
// TestIsNonIdempotentHTTPMethod tests the IsNonIdempotentHTTPMethod function with various HTTP methods
38+
func TestIsNonIdempotentHTTPMethod(t *testing.T) {
39+
tests := []struct {
40+
method string
41+
expected bool
42+
}{
43+
// Non-idempotent methods
44+
{http.MethodPost, true},
45+
{http.MethodPatch, true},
46+
{http.MethodConnect, true},
47+
// Idempotent methods
48+
{http.MethodGet, false},
49+
{http.MethodPut, false},
50+
{http.MethodDelete, false},
51+
{http.MethodHead, false},
52+
{http.MethodOptions, false},
53+
{http.MethodTrace, false},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.method, func(t *testing.T) {
58+
result := IsNonIdempotentHTTPMethod(tt.method)
59+
assert.Equal(t, tt.expected, result, "Non-idempotency status should match expected for method "+tt.method)
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)