Open apuatcfbd opened 3 months ago
Have you checked echojwt tests for examples? For example: https://github.com/labstack/echo-jwt/blob/main/jwt_extranal_test.go
Thank you for your response, @aldas. I didn't check that earlier. After reviewing it, I think that is a bit different than the docs way, where we need to start the server. I understand this might be necessary to execute registered routes & middleware.
I got some insight from that test you mentioned and managed to serve the request with e.ServeHTTP
fn. Above in "The test" inside t.Run
(for Case2
) I was getting 404
because in this new echo I do not have that route (/v1/auth/me
) which is used in the request (httptest.NewRequest
).
I've solved the issue by adding the route, which looks like the following
func TestAuthUser(t *testing.T) {
e := initEcho()
doSignup := func() (token string, user model.User) {
// sign up a new user ...
// return token & user
return response.Token, res.Data.User
}
// login
authToken, user := doSignup()
// at last delete the user
t.Cleanup(func() {
err := database.DB.Delete(&user).Error
if err != nil {
th.Fatal(t, "Failed to delete created user:", err)
}
})
// get auth user
tests := []struct {
name string
token string
wantResCode int
}{
{
name: "success with valid token",
token: authToken,
wantResCode: http.StatusOK,
},
{
name: "fail with invalid token",
token: "authToken",
wantResCode: http.StatusUnauthorized,
},
}
// [---KEY POINT 1---] register necessary middleware (which is necessary for the hander)
// here, for my case as the handler is under a protected route, I need following 2 middleware
e.Use(
middleware2.JwtAuth(),
middleware2.Acl,
)
// [---KEY POINT 2---] register the route using the handler
e.GET("/v1/auth/me", AuthUser)
for _, tt := range tests {
// request with token
req := httptest.NewRequest(http.MethodGet, "/v1/auth/me", nil)
req.Header.Set(echo.HeaderAuthorization, "Bearer "+tt.token)
// create response writer & context
rec := httptest.NewRecorder()
t.Run(tt.name, func(t *testing.T) {
// [---KEY POINT 3---] serve the request, this triggers registered routes & middlewares
e.ServeHTTP(rec, req)
if rec.Code != tt.wantResCode {
th.Errorf(t, "Response Code %d want %d for user '%s'", rec.Code, tt.wantResCode, user.Email)
}
})
}
}
Now the tests are passing
✅ PASS: TestAuthUser (0.89s)
✅ PASS: TestAuthUser/success_with_valid_token (0.00s)
✅ PASS: TestAuthUser/fail_with_invalid_token (0.00s)
I think this (or better) example for testing protected routes/ handlers should be added in the docs. That'll help newcomers.
Issue Description
Doc has a Testing section. Examples there only works with public/ unprotected routes/ handlers. In a real-world app, most of the routes are protected. Same for my case. I'm using echojwt to protect routes. Unfortunately, I've failed to test those protected routes even after googling.
Checklist
Expected behaviour
Need way/ (Doc) examples to be able to test protected handlers.
Actual behaviour
No examples/ guidelines for testing protected handlers in the doc
Steps to reproduce
Working code to debug
I don't know what this section is for
Middlewares
The handler func
The test
TL,DR: I'm new in Go & Echo. So please forgive my silly mistakes, I welcome any suggestion/ resource to learn more.
Please don't hesitate to ask any questions regarding this topic. I'm open to do what it takes to sort out this issue :).
Version/commit
go 1.22.5
github.com/labstack/echo-jwt/v4 v4.2.0
github.com/labstack/echo/v4 v4.12.0