mirror of
https://github.com/caddyserver/caddy.git
synced 2025-12-08 06:09:53 +00:00
Fix test to use header response placeholders
This commit is contained in:
parent
913e6ffd70
commit
1898a907fe
1 changed files with 20 additions and 11 deletions
|
|
@ -294,9 +294,10 @@ func TestTracing_OpenTelemetry_Span_Attributes(t *testing.T) {
|
||||||
ot := &Tracing{
|
ot := &Tracing{
|
||||||
SpanName: "test-span",
|
SpanName: "test-span",
|
||||||
SpanAttributes: map[string]string{
|
SpanAttributes: map[string]string{
|
||||||
"placeholder": "{http.request.method}",
|
"static": "test-service",
|
||||||
"static": "test-service",
|
"request-placeholder": "{http.request.method}",
|
||||||
"mixed": "prefix-{http.request.method}-suffix",
|
"response-placeholder": "{http.response.header.X-Some-Header}",
|
||||||
|
"mixed": "prefix-{http.request.method}-{http.response.header.X-Some-Header}",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -304,18 +305,26 @@ func TestTracing_OpenTelemetry_Span_Attributes(t *testing.T) {
|
||||||
req, _ := http.NewRequest("POST", "https://api.example.com/v1/users?id=123", nil)
|
req, _ := http.NewRequest("POST", "https://api.example.com/v1/users?id=123", nil)
|
||||||
req.Host = "api.example.com"
|
req.Host = "api.example.com"
|
||||||
|
|
||||||
// Set up the request context with proper replacer and vars
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// Set up the replacer
|
||||||
repl := caddy.NewReplacer()
|
repl := caddy.NewReplacer()
|
||||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||||
ctx = context.WithValue(ctx, caddyhttp.VarsCtxKey, make(map[string]any))
|
ctx = context.WithValue(ctx, caddyhttp.VarsCtxKey, make(map[string]any))
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
|
// Set up request placeholders
|
||||||
repl.Set("http.request.method", req.Method)
|
repl.Set("http.request.method", req.Method)
|
||||||
|
repl.Set("http.request.uri", req.URL.RequestURI())
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
// Handler to generate the response
|
||||||
|
|
||||||
// Handler that ensures the request gets processed
|
|
||||||
var handler caddyhttp.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) error {
|
var handler caddyhttp.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) error {
|
||||||
|
writer.Header().Set("X-Some-Header", "some-value")
|
||||||
writer.WriteHeader(200)
|
writer.WriteHeader(200)
|
||||||
|
|
||||||
|
// Make response headers available to replacer
|
||||||
|
repl.Set("http.response.header.X-Some-Header", writer.Header().Get("X-Some-Header"))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -366,10 +375,10 @@ func TestTracing_OpenTelemetry_Span_Attributes(t *testing.T) {
|
||||||
|
|
||||||
// Verify that the span attributes were set correctly with placeholder replacement
|
// Verify that the span attributes were set correctly with placeholder replacement
|
||||||
expectedAttributes := map[string]string{
|
expectedAttributes := map[string]string{
|
||||||
"placeholder": "POST",
|
"static": "test-service",
|
||||||
"static": "test-service",
|
"request-placeholder": "POST",
|
||||||
"mixed": "prefix-POST-suffix",
|
"response-placeholder": "some-value",
|
||||||
"http.response.status_code": "200", // OTEL default
|
"mixed": "prefix-POST-some-value",
|
||||||
}
|
}
|
||||||
|
|
||||||
actualAttributes := make(map[string]string)
|
actualAttributes := make(map[string]string)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue