-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
request Params not populated when handler invoked directly during testing #46
Comments
This is expected, because you skip the whole routing process which is responsible for extracting route params. Also, we can't extract params without having any routes. The simplest test looks like this and uses router := New()
router.GET("/user/:param", func(w http.ResponseWriter, req Request) error {
value1 := req.Param("param")
require.Equal(t, "hello", value1)
return nil
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/user/hello", nil)
router.ServeHTTP(w, req) Does that work for you? |
Yes, after opening this issue I was searching through the pkg and found
I'm curious if there's a way to extract the params without going through The downside to using |
That is a very good point. With the new version you should be able to do this: w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/user/hello", nil)
err := router.ServeHTTPError(w, req)
// check the err here |
Using a test request generator like this, from the docs:
then generating a
bunrouter.Request
with:and expecting it to match based on the following rule:
code within my
GetUser
handler cannot access the params when testing, i.e. passing thebunrouter.Request
and anhttptest.ResponseRecorder
into the handler directly.I've confirmed the
Params
work as expected when invoking the server from an actual client, but from what I can tell theParams
map and nodes are all empty/nil when the handler is invoked directly using a test request.For example, I've got this line at the top of my
GetUser
handler:When the handler is invoked via the router from a real client API call, it prints:
When the handler is invoked from a test suite, using a
bunrouter.Request
, it prints:The text was updated successfully, but these errors were encountered: