-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
No (Documented) way to test protected handlers #2676
Comments
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. 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
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
The text was updated successfully, but these errors were encountered: