23/02/2020 - GO
HTTP işleyiciniz için testlerde istek bağlamı (request context) parametreleri ayarlamanız gerekiyorsa, aşağıdaki basit örneği kullanabilirsiniz.
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)
İşleyicinizde aşağıdaki kodu kullanırsanız, some-uuid
öğesinin orada olduğunu görürsünüz.
fmt.Println(r.Context().Value("uuid"))