forked from opus-domini/fast-shot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_builder_config_test.go
190 lines (172 loc) · 4.94 KB
/
client_builder_config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package fastshot
import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/opus-domini/fast-shot/mock"
"github.com/stretchr/testify/assert"
)
func TestClientConfigBuilder(t *testing.T) {
tests := []struct {
name string
method func(*ClientBuilder) *ClientBuilder
setupClient func(*ClientConfigBase)
expectedConfig func(*ClientConfigBase) bool
expectedErrors []error
}{
{
name: "Set custom HTTP client",
method: func(cb *ClientBuilder) *ClientBuilder {
mockClient := new(mock.HttpClientComponent)
return cb.Config().SetCustomHttpClient(mockClient)
},
expectedConfig: func(ccb *ClientConfigBase) bool {
_, ok := ccb.HttpClient().(*mock.HttpClientComponent)
return ok
},
},
{
name: "Set custom transport",
method: func(cb *ClientBuilder) *ClientBuilder {
transport := &http.Transport{}
return cb.Config().SetCustomTransport(transport)
},
expectedConfig: func(ccb *ClientConfigBase) bool {
return ccb.HttpClient().Transport() != nil
},
},
{
name: "Set timeout",
method: func(cb *ClientBuilder) *ClientBuilder {
return cb.Config().SetTimeout(5 * time.Second)
},
expectedConfig: func(ccb *ClientConfigBase) bool {
return ccb.HttpClient().Timeout() == 5*time.Second
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
cb := &ClientBuilder{
client: newClientConfigBase("https://api.example.com"),
}
if tt.setupClient != nil {
tt.setupClient(cb.client.(*ClientConfigBase))
}
// Act
result := tt.method(cb)
// Assert
assert.Equal(t, cb, result)
assert.True(t, tt.expectedConfig(cb.client.(*ClientConfigBase)))
if tt.expectedErrors != nil {
assert.Len(t, cb.client.Validations().Unwrap(), len(tt.expectedErrors))
for i, expectedErr := range tt.expectedErrors {
assert.True(t, errors.Is(cb.client.Validations().Get(i), expectedErr))
}
} else {
assert.Empty(t, cb.client.Validations().Unwrap())
}
})
}
}
func TestClientConfigBuilder_SetFollowRedirects(t *testing.T) {
// Arrange
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/redirect" {
http.Redirect(w, r, "/target", http.StatusFound)
return
}
_, _ = w.Write([]byte("OK"))
}))
defer server.Close()
tests := []struct {
name string
followRedirects bool
wantFinalURL string
wantStatusCode int
}{
{
name: "Follow Redirects",
followRedirects: true,
wantFinalURL: server.URL + "/target",
wantStatusCode: http.StatusOK,
},
{
name: "Do Not Follow Redirects",
followRedirects: false,
wantFinalURL: server.URL + "/redirect",
wantStatusCode: http.StatusFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Act
client := NewClient(server.URL).
Config().SetFollowRedirects(tt.followRedirects).
Build()
resp, err := client.GET("/redirect").Send()
// Assert
assert.NoError(t, err)
defer resp.Body().Close()
assert.Equal(t, tt.wantFinalURL, resp.Request().URL())
assert.Equal(t, tt.wantStatusCode, resp.Status().Code())
})
}
}
func TestClientConfigBuilder_SetProxy(t *testing.T) {
tests := []struct {
name string
baseURL string
proxyURL string
setupTransport func(*ClientBuilder)
assertFunc func(*testing.T, *ClientBuilder)
}{
{
name: "Set Proxy with Default Transport",
baseURL: "https://example.com",
proxyURL: "http://localhost:8080",
assertFunc: func(t *testing.T, builder *ClientBuilder) {
transport, ok := builder.client.HttpClient().Transport().(*http.Transport)
assert.True(t, ok, "Transport should be of type *http.Transport")
assert.NotNil(t, transport.Proxy, "Proxy function should be set")
},
},
{
name: "Set Proxy with Custom Transport",
baseURL: "https://example.com",
proxyURL: "http://localhost:8080",
setupTransport: func(builder *ClientBuilder) {
builder.Config().SetCustomTransport(&http.Transport{
Proxy: http.ProxyURL(&url.URL{
Scheme: "http",
Host: "localhost:9090",
}),
})
},
assertFunc: func(t *testing.T, builder *ClientBuilder) {
transport, ok := builder.client.HttpClient().Transport().(*http.Transport)
assert.True(t, ok, "Transport should be of type *http.Transport")
assert.NotNil(t, transport.Proxy, "Proxy function should be set")
proxyURL, _ := transport.Proxy(&http.Request{URL: &url.URL{Scheme: "http", Host: "example.com"}})
assert.Equal(t, "localhost:8080", proxyURL.Host, "Proxy URL should be updated to the new value")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
builder := NewClient(tt.baseURL)
if tt.setupTransport != nil {
tt.setupTransport(builder)
}
// Act
builder.Config().SetProxy(tt.proxyURL)
// Assert
tt.assertFunc(t, builder)
})
}
}