Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby authored Sep 8, 2024
2 parents 65948c0 + f668537 commit 12b8bf0
Show file tree
Hide file tree
Showing 38 changed files with 1,240 additions and 371 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
# NOTE: Keep this in sync with the version from .golangci.yml
version: v1.60.1
version: v1.60.3
4 changes: 0 additions & 4 deletions .github/workflows/markdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ on:
branches:
- master
- main
paths:
- "**/*.md"
pull_request:
paths:
- "**/*.md"

jobs:
markdownlint:
Expand Down
11 changes: 5 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ linters:
- depguard
- dogsled
# - dupl
# - dupword # TODO: Enable
- dupword # TODO: Enable
- durationcheck
- errcheck
- errchkjson
Expand All @@ -287,7 +287,7 @@ linters:
- exhaustive
# - exhaustivestruct
# - exhaustruct
- exportloopref
- copyloopvar
- forbidigo
- forcetypeassert
# - funlen
Expand All @@ -298,7 +298,7 @@ linters:
# - gochecknoinits # TODO: Enable
- gochecksumtype
# - gocognit
# - goconst # TODO: Enable
- goconst # TODO: Enable
- gocritic
# - gocyclo
# - godot
Expand All @@ -307,9 +307,8 @@ linters:
- gofmt
- gofumpt
# - goheader
# - goimports
# - golint
# - gomnd # TODO: Enable
- goimports
# - mnd # TODO: Enable
- gomoddirectives
# - gomodguard
- goprintffuncname
Expand Down
18 changes: 8 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ coverage:
format:
go run mvdan.cc/gofumpt@latest -w -l .

## format: 🎨 Find markdown format issues (Requires markdownlint-cli)
## markdown: 🎨 Find markdown format issues (Requires markdownlint-cli)
.PHONY: markdown
markdown:
markdownlint-cli2 "**/*.md" "#vendor"

## lint: 🚨 Run lint checks
.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/[email protected].1 run ./...
go run github.com/golangci/golangci-lint/cmd/[email protected].3 run ./...

## test: 🚦 Execute all tests
.PHONY: test
Expand All @@ -57,11 +57,9 @@ tidy:
betteralign:
go run github.com/dkorunic/betteralign/cmd/betteralign@latest -test_files -generated_files -apply ./...

## tidy: ⚡️ Generate msgp
.PHONY: msgp
msgp:
go run github.com/tinylib/msgp@latest -file="middleware/cache/manager.go" -o="middleware/cache/manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/session/data.go" -o="middleware/session/data_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/csrf/storage_manager.go" -o="middleware/csrf/storage_manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/limiter/manager.go" -o="middleware/limiter/manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/idempotency/response.go" -o="middleware/idempotency/response_msgp.go" -tests=true -unexported
## generate: ⚡️ Generate msgp && interface implementations
.PHONY: generate
generate:
go install github.com/tinylib/msgp@latest
go install github.com/vburenin/ifacemaker@975a95966976eeb2d4365a7fb236e274c54da64c
go generate ./...
2 changes: 0 additions & 2 deletions addon/retry/exponential_backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func Test_ExponentialBackoff_Retry(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.expBackoff.Retry(tt.f)
Expand Down Expand Up @@ -106,7 +105,6 @@ func Test_ExponentialBackoff_Next(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
for i := 0; i < tt.expBackoff.MaxRetryCount; i++ {
Expand Down
4 changes: 4 additions & 0 deletions binder/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (b *formBinding) Bind(reqCtx *fasthttp.RequestCtx, out any) error {
}
})

if err != nil {
return err
}

return parse(b.Name(), out, data)
}

