package httpfetcher import ( "context" "io" "net/http" "net/http/httptest" "testing" "github.com/go-chi/chi/v5/middleware" ) // TestFetchSendsRequestID verifies that a fetch sends the ID of the request // it serves, which the RequestID middleware stores in the request context, // to the upstream host as X-Request-Id, so the fetch can be found in that // host's logs. func TestFetchSendsRequestID(t *testing.T) { t.Parallel() const requestID = "test-request-id" received := make(chan string, 1) srv := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { received <- r.Header.Get("X-Request-Id") w.Header().Set("Content-Type", contentTypeJPEG) _, _ = io.WriteString(w, imagePayload) })) t.Cleanup(srv.Close) f, _ := newServerFetcher(t, srv, nil) ctx := context.WithValue(testContext(t), middleware.RequestIDKey, requestID) res, err := f.Fetch(ctx, upstreamURL("/image")) if err != nil { t.Fatalf("Fetch() error = %v", err) } _ = res.Content.Close() got := <-received t.Logf("upstream received X-Request-Id %q", got) if got != requestID { t.Errorf("upstream X-Request-Id = %q, want %q", got, requestID) } }