Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Erfan Momeni authored and Erfan Momeni committed Dec 29, 2024
2 parents bd4dc0c + b9b4251 commit 6d7fbc2
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 102 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.62.0
version: v1.62.2
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ markdown:
## lint: 🚨 Run lint checks
.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/[email protected].0 run ./...
go run github.com/golangci/golangci-lint/cmd/[email protected].2 run ./...

## test: 🚦 Execute all tests
.PHONY: test
Expand Down
16 changes: 14 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ func (app *App) handleTrustedProxy(ipAddress string) {
// Note: It doesn't allow adding new methods, only customizing exist methods.
func (app *App) NewCtxFunc(function func(app *App) CustomCtx) {
app.newCtxFunc = function

if app.server != nil {
app.server.Handler = app.customRequestHandler
}
}

// RegisterCustomConstraint allows to register custom constraint.
Expand Down Expand Up @@ -868,7 +872,11 @@ func (app *App) Config() Config {
func (app *App) Handler() fasthttp.RequestHandler { //revive:disable-line:confusing-naming // Having both a Handler() (uppercase) and a handler() (lowercase) is fine. TODO: Use nolint:revive directive instead. See https://github.com/golangci/golangci-lint/issues/3476
// prepare the server for the start
app.startupProcess()
return app.requestHandler

if app.newCtxFunc != nil {
return app.customRequestHandler
}
return app.defaultRequestHandler
}

// Stack returns the raw router stack.
Expand Down Expand Up @@ -1057,7 +1065,11 @@ func (app *App) init() *App {
}

// fasthttp server settings
app.server.Handler = app.requestHandler
if app.newCtxFunc != nil {
app.server.Handler = app.customRequestHandler
} else {
app.server.Handler = app.defaultRequestHandler
}
app.server.Name = app.config.ServerHeader
app.server.Concurrency = app.config.Concurrency
app.server.NoDefaultDate = app.config.DisableDefaultDate
Expand Down
39 changes: 29 additions & 10 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,32 +581,51 @@ func Test_App_Use_StrictRouting(t *testing.T) {

func Test_App_Add_Method_Test(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err != nil {
require.Equal(t, "add: invalid http method JANE\n", fmt.Sprintf("%v", err))
}
}()

methods := append(DefaultMethods, "JOHN") //nolint:gocritic // We want a new slice here
app := New(Config{
RequestMethods: methods,
})

app.Add([]string{"JOHN"}, "/doe", testEmptyHandler)
app.Add([]string{"JOHN"}, "/john", testEmptyHandler)

resp, err := app.Test(httptest.NewRequest("JOHN", "/doe", nil))
resp, err := app.Test(httptest.NewRequest("JOHN", "/john", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/doe", nil))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/john", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusMethodNotAllowed, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest("UNKNOWN", "/doe", nil))
resp, err = app.Test(httptest.NewRequest("UNKNOWN", "/john", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusNotImplemented, resp.StatusCode, "Status code")

app.Add([]string{"JANE"}, "/doe", testEmptyHandler)
// Add a new method
require.Panics(t, func() {
app.Add([]string{"JANE"}, "/jane", testEmptyHandler)
})
}

func Test_App_All_Method_Test(t *testing.T) {
t.Parallel()

methods := append(DefaultMethods, "JOHN") //nolint:gocritic // We want a new slice here
app := New(Config{
RequestMethods: methods,
})

// Add a new method with All
app.All("/doe", testEmptyHandler)

resp, err := app.Test(httptest.NewRequest("JOHN", "/doe", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

// Add a new method
require.Panics(t, func() {
app.Add([]string{"JANE"}, "/jane", testEmptyHandler)
})
}

// go test -run Test_App_GETOnly
Expand Down
4 changes: 1 addition & 3 deletions binder/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ func parseToStruct(aliasTag string, out any, data map[string][]string) error {
func parseToMap(ptr any, data map[string][]string) error {
elem := reflect.TypeOf(ptr).Elem()

//nolint:exhaustive // it's not necessary to check all types
switch elem.Kind() {
switch elem.Kind() { //nolint:exhaustive // it's not necessary to check all types
case reflect.Slice:
newMap, ok := ptr.(map[string][]string)
if !ok {
Expand All @@ -129,7 +128,6 @@ func parseToMap(ptr any, data map[string][]string) error {
newMap[k] = ""
continue
}

newMap[k] = v[len(v)-1]
}
}
Expand Down
3 changes: 3 additions & 0 deletions ctx_interface_gen.go

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

29 changes: 29 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ func Test_Ctx_CustomCtx(t *testing.T) {
require.Equal(t, "prefix_v3", string(body))
}

// go test -run Test_Ctx_CustomCtx
func Test_Ctx_CustomCtx_and_Method(t *testing.T) {
t.Parallel()

// Create app with custom request methods
methods := append(DefaultMethods, "JOHN") //nolint:gocritic // We want a new slice here
app := New(Config{
RequestMethods: methods,
})

// Create custom context
app.NewCtxFunc(func(app *App) CustomCtx {
return &customCtx{
DefaultCtx: *NewDefaultCtx(app),
}
})

// Add route with custom method
app.Add([]string{"JOHN"}, "/doe", testEmptyHandler)
resp, err := app.Test(httptest.NewRequest("JOHN", "/doe", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

// Add a new method
require.Panics(t, func() {
app.Add([]string{"JANE"}, "/jane", testEmptyHandler)
})
}

// go test -run Test_Ctx_Accepts_EmptyAccept
func Test_Ctx_Accepts_EmptyAccept(t *testing.T) {
t.Parallel()
Expand Down
12 changes: 9 additions & 3 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,16 @@ func GetTrimmedParam(param string) string {

// RemoveEscapeChar remove escape characters
func RemoveEscapeChar(word string) string {
if strings.IndexByte(word, escapeChar) != -1 {
return strings.ReplaceAll(word, string(escapeChar), "")
b := []byte(word)
dst := 0
for src := 0; src < len(b); src++ {
if b[src] == '\\' {
continue
}
b[dst] = b[src]
dst++
}
return word
return string(b[:dst])
}

func getParamConstraintType(constraintPart string) TypeConstraint {
Expand Down
Loading

0 comments on commit 6d7fbc2

Please sign in to comment.