snapshot of https://github.com/go-chi/chi.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
Test / test (1.24.x, ubuntu-latest) (push) Successful in 2m56s
Test / test (1.25.x, ubuntu-latest) (push) Successful in 2m15s
Test / test (1.26.x, ubuntu-latest) (push) Successful in 2m23s
Test / test (1.27.x, ubuntu-latest) (push) Failing after 3m56s
Test / test (1.24.x, windows-latest) (push) Has been cancelled
Test / test (1.25.x, windows-latest) (push) Has been cancelled
Test / test (1.26.x, windows-latest) (push) Has been cancelled
Test / test (1.27.x, windows-latest) (push) Has been cancelled
Some checks failed
Test / test (1.24.x, ubuntu-latest) (push) Successful in 2m56s
Test / test (1.25.x, ubuntu-latest) (push) Successful in 2m15s
Test / test (1.26.x, ubuntu-latest) (push) Successful in 2m23s
Test / test (1.27.x, ubuntu-latest) (push) Failing after 3m56s
Test / test (1.24.x, windows-latest) (push) Has been cancelled
Test / test (1.25.x, windows-latest) (push) Has been cancelled
Test / test (1.26.x, windows-latest) (push) Has been cancelled
Test / test (1.27.x, windows-latest) (push) Has been cancelled
This commit is contained in:
71
middleware/request_id_test.go
Normal file
71
middleware/request_id_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func maintainDefaultRequestID() func() {
|
||||
original := RequestIDHeader
|
||||
|
||||
return func() {
|
||||
RequestIDHeader = original
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestID(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
requestIDHeader string
|
||||
request func() *http.Request
|
||||
expectedResponse string
|
||||
}{
|
||||
"Retrieves Request Id from default header": {
|
||||
"X-Request-Id",
|
||||
func() *http.Request {
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
req.Header.Add("X-Request-Id", "req-123456")
|
||||
|
||||
return req
|
||||
},
|
||||
"RequestID: req-123456",
|
||||
},
|
||||
"Retrieves Request Id from custom header": {
|
||||
"X-Trace-Id",
|
||||
func() *http.Request {
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
req.Header.Add("X-Trace-Id", "trace:abc123")
|
||||
|
||||
return req
|
||||
},
|
||||
"RequestID: trace:abc123",
|
||||
},
|
||||
}
|
||||
|
||||
defer maintainDefaultRequestID()()
|
||||
|
||||
for _, test := range tests {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
||||
RequestIDHeader = test.requestIDHeader
|
||||
|
||||
r.Use(RequestID)
|
||||
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := GetReqID(r.Context())
|
||||
response := fmt.Sprintf("RequestID: %s", requestID)
|
||||
|
||||
w.Write([]byte(response))
|
||||
})
|
||||
r.ServeHTTP(w, test.request())
|
||||
|
||||
if w.Body.String() != test.expectedResponse {
|
||||
t.Fatalf("RequestID was not the expected value")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user