Expand Down
23 changes: 3 additions & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
urlpkg "net/url"
"os"
"path/filepath"
"sync"
Expand All @@ -20,12 +18,10 @@ import (
"github.com/gofiber/utils/v2"

"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)

var (
ErrInvalidProxyURL = errors.New("invalid proxy url scheme")
ErrFailedToAppendCert = errors.New("failed to append certificate")
)
var ErrFailedToAppendCert = errors.New("failed to append certificate")

// The Client is used to create a Fiber Client with
// client-level settings that apply to all requests
Expand Down Expand Up @@ -58,9 +54,6 @@ type Client struct {
userAgent string
referer string

// proxy
proxyURL string

// user defined request hooks
userRequestHooks []RequestHook

Expand Down Expand Up @@ -229,16 +222,7 @@ 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) error {
pURL, err := urlpkg.Parse(proxyURL)
if err != nil {
return fmt.Errorf("client: %w", err)
}

if pURL.Scheme != "http" && pURL.Scheme != "https" {
return fmt.Errorf("client: %w", ErrInvalidProxyURL)
}

c.proxyURL = pURL.String()
c.fasthttp.Dial = fasthttpproxy.FasthttpHTTPDialer(proxyURL)

return nil
}
Expand Down Expand Up @@ -582,7 +566,6 @@ func (c *Client) Reset() {
c.timeout = 0
c.userAgent = ""
c.referer = ""
c.proxyURL = ""
c.retryConfig = nil
c.debug = false

Expand Down
67 changes: 53 additions & 14 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)

func startTestServerWithPort(t *testing.T, beforeStarting func(app *fiber.App)) (*fiber.App, string) {
Expand Down Expand Up @@ -1539,11 +1540,53 @@ func Test_Client_SetProxyURL(t *testing.T) {

app, dial, start := createHelperServer(t)
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("hello world")
return c.SendString(c.Get("isProxy"))
})

go start()

fasthttpClient := &fasthttp.Client{
Dial: dial,
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
}

// Create a simple proxy sever
proxyServer := fiber.New()

proxyServer.Use("*", func(c fiber.Ctx) error {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()

req.SetRequestURI(c.BaseURL())
req.Header.SetMethod(fasthttp.MethodGet)

c.Request().Header.VisitAll(func(key, value []byte) {
req.Header.AddBytesKV(key, value)
})

req.Header.Set("isProxy", "true")

if err := fasthttpClient.Do(req, resp); err != nil {
return err
}

c.Status(resp.StatusCode())
c.Context().SetBody(resp.Body())

return nil
})

addrChan := make(chan string)
go func() {
assert.NoError(t, proxyServer.Listen(":0", fiber.ListenConfig{
DisableStartupMessage: true,
ListenerAddrFunc: func(addr net.Addr) {
addrChan <- addr.String()
},
}))
}()

t.Cleanup(func() {
require.NoError(t, app.Shutdown())
})
Expand All @@ -1552,31 +1595,27 @@ func Test_Client_SetProxyURL(t *testing.T) {

t.Run("success", func(t *testing.T) {
t.Parallel()
client := New().SetDial(dial)
err := client.SetProxyURL("http://test.com")

require.NoError(t, err)

_, err = client.Get("http://localhost:3000")
client := New()
err := client.SetProxyURL(<-addrChan)

require.NoError(t, err)
})

t.Run("wrong url", func(t *testing.T) {
t.Parallel()
client := New()

err := client.SetProxyURL(":this is not a url")
resp, err := client.Get("http://localhost:3000")
require.NoError(t, err)

require.Error(t, err)
require.Equal(t, 200, resp.StatusCode())
require.Equal(t, "true", string(resp.Body()))
})

t.Run("error", func(t *testing.T) {
t.Parallel()
client := New()

err := client.SetProxyURL("htgdftp://test.com")
err := client.SetProxyURL(":this is not a proxy")
require.NoError(t, err)

_, err = client.Get("http://localhost:3000")
require.Error(t, err)
})
}
Expand Down
1 change: 0 additions & 1 deletion client/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func Test_AddMissing_Port(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt // create a new 'tt' variable for the goroutine
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
require.Equal(t, tt.want, addMissingPort(tt.args.addr, tt.args.isTLS))
Expand Down
3 changes: 1 addition & 2 deletions client/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func Test_Rand_String(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := randString(tt.args)
Expand Down Expand Up @@ -188,7 +187,7 @@ func Test_Parser_Request_URL(t *testing.T) {
flag1 = true
case "foo2":
flag2 = true
case "foo":
case "foo": //nolint:goconst // test
flag3 = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ func Test_SetValWithStruct(t *testing.T) {

require.True(t, func() bool {
for _, v := range p.PeekMulti("TSlice") {
if string(v) == "bar" {
if string(v) == "bar" { //nolint:goconst // test
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type DefaultCtx struct {
pathOriginal string // Original HTTP path
pathBuffer []byte // HTTP path buffer
detectionPathBuffer []byte // HTTP detectionPath buffer
redirectionMessages []string // Messages of the previous redirect
flashMessages redirectionMsgs // Flash messages
indexRoute int // Index of the current route
indexHandler int // Index of the current handler
methodINT int // HTTP method INT equivalent
Expand Down Expand Up @@ -1896,7 +1896,7 @@ func (c *DefaultCtx) release() {
c.route = nil
c.fasthttp = nil
c.bind = nil
c.redirectionMessages = c.redirectionMessages[:0]
c.flashMessages = c.flashMessages[:0]
c.viewBindMap = sync.Map{}
if c.redirect != nil {
ReleaseRedirect(c.redirect)
Expand Down
1 change: 1 addition & 0 deletions ctx_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,6 @@ func Test_Ctx_UserContext_Multiple_Requests(t *testing.T) {

// Consecutive Requests
for i := 1; i <= 10; i++ {
i := i
t.Run(fmt.Sprintf("request_%d", i), func(t *testing.T) {
t.Parallel()
resp, err := app.Test(httptest.NewRequest(MethodGet, fmt.Sprintf("/?input=%d", i), nil))
Expand Down Expand Up @@ -3122,8 +3121,6 @@ func Test_Static_Compress(t *testing.T) {
// Note: deflate is not supported by fasthttp.FS
algorithms := []string{"zstd", "gzip", "br"}
for _, algo := range algorithms {
algo := algo

t.Run(algo+"_compression", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3338,7 +3335,6 @@ func Test_Ctx_SendFile_Immutable(t *testing.T) {
}

for _, endpoint := range endpointsForTest {
endpoint := endpoint
t.Run(endpoint, func(t *testing.T) {
t.Parallel()
// 1st try
Expand Down
Loading

0 comments on commit 12b8bf0

Please sign in to comment.