If you have to set request context parameters in unit tests for your HTTP handler, you can use simple example below.


Test


handler := your HTTP handler

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

ctx := r.Context()
ctx = context.WithValue(ctx, "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.


fmt.Println(r.Context().Value("uuid"))