check / check (push) Failing after 2s
Every response carries X-Request-Id, the upstream fetch sends it, and the "upstream fetched", "image converted" and "image served" lines log it as request_id. pixa's own RequestID middleware keeps a request's own ID only when it is at most 64 letters, digits, '-', '_' or '.', and otherwise makes a random one with crypto/rand, so nothing a client chooses freely and nothing about the host reaches upstream. /v1/e/ now sets ETag, answers a matching If-None-Match with 304 and is routed for HEAD, through notModified, which both image handlers call. No Vary is added: go-chi/cors already sends Vary: Origin. Model: opus-5-5
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// requestIDHeader is the header that carries a request's ID.
|
|
const requestIDHeader = "X-Request-Id"
|
|
|
|
// TestResponsesCarryRequestID verifies that every response, whatever its route
|
|
// and status, carries the request's ID as X-Request-Id, so a client can quote
|
|
// it when reporting a problem: one pixa made up when the request brought none,
|
|
// and the request's own X-Request-Id when it brought one.
|
|
func TestResponsesCarryRequestID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const clientRequestID = "client-request-id"
|
|
|
|
s := newTestServer(t)
|
|
|
|
paths := []string{
|
|
"/robots.txt",
|
|
"/no-such-path",
|
|
unsignedImagePath,
|
|
encryptedImagePath,
|
|
}
|
|
|
|
for _, path := range paths {
|
|
t.Run(path, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
rec := httptest.NewRecorder()
|
|
s.ServeHTTP(rec, httptest.NewRequestWithContext(
|
|
t.Context(), http.MethodGet, path, nil))
|
|
t.Logf("status %d, %s %q",
|
|
rec.Code, requestIDHeader, rec.Header().Get(requestIDHeader))
|
|
|
|
if rec.Header().Get(requestIDHeader) == "" {
|
|
t.Errorf("response has no %s", requestIDHeader)
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(
|
|
t.Context(), http.MethodGet, path, nil)
|
|
req.Header.Set(requestIDHeader, clientRequestID)
|
|
|
|
rec = httptest.NewRecorder()
|
|
s.ServeHTTP(rec, req)
|
|
|
|
got := rec.Header().Get(requestIDHeader)
|
|
if got != clientRequestID {
|
|
t.Errorf("%s = %q, want the request's own %q",
|
|
requestIDHeader, got, clientRequestID)
|
|
}
|
|
})
|
|
}
|
|
}
|