Skip to content

Commit

Permalink
organize helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch786 committed Dec 8, 2024
1 parent fe7f8d2 commit 04764d8
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,15 @@ func Test_Router_NotFound_HTML_Inject(t *testing.T) {
require.Equal(t, "Cannot DELETE /does/not/exist<script>alert('foo');</script>", string(c.Response.Body()))
}

func testRequest(tb testing.TB, app *App, path string, expectedStatus int) *http.Response {
resp, err := app.Test(httptest.NewRequest(MethodGet, path, nil))
require.NoError(tb, err, "app.Test(req)")
require.Equal(tb, expectedStatus, resp.StatusCode, "Status code")
return resp
}

func Test_App_Rebuild_Tree(t *testing.T) {
t.Parallel()
app := New()

registerTreeManipulationRoutes(app)

testRequest(t, app, "/dynamically-defined", http.StatusNotFound)
testRequest(t, app, "/test", http.StatusOK)
testRequest(t, app, "/dynamically-defined", http.StatusOK)
verifyRequest(t, app, "/dynamically-defined", http.StatusNotFound)
verifyRequest(t, app, "/test", http.StatusOK)
verifyRequest(t, app, "/dynamically-defined", http.StatusOK)
}

func Test_App_Remove_Route(t *testing.T) {
Expand All @@ -397,38 +390,25 @@ func Test_App_Remove_Route(t *testing.T) {
registerTreeManipulationRoutes(app)
registerTreeManipulationRoutes(app)

testRequest(t, app, "/dynamically-defined", http.StatusNotFound)
verifyRequest(t, app, "/dynamically-defined", http.StatusNotFound)
require.Equal(t, uint32(1), app.handlersCount)

testRequest(t, app, "/test", http.StatusOK)
verifyRequest(t, app, "/test", http.StatusOK)
require.Equal(t, uint32(2), app.handlersCount)

testRequest(t, app, "/dynamically-defined", http.StatusOK)
verifyRequest(t, app, "/dynamically-defined", http.StatusOK)
require.Equal(t, uint32(2), app.handlersCount)

testRequest(t, app, "/test", http.StatusOK)
verifyRequest(t, app, "/test", http.StatusOK)
require.Equal(t, uint32(2), app.handlersCount)

testRequest(t, app, "/dynamically-defined", http.StatusOK)
verifyRequest(t, app, "/dynamically-defined", http.StatusOK)
require.Equal(t, uint32(2), app.handlersCount)
require.Equal(t, uint32(2), app.routesCount)

verifyRouteHandlerCounts(t, app)
}

func registerTreeManipulationRoutes(app *App, middleware ...func(Ctx) error) {
app.Get("/test", func(c Ctx) error {
app.Get("/dynamically-defined", func(c Ctx) error {
return c.SendStatus(http.StatusOK)
})

app.RebuildTree()

return c.SendStatus(http.StatusOK)
}, middleware...)

}

func Test_App_Remove_Route_With_Middleware(t *testing.T) {
t.Parallel()
app := New()
Expand All @@ -440,25 +420,48 @@ func Test_App_Remove_Route_With_Middleware(t *testing.T) {
registerTreeManipulationRoutes(app, middleware)
registerTreeManipulationRoutes(app)

testRequest(t, app, "/dynamically-defined", http.StatusNotFound)
verifyRequest(t, app, "/dynamically-defined", http.StatusNotFound)
require.Equal(t, uint32(2), app.handlersCount)

testRequest(t, app, "/test", http.StatusOK)
verifyRequest(t, app, "/test", http.StatusOK)
require.Equal(t, uint32(3), app.handlersCount)

testRequest(t, app, "/dynamically-defined", http.StatusOK)
verifyRequest(t, app, "/dynamically-defined", http.StatusOK)
require.Equal(t, uint32(3), app.handlersCount)

testRequest(t, app, "/test", http.StatusOK)
verifyRequest(t, app, "/test", http.StatusOK)
require.Equal(t, uint32(3), app.handlersCount)

testRequest(t, app, "/dynamically-defined", http.StatusOK)
verifyRequest(t, app, "/dynamically-defined", http.StatusOK)
require.Equal(t, uint32(3), app.handlersCount)
require.Equal(t, uint32(2), app.routesCount)

verifyRouteHandlerCounts(t, app)
}

func registerTreeManipulationRoutes(app *App, middleware ...func(Ctx) error) {
app.Get("/test", func(c Ctx) error {
app.Get("/dynamically-defined", func(c Ctx) error {
return c.SendStatus(http.StatusOK)
})

app.RebuildTree()

return c.SendStatus(http.StatusOK)
}, middleware...)

}

func verifyRequest(tb testing.TB, app *App, path string, expectedStatus int) *http.Response {
tb.Helper()

resp, err := app.Test(httptest.NewRequest(MethodGet, path, nil))
require.NoError(tb, err, "app.Test(req)")
require.Equal(tb, expectedStatus, resp.StatusCode, "Status code")

return resp
}

// this is taken from listen.go's printRoutesMessage app method
func verifyRouteHandlerCounts(tb testing.TB, app *App) {
tb.Helper()
Expand Down

0 comments on commit 04764d8

Please sign in to comment.