Giter Site home page Giter Site logo

Comments (5)

soedar avatar soedar commented on May 5, 2024 121

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:

chi/mux_test.go

Lines 1143 to 1145 in 91a3777

rctx := NewRouteContext()
r = r.WithContext(context.WithValue(r.Context(), RouteCtxKey, rctx))
rctx.URLParams.Add("name", "joe")

from chi.

gonzaloserrano avatar gonzaloserrano commented on May 5, 2024 12

@soedar solution works, thanks a lot!

Anyway I don't think chi should panic if the context is not available like it does with:

panic: interface conversion: interface {} is nil, not *chi.Context [recovered]
	panic: interface conversion: interface {} is nil, not *chi.Context

Maybe if there is no context URLParam() could return empty string instead?

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

@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

from chi.

gpopovic avatar gpopovic commented on May 5, 2024

@pkieltyka exactly what i needed. Thanks

from chi.

christopherdiehl avatar christopherdiehl commented on May 5, 2024

@pkieltyka can you please elaborate? I don't understand how to replicate the URLParams in the router context.

Thank you!

from chi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.