Skip to content

Commit 2117d43

Browse files
committed
Address lint feedback for transport helpers
1 parent 813b919 commit 2117d43

3 files changed

Lines changed: 48 additions & 28 deletions

File tree

client/client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ func (c *Client) CloseIdleConnections() {
9191
c.transport.CloseIdleConnections()
9292
}
9393

94-
func (c *Client) tlsConfig() *tls.Config {
94+
func (c *Client) currentTLSConfig() *tls.Config {
9595
return c.transport.TLSConfig()
9696
}
9797

98-
func (c *Client) setTLSConfig(config *tls.Config) {
98+
func (c *Client) applyTLSConfig(config *tls.Config) {
9999
c.transport.SetTLSConfig(config)
100100
}
101101

102-
func (c *Client) setDial(dial fasthttp.DialFunc) {
102+
func (c *Client) applyDial(dial fasthttp.DialFunc) {
103103
c.transport.SetDial(dial)
104104
}
105105

@@ -233,17 +233,17 @@ func (c *Client) SetCBORUnmarshal(f utils.CBORUnmarshal) *Client {
233233
// TLSConfig returns the client's TLS configuration.
234234
// If none is set, it initializes a new one.
235235
func (c *Client) TLSConfig() *tls.Config {
236-
if cfg := c.tlsConfig(); cfg != nil {
236+
if cfg := c.currentTLSConfig(); cfg != nil {
237237
return cfg
238238
}
239239
cfg := &tls.Config{MinVersion: tls.VersionTLS12}
240-
c.setTLSConfig(cfg)
240+
c.applyTLSConfig(cfg)
241241
return cfg
242242
}
243243

244244
// SetTLSConfig sets the TLS configuration for the client.
245245
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
246-
c.setTLSConfig(config)
246+
c.applyTLSConfig(config)
247247
return c
248248
}
249249

@@ -301,7 +301,7 @@ func (c *Client) SetRootCertificateFromString(pem string) *Client {
301301

302302
// SetProxyURL sets the proxy URL for the client. This affects all subsequent requests.
303303
func (c *Client) SetProxyURL(proxyURL string) error {
304-
c.setDial(fasthttpproxy.FasthttpHTTPDialer(proxyURL))
304+
c.applyDial(fasthttpproxy.FasthttpHTTPDialer(proxyURL))
305305
return nil
306306
}
307307

@@ -583,7 +583,7 @@ func (c *Client) SetDial(dial fasthttp.DialFunc) *Client {
583583
c.mu.Lock()
584584
defer c.mu.Unlock()
585585

586-
c.setDial(dial)
586+
c.applyDial(dial)
587587
return c
588588
}
589589

client/client_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ func TestClientCBORUnmarshalOverride(t *testing.T) {
164164

165165
called := false
166166
custom := func(b []byte, v any) error {
167+
_ = b
168+
_ = v
167169
called = true
168170
return nil
169171
}
@@ -245,8 +247,8 @@ func TestClientResetClearsState(t *testing.T) {
245247
require.False(t, client.disablePathNormalizing)
246248
require.Nil(t, client.cookieJar)
247249
require.Nil(t, jar.hostCookies)
248-
require.Equal(t, 0, len(*client.path))
249-
require.Equal(t, 0, len(*client.cookies))
250+
require.Empty(t, *client.path)
251+
require.Empty(t, *client.cookies)
250252
require.Equal(t, 0, client.header.Len())
251253
require.Equal(t, 0, client.params.Len())
252254
}
@@ -376,7 +378,7 @@ func Test_Client_HostClient_Behavior(t *testing.T) {
376378
})
377379

378380
go func() {
379-
assert.NoError(t, app.Listener(ln, fiber.ListenConfig{ //nolint:errcheck // handled in assertion
381+
assert.NoError(t, app.Listener(ln, fiber.ListenConfig{
380382
DisableStartupMessage: true,
381383
}))
382384
}()
@@ -404,6 +406,7 @@ func Test_Client_HostClient_Behavior(t *testing.T) {
404406

405407
var called int32
406408
customDial := func(addr string) (net.Conn, error) {
409+
_ = addr
407410
atomic.AddInt32(&called, 1)
408411
return nil, errors.New("dial")
409412
}
@@ -428,6 +431,7 @@ func Test_Client_HostClient_Behavior(t *testing.T) {
428431
var dialCalls int32
429432
dialErr := errors.New("dial failed")
430433
client.HostClient().Dial = func(addr string) (net.Conn, error) {
434+
_ = addr
431435
atomic.AddInt32(&dialCalls, 1)
432436
return nil, dialErr
433437
}
@@ -539,14 +543,16 @@ func Test_Client_LBClient_Behavior(t *testing.T) {
539543
})
540544

541545
go func() {
542-
assert.NoError(t, app.Listener(ln, fiber.ListenConfig{ //nolint:errcheck // handled in assertion
546+
assert.NoError(t, app.Listener(ln, fiber.ListenConfig{
543547
DisableStartupMessage: true,
544548
}))
545549
}()
546550
time.Sleep(1 * time.Second)
547551

548552
lb := newLBClient(ln.Addr().String())
549-
lb.Clients[0].(*fasthttp.HostClient).IsTLS = true
553+
host, ok := lb.Clients[0].(*fasthttp.HostClient)
554+
require.True(t, ok)
555+
host.IsTLS = true
550556
client := NewWithLBClient(lb)
551557

552558
resp, err := client.SetTLSConfig(clientTLSConf).Get("https://" + ln.Addr().String())
@@ -579,6 +585,7 @@ func Test_Client_LBClient_Behavior(t *testing.T) {
579585

580586
var called int32
581587
customDial := func(addr string) (net.Conn, error) {
588+
_ = addr
582589
atomic.AddInt32(&called, 1)
583590
return nil, errors.New("dial")
584591
}
@@ -611,6 +618,7 @@ func Test_Client_LBClient_Behavior(t *testing.T) {
611618
for _, bc := range client.LBClient().Clients {
612619
if hc, ok := bc.(*fasthttp.HostClient); ok {
613620
hc.Dial = func(addr string) (net.Conn, error) {
621+
_ = addr
614622
atomic.AddInt32(&dialCalls, 1)
615623
return nil, dialErr
616624
}

client/transport_test.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@ func (l *lbBalancingClient) DoDeadline(req *fasthttp.Request, resp *fasthttp.Res
3030
return l.client.DoDeadline(req, resp, deadline)
3131
}
3232

33-
func (l *lbBalancingClient) PendingRequests() int { return 0 }
33+
func (*lbBalancingClient) PendingRequests() int { return 0 }
3434

3535
func (l *lbBalancingClient) LBClient() *fasthttp.LBClient { return l.client }
3636

3737
type stubRedirectCall struct {
38-
status int
39-
location string
4038
err error
39+
status *int
40+
location *string
4141
}
4242

43+
func ptrInt(v int) *int { return &v }
44+
45+
func ptrString(v string) *string { return &v }
46+
4347
type stubRedirectClient struct {
4448
calls []stubRedirectCall
4549
}
4650

4751
func (s *stubRedirectClient) Do(req *fasthttp.Request, resp *fasthttp.Response) error {
52+
_ = req
4853
if len(s.calls) == 0 {
4954
resp.Reset()
5055
resp.Header.SetStatusCode(fasthttp.StatusOK)
@@ -55,11 +60,11 @@ func (s *stubRedirectClient) Do(req *fasthttp.Request, resp *fasthttp.Response)
5560
s.calls = s.calls[1:]
5661

5762
resp.Reset()
58-
if call.status != 0 {
59-
resp.Header.SetStatusCode(call.status)
63+
if call.status != nil {
64+
resp.Header.SetStatusCode(*call.status)
6065
}
61-
if call.location != "" {
62-
resp.Header.Set("Location", call.location)
66+
if call.location != nil {
67+
resp.Header.Set("Location", *call.location)
6368
}
6469
return call.err
6570
}
@@ -70,6 +75,7 @@ func TestStandardClientTransportCoverage(t *testing.T) {
7075
var dialCount atomic.Int32
7176
client := &fasthttp.Client{}
7277
client.Dial = func(addr string) (net.Conn, error) {
78+
_ = addr
7379
dialCount.Add(1)
7480
return nil, errors.New("dial error")
7581
}
@@ -124,14 +130,16 @@ func TestLBClientTransportAccessorsAndOverrides(t *testing.T) {
124130

125131
hostWithoutOverrides := &fasthttp.HostClient{Addr: "example.com:80"}
126132
nestedDialHost := &fasthttp.HostClient{Addr: "example.org:80"}
127-
nestedTLSHost := &fasthttp.HostClient{Addr: "example.net:80", TLSConfig: &tls.Config{ServerName: "example"}}
133+
nestedTLSHost := &fasthttp.HostClient{Addr: "example.net:80", TLSConfig: &tls.Config{ServerName: "example", MinVersion: tls.VersionTLS12}}
128134
multiLevelHost := &fasthttp.HostClient{Addr: "example.edu:80"}
129135

130136
nestedDialHost.Dial = func(addr string) (net.Conn, error) {
137+
_ = addr
131138
return nil, errors.New("original dial")
132139
}
133140

134141
multiLevelHost.Dial = func(addr string) (net.Conn, error) {
142+
_ = addr
135143
return nil, errors.New("multi-level dial")
136144
}
137145

@@ -148,7 +156,7 @@ func TestLBClientTransportAccessorsAndOverrides(t *testing.T) {
148156
require.Equal(t, nestedTLSHost.TLSConfig, transport.TLSConfig())
149157
require.NotNil(t, transport.dial)
150158

151-
overrideTLS := &tls.Config{ServerName: "override"}
159+
overrideTLS := &tls.Config{ServerName: "override", MinVersion: tls.VersionTLS12}
152160
transport.SetTLSConfig(overrideTLS)
153161
require.Equal(t, overrideTLS, hostWithoutOverrides.TLSConfig)
154162
require.Equal(t, overrideTLS, nestedDialHost.TLSConfig)
@@ -158,6 +166,7 @@ func TestLBClientTransportAccessorsAndOverrides(t *testing.T) {
158166

159167
overrideDialCalled := atomic.Bool{}
160168
overrideDial := func(addr string) (net.Conn, error) {
169+
_ = addr
161170
overrideDialCalled.Store(true)
162171
return nil, errors.New("override dial")
163172
}
@@ -191,10 +200,10 @@ func TestExtractTLSConfigVariations(t *testing.T) {
191200
require.Nil(t, extractTLSConfig(nil))
192201
require.Nil(t, extractTLSConfig([]fasthttp.BalancingClient{stubBalancingClient{}}))
193202

194-
host := &fasthttp.HostClient{TLSConfig: &tls.Config{ServerName: "configured"}}
203+
host := &fasthttp.HostClient{TLSConfig: &tls.Config{ServerName: "configured", MinVersion: tls.VersionTLS12}}
195204
require.Equal(t, host.TLSConfig, extractTLSConfig([]fasthttp.BalancingClient{host}))
196205

197-
nested := &fasthttp.HostClient{TLSConfig: &tls.Config{ServerName: "nested"}}
206+
nested := &fasthttp.HostClient{TLSConfig: &tls.Config{ServerName: "nested", MinVersion: tls.VersionTLS12}}
198207
nestedLB := &lbBalancingClient{client: &fasthttp.LBClient{Clients: []fasthttp.BalancingClient{nested}}}
199208
require.Equal(t, nested.TLSConfig, extractTLSConfig([]fasthttp.BalancingClient{nestedLB}))
200209

@@ -212,6 +221,7 @@ func TestExtractDialVariations(t *testing.T) {
212221
hostWithDial := &fasthttp.HostClient{}
213222
called := atomic.Bool{}
214223
hostWithDial.Dial = func(addr string) (net.Conn, error) {
224+
_ = addr
215225
called.Store(true)
216226
return nil, errors.New("dial")
217227
}
@@ -225,6 +235,7 @@ func TestExtractDialVariations(t *testing.T) {
225235
nestedHost := &fasthttp.HostClient{}
226236
nestedCalled := atomic.Bool{}
227237
nestedHost.Dial = func(addr string) (net.Conn, error) {
238+
_ = addr
228239
nestedCalled.Store(true)
229240
return nil, errors.New("nested dial")
230241
}
@@ -238,6 +249,7 @@ func TestExtractDialVariations(t *testing.T) {
238249
multiNestedHost := &fasthttp.HostClient{}
239250
multiCalled := atomic.Bool{}
240251
multiNestedHost.Dial = func(addr string) (net.Conn, error) {
252+
_ = addr
241253
multiCalled.Store(true)
242254
return nil, errors.New("multi nested dial")
243255
}
@@ -283,20 +295,20 @@ func TestDoRedirectsWithClientBranches(t *testing.T) {
283295

284296
req.SetRequestURI("http://example.com/start")
285297

286-
client := &stubRedirectClient{calls: []stubRedirectCall{{status: fasthttp.StatusMovedPermanently, location: "/next"}}}
298+
client := &stubRedirectClient{calls: []stubRedirectCall{{status: ptrInt(fasthttp.StatusMovedPermanently), location: ptrString("/next")}}}
287299
require.ErrorIs(t, doRedirectsWithClient(req, resp, -1, client), fasthttp.ErrTooManyRedirects)
288300

289301
req.Header.SetMethod(fasthttp.MethodPost)
290302
req.SetRequestURI("http://example.com/start")
291-
client = &stubRedirectClient{calls: []stubRedirectCall{{status: fasthttp.StatusFound}}}
303+
client = &stubRedirectClient{calls: []stubRedirectCall{{status: ptrInt(fasthttp.StatusFound)}}}
292304
require.ErrorIs(t, doRedirectsWithClient(req, resp, 1, client), fasthttp.ErrMissingLocation)
293305

294306
req.Header.SetMethod(fasthttp.MethodPost)
295307
req.SetRequestURI("http://example.com/start")
296308
req.SetBodyString("payload")
297-
client = &stubRedirectClient{calls: []stubRedirectCall{{status: fasthttp.StatusMovedPermanently, location: "/redirect"}, {status: fasthttp.StatusOK}}}
309+
client = &stubRedirectClient{calls: []stubRedirectCall{{status: ptrInt(fasthttp.StatusMovedPermanently), location: ptrString("/redirect")}, {status: ptrInt(fasthttp.StatusOK)}}}
298310
require.NoError(t, doRedirectsWithClient(req, resp, 1, client))
299311
require.Equal(t, fasthttp.MethodGet, string(req.Header.Method()))
300312
require.Equal(t, "http://example.com/redirect", req.URI().String())
301-
require.Len(t, req.Body(), 0)
313+
require.Empty(t, req.Body())
302314
}

0 commit comments

Comments
 (0)