Skip to content

Commit

Permalink
Add test for custom Ctx and Request Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Dec 29, 2024
1 parent 2e5ef2c commit f0e7c89
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
12 changes: 6 additions & 6 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,24 +592,24 @@ func Test_App_Add_Method_Test(t *testing.T) {
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")

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

resp, err = app.Test(httptest.NewRequest("JANE", "/doe", nil))
resp, err = app.Test(httptest.NewRequest("JANE", "/jane", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand Down
36 changes: 36 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,42 @@ 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()

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

// 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
app.Add([]string{"JANE"}, "/doe", testEmptyHandler)
resp, err = app.Test(httptest.NewRequest("JANE", "/doe", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}

// go test -run Test_Ctx_Accepts_EmptyAccept
func Test_Ctx_Accepts_EmptyAccept(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit f0e7c89

Please sign in to comment.