Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Register custom methods #2107

Merged
5 changes: 5 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ func New(config ...Config) *App {
return app
}

func (app *App) updateStack() {
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
app.stack = append(app.stack, []*Route{})
app.treeStack = append(app.treeStack, map[string][]*Route{})
}

// Adds an ip address to trustedProxyRanges or trustedProxiesMap based on whether it is an IP range or not
func (app *App) handleTrustedProxy(ipAddress string) {
if strings.Contains(ipAddress, "/") {
Expand Down
32 changes: 26 additions & 6 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,24 @@ func Test_App_Use_StrictRouting(t *testing.T) {

func Test_App_Add_Method_Test(t *testing.T) {
app := New()
defer func() {
if err := recover(); err != nil {
utils.AssertEqual(t, "add: invalid http method JOHN\n", fmt.Sprintf("%v", err))
}
}()
app.Add("JOHN", "/doe", testEmptyHandler)
app.Add("JANE", "/doe", testEmptyHandler)

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

resp, err = app.Test(httptest.NewRequest("JANE", "/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusMethodNotAllowed, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest("UNKNOWN", "/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusBadRequest, resp.StatusCode, "Status code")
}

// go test -run Test_App_GETOnly
Expand Down Expand Up @@ -1301,7 +1313,7 @@ func Test_App_Stack(t *testing.T) {
app.Post("/path3", testEmptyHandler)

stack := app.Stack()
utils.AssertEqual(t, 9, len(stack))
utils.AssertEqual(t, len(intMethod), len(stack))
utils.AssertEqual(t, 3, len(stack[methodInt(MethodGet)]))
utils.AssertEqual(t, 3, len(stack[methodInt(MethodHead)]))
utils.AssertEqual(t, 2, len(stack[methodInt(MethodPost)]))
Expand Down Expand Up @@ -1647,3 +1659,11 @@ func Test_App_SetTLSHandler(t *testing.T) {

utils.AssertEqual(t, "example.golang", c.ClientHelloInfo().ServerName)
}

func Test_App_UpdateStact(t *testing.T) {
app := New()
utils.AssertEqual(t, len(app.stack), len(intMethod))

app.updateStack()
utils.AssertEqual(t, len(app.stack), len(intMethod)+1)
}
27 changes: 6 additions & 21 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,28 +332,13 @@ var getBytesImmutable = func(s string) (b []byte) {

// HTTP methods and their unique INTs
func methodInt(s string) int {
switch s {
case MethodGet:
return 0
case MethodHead:
return 1
case MethodPost:
return 2
case MethodPut:
return 3
case MethodDelete:
return 4
case MethodConnect:
return 5
case MethodOptions:
return 6
case MethodTrace:
return 7
case MethodPatch:
return 8
default:
return -1
for i, v := range intMethod {
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
if s == v {
return i
}
}

return -1
}

// HTTP methods slice
Expand Down
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Router {
method = utils.ToUpper(method)
// Check if the HTTP method is valid unless it's USE
if method != methodUse && methodInt(method) == -1 {
panic(fmt.Sprintf("add: invalid http method %s\n", method))
intMethod = append(intMethod, method)
app.updateStack()
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
}
// A route requires atleast one ctx handler
if len(handlers) == 0 {
Expand Down