mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 12:16:16 +08:00
133ed18374
* Create request_id directive #1590 * Address Comments * Fix TestListenerAddrEqual * requestid: Add some tests * Address Comments by tobya * Address Comments
34 lines
652 B
Go
34 lines
652 B
Go
package requestid
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
)
|
|
|
|
func TestRequestID(t *testing.T) {
|
|
request, err := http.NewRequest("GET", "http://localhost/", nil)
|
|
if err != nil {
|
|
t.Fatal("Could not create HTTP request:", err)
|
|
}
|
|
|
|
reqid := UUID()
|
|
|
|
c := context.WithValue(request.Context(), httpserver.RequestIDCtxKey, reqid)
|
|
|
|
request = request.WithContext(c)
|
|
|
|
// See caddyhttp/replacer.go
|
|
value, _ := request.Context().Value(httpserver.RequestIDCtxKey).(string)
|
|
|
|
if value == "" {
|
|
t.Fatal("Request ID should not be empty")
|
|
}
|
|
|
|
if value != reqid {
|
|
t.Fatal("Request ID does not match")
|
|
}
|
|
}
|