From 99155fc5a73c9d7b197d458eec82a5d825416c05 Mon Sep 17 00:00:00 2001 From: Roland <33993199+rolznz@users.noreply.github.com> Date: Fri, 19 Sep 2025 12:06:21 +0700 Subject: [PATCH] docs: add testing middleware and handler section Added a section on testing middleware and handlers with example code - this is useful for testing the entire request flow. --- website/docs/guide/testing.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/website/docs/guide/testing.md b/website/docs/guide/testing.md index cc07c501..ffac7466 100644 --- a/website/docs/guide/testing.md +++ b/website/docs/guide/testing.md @@ -156,3 +156,19 @@ req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil) *TBD* For now you can look into built-in middleware [test cases](https://github.com/labstack/echo/tree/master/middleware). + +## Testing Middleware and Handler + +```go +e := echo.New() +// ... register your middleware and handlers as normal + +req := httptest.NewRequest(http.MethodGet, "/api/apps", nil) +req.Header.Set("Authorization", "Bearer " + yourToken) // e.g. if you use JWT middleware +rec := httptest.NewRecorder() +e.ServeHTTP(rec, req) + +// Assertions + +assert.Equal(t, http.StatusOK, rec.Code) +```