-
-
Notifications
You must be signed in to change notification settings - Fork 965
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
How to get params without route context? #76
Comments
@gpopovic can you show me the test code for that handler? it's possible to go about it a few ways, but if you're testing just a single handler that is trying to fetch URLParams from a request, then you need to make sure the URLParams object is available on the request chain. Have a look at https://github.com/pressly/chi/blob/master/mux_test.go#L810-L829 |
@pkieltyka exactly what i needed. Thanks |
@pkieltyka can you please elaborate? I don't understand how to replicate the URLParams in the router context. Thank you! |
I've recently ran into the same issue. For posterity, this should work: w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
rctx := chi.NewRouteContext()
rctx.URLParams.Add("key", "value")
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
handler := func(w http.ResponseWriter, r *http.Request) {
key := chi.URLParam(r, "key") // "value"
}
handler(w, r) Adapted from: Lines 1143 to 1145 in 91a3777
|
@soedar solution works, thanks a lot! Anyway I don't think chi should panic if the context is not available like it does with:
Maybe if there is no context |
Is it possible to get param from the handler without route context?
When i'm testing handler, i need to be able to retrieve params with chi.URLParam(), but when i try to do it outside router, i'm getting panic
The text was updated successfully, but these errors were encountered: