If you are using httprouter package for routing in your Go application and need to test "named" URL path parameters, you can use example below.


Test


handler := your handler

r := httptest.NewRequest(http.MethodGet, "/api/v1/users/:uuid", nil)
w := httptest.NewRecorder()

ctx := r.Context()
ctx = context.WithValue(ctx, httprouter.ParamsKey, httprouter.Params{
{"uuid", "some-uuid"},
})
r = r.WithContext(ctx)

handler.Handle(w, r)

Confirmation


If you use code below in your handler, you will see that the some-uuid is there.


httprouter.ParamsFromContext(r.Context()).ByName("uuid")