Skip to content

Commit

Permalink
fix some lint errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Dec 18, 2023
1 parent c34321a commit 615d898
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 329 deletions.
41 changes: 25 additions & 16 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
"crypto/x509"
"encoding/json"
"encoding/xml"
"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/utils/v2"
"fmt"
"io"
"net/url"
urlPkg "net/url"
"os"
"path/filepath"
"sort"
"sync"
"time"

"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/utils/v2"

"github.com/valyala/fasthttp"
)

Expand All @@ -41,13 +43,13 @@ var _ (Logger) = (*disableLogger)(nil)
// All logs are turned off by default.
type disableLogger struct{}

func (*disableLogger) Errorf(format string, args ...any) {}
func (*disableLogger) Errorf(_ string, _ ...any) {}

func (*disableLogger) Warnf(format string, args ...any) {}
func (*disableLogger) Warnf(_ string, _ ...any) {}

func (*disableLogger) Infof(format string, args ...any) {}
func (*disableLogger) Infof(_ string, _ ...any) {}

func (*disableLogger) Debugf(format string, args ...any) {}
func (*disableLogger) Debugf(_ string, _ ...any) {}

// The Client is used to create a Fiber Client with
// client-level settings that apply to all requests
Expand All @@ -58,7 +60,7 @@ func (*disableLogger) Debugf(format string, args ...any) {}
type Client struct {
mu sync.RWMutex

baseUrl string
baseURL string
userAgent string
referer string
header *Header
Expand Down Expand Up @@ -209,7 +211,9 @@ func (c *Client) SetRootCertificate(path string) *Client {
if err != nil {
return c
}
defer func() { _ = file.Close() }()
defer func() {
_ = file.Close() //nolint:errcheck // It is fine to ignore the error here
}()
pem, err := io.ReadAll(file)
if err != nil {
return c
Expand Down Expand Up @@ -238,13 +242,13 @@ func (c *Client) SetRootCertificateFromString(pem string) *Client {

// SetProxyURL sets proxy url in client. It will apply via core to hostclient.
func (c *Client) SetProxyURL(proxyURL string) *Client {
pUrl, err := url.Parse(proxyURL)
pURL, err := urlPkg.Parse(proxyURL)
if err != nil {
log.Errorf("%v", err)

return c
}
c.proxyURL = pUrl.String()
c.proxyURL = pURL.String()

return c
}
Expand All @@ -264,12 +268,12 @@ func (c *Client) SetRetryConfig(config *RetryConfig) *Client {

// BaseURL returns baseurl in Client instance.
func (c *Client) BaseURL() string {
return c.baseUrl
return c.baseURL
}

// Set baseUrl which is prefix of real url.
func (c *Client) SetBaseURL(url string) *Client {
c.baseUrl = url
c.baseURL = url
return c
}

Expand All @@ -282,7 +286,7 @@ func (c *Client) Header(key string) []string {

// AddHeader method adds a single header field and its value in the client instance.
// These headers will be applied to all requests raised from this client instance.
// Also it can be overridden at request level header options.
// Also, it can be overridden at request level header options.
func (c *Client) AddHeader(key, val string) *Client {
c.header.Add(key, val)
return c
Expand Down Expand Up @@ -553,7 +557,7 @@ func (c *Client) Patch(url string, cfg ...Config) (*Response, error) {

// Reset clear Client object
func (c *Client) Reset() {
c.baseUrl = ""
c.baseURL = ""
c.timeout = 0
c.userAgent = ""
c.referer = ""
Expand Down Expand Up @@ -691,7 +695,12 @@ func init() {
// The returned Client object may be returned to the pool with ReleaseClient when no longer needed.
// This allows reducing GC load.
func AcquireClient() *Client {
return clientPool.Get().(*Client)
client, ok := clientPool.Get().(*Client)
if !ok {
panic(fmt.Errorf("failed to type-assert to *Client"))
}

return client
}

// ReleaseClient returns the object acquired via AcquireClient to the pool.
Expand Down
13 changes: 7 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/gofiber/fiber/v3/addon/retry"
"github.com/gofiber/fiber/v3/log"
"io"
"net"
"os"
"reflect"
"testing"
"time"

"github.com/gofiber/fiber/v3/addon/retry"
"github.com/gofiber/fiber/v3/log"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/internal/tlstest"
"github.com/gofiber/utils/v2"
Expand Down Expand Up @@ -497,8 +498,8 @@ func Test_Client_Header_With_Server(t *testing.T) {
handler := func(c fiber.Ctx) error {
c.Request().Header.VisitAll(func(key, value []byte) {
if k := string(key); k == "K1" || k == "K2" {
_, _ = c.Write(key)
_, _ = c.Write(value)
_, _ = c.Write(key) //nolint:errcheck // It is fine to ignore the error here
_, _ = c.Write(value) //nolint:errcheck // It is fine to ignore the error here
}
})
return nil
Expand Down Expand Up @@ -829,8 +830,8 @@ func Test_Client_QueryParam(t *testing.T) {

func Test_Client_QueryParam_With_Server(t *testing.T) {
handler := func(c fiber.Ctx) error {
c.WriteString(c.Query("k1"))
c.WriteString(c.Query("k2"))
_, _ = c.WriteString(c.Query("k1")) //nolint:errcheck // It is fine to ignore the error here
_, _ = c.WriteString(c.Query("k2")) //nolint:errcheck // It is fine to ignore the error here

return nil
}
Expand Down
14 changes: 5 additions & 9 deletions client/cookiejar.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func (cj *CookieJar) Get(uri *fasthttp.URI) []*fasthttp.Cookie {
return nil
}

return cj.get(uri.Host(), uri.Path())
return cj.getByHostAndPath(uri.Host(), uri.Path())
}

// get returns the cookies stored from a specific host and path.
func (cj *CookieJar) get(host, path []byte) []*fasthttp.Cookie {
func (cj *CookieJar) getByHostAndPath(host, path []byte) []*fasthttp.Cookie {
if cj.hostCookies == nil {
return nil
}
Expand Down Expand Up @@ -108,18 +108,14 @@ func (cj *CookieJar) Set(uri *fasthttp.URI, cookies ...*fasthttp.Cookie) {
return
}

cj.set(uri.Host(), cookies...)
cj.SetByHost(uri.Host(), cookies...)
}

// SetByHost sets cookies for a specific host.
// If the cookie key already exists it will be replaced by the new cookie value.
//
// CookieJar keeps a copy of the cookies, so the parsed cookies can be released safely.
func (cj *CookieJar) SetByHost(host []byte, cookies ...*fasthttp.Cookie) {
cj.set(host, cookies...)
}

func (cj *CookieJar) set(host []byte, cookies ...*fasthttp.Cookie) {
hostStr := utils.UnsafeString(host)

cj.mu.Lock()
Expand Down Expand Up @@ -168,14 +164,14 @@ func (cj *CookieJar) setKeyValue(host string, key, value []byte) {
c.SetKeyBytes(key)
c.SetValueBytes(value)

cj.set(utils.UnsafeBytes(host), c)
cj.SetByHost(utils.UnsafeBytes(host), c)
}

// dumpCookiesToReq dumps the stored cookies to the request.
func (cj *CookieJar) dumpCookiesToReq(req *fasthttp.Request) {
uri := req.URI()

cookies := cj.get(uri.Host(), uri.Path())
cookies := cj.getByHostAndPath(uri.Host(), uri.Path())
for _, cookie := range cookies {
req.Header.SetCookieBytesKV(cookie.Key(), cookie.Value())
}
Expand Down
10 changes: 8 additions & 2 deletions client/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import (
"github.com/valyala/fasthttp/fasthttputil"
)

func createHelperServer(t testing.TB) (*fiber.App, func(addr string) (net.Conn, error), func()) {
func createHelperServer(t testing.TB, config ...fiber.Config) (*fiber.App, func(addr string) (net.Conn, error), func()) {
t.Helper()

ln := fasthttputil.NewInmemoryListener()
app := fiber.New()

var cfg fiber.Config
if len(config) > 0 {
cfg = config[0]
}

app := fiber.New(cfg)

return app, func(addr string) (net.Conn, error) {
return ln.Dial()
Expand Down
1 change: 1 addition & 0 deletions client/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func parserRequestBody(c *Client, req *Request) error {
return ErrBodyType
}
}

return nil
}

Expand Down
Loading

0 comments on commit 615d898

Please sign in to comment.