-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
144 lines (118 loc) · 4.37 KB
/
Copy pathclient_test.go
File metadata and controls
144 lines (118 loc) · 4.37 KB
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
package sprites
import (
"context"
"net"
"net/http"
"testing"
"time"
)
func TestClientOptions(t *testing.T) {
// Test default client
client := New("test-token")
if client.baseURL != "https://api.sprites.dev" {
t.Errorf("default baseURL = %q, want %q", client.baseURL, "https://api.sprites.dev")
}
if client.token != "test-token" {
t.Errorf("token = %q, want %q", client.token, "test-token")
}
// Test with custom base URL
client = New("test-token", WithBaseURL("http://localhost:8080"))
if client.baseURL != "http://localhost:8080" {
t.Errorf("custom baseURL = %q, want %q", client.baseURL, "http://localhost:8080")
}
}
// unwrapTransport extracts the underlying *http.Transport from a Client's
// httpClient, looking through the versionCapturingTransport wrapper.
func unwrapTransport(t *testing.T, c *Client) *http.Transport {
t.Helper()
vct, ok := c.httpClient.Transport.(*versionCapturingTransport)
if !ok {
t.Fatalf("expected c.httpClient.Transport to be *versionCapturingTransport, got %T", c.httpClient.Transport)
}
transport, ok := vct.wrapped.(*http.Transport)
if !ok {
t.Fatalf("expected wrapped transport to be *http.Transport, got %T", vct.wrapped)
}
return transport
}
func TestNew_DefaultTransportIsNotSharedGlobal(t *testing.T) {
c := New("test-token")
transport := unwrapTransport(t, c)
if transport == http.DefaultTransport {
t.Fatal("New() must not fall back to the shared http.DefaultTransport")
}
if transport.MaxIdleConnsPerHost <= 0 {
t.Fatalf("expected a pooled transport (MaxIdleConnsPerHost > 0), got %d", transport.MaxIdleConnsPerHost)
}
if transport.DisableKeepAlives {
t.Fatal("expected a pooled transport with keepalives enabled")
}
}
func TestNew_WithHTTPClientNilDoesNotPanic(t *testing.T) {
c := New("test-token", WithHTTPClient(nil))
if c.httpClient == nil {
t.Fatal("expected New() to restore a non-nil httpClient when WithHTTPClient(nil) is used")
}
transport := unwrapTransport(t, c)
if transport == http.DefaultTransport {
t.Fatal("New() must not fall back to the shared http.DefaultTransport")
}
}
func TestWithNetDialContext_NilHTTPClientDoesNotPanic(t *testing.T) {
fn := func(ctx context.Context, network, addr string) (net.Conn, error) {
return nil, nil
}
c := New("test-token", WithHTTPClient(nil), WithNetDialContext(fn))
transport := unwrapTransport(t, c)
if transport.DialContext == nil {
t.Fatal("expected DialContext to be set")
}
}
func TestWithNetDialContext_PreservesExistingTransportSettings(t *testing.T) {
const distinguishingTimeout = 42 * time.Second
customTransport := &http.Transport{
TLSHandshakeTimeout: distinguishingTimeout,
}
fn := func(ctx context.Context, network, addr string) (net.Conn, error) {
return nil, nil
}
c := New("test-token",
WithHTTPClient(&http.Client{Transport: customTransport}),
WithNetDialContext(fn),
)
transport := unwrapTransport(t, c)
if transport.TLSHandshakeTimeout != distinguishingTimeout {
t.Fatalf("expected WithNetDialContext to preserve the existing transport's TLSHandshakeTimeout, got %v", transport.TLSHandshakeTimeout)
}
if transport.DialContext == nil {
t.Fatal("expected DialContext to be set")
}
if transport == customTransport {
t.Fatal("expected WithNetDialContext to clone the existing transport, not mutate it in place")
}
}
func TestWithNetDialContext_OrderIndependentOfWithHTTPClient(t *testing.T) {
const distinguishingTimeout = 42 * time.Second
customTransport := &http.Transport{
TLSHandshakeTimeout: distinguishingTimeout,
}
fn := func(ctx context.Context, network, addr string) (net.Conn, error) {
return nil, nil
}
// WithNetDialContext is applied *before* WithHTTPClient here — the
// reverse of TestWithNetDialContext_PreservesExistingTransportSettings.
// Since netDialContext is only stored during the options loop and
// actually applied once afterward in New(), this must still take
// effect regardless of option order.
c := New("test-token",
WithNetDialContext(fn),
WithHTTPClient(&http.Client{Transport: customTransport}),
)
transport := unwrapTransport(t, c)
if transport.TLSHandshakeTimeout != distinguishingTimeout {
t.Fatalf("expected the final transport to preserve WithHTTPClient's TLSHandshakeTimeout, got %v", transport.TLSHandshakeTimeout)
}
if transport.DialContext == nil {
t.Fatal("expected DialContext to still be set even though WithNetDialContext ran before WithHTTPClient")
}
}