Files
go-otel/internal/httpattr/httpattr_test.go
2026-09-19 14:13:43 +02:00

50 lines
1.7 KiB
Go

package httpattr
import "testing"
func lookup(m map[string]string) Lookup {
return func(keys ...string) string {
for _, k := range keys {
if v := m[k]; v != "" {
return v
}
}
return ""
}
}
func TestExtractVariants(t *testing.T) {
cases := []struct {
name string
attrs map[string]string
want string
}{
{"stable semconv", map[string]string{"http.request.method": "get", "url.path": "/api/orders/"}, "GET /api/orders"},
{"legacy nginx target", map[string]string{"http.method": "GET", "http.target": "/api/orders?x=1"}, "GET /api/orders"},
{"go http.url", map[string]string{"http.method": "POST", "http.url": "https://shop.example.com:8443/api/cart?a=b"}, "POST /api/cart"},
{"browser url.full", map[string]string{"http.request.method": "GET", "url.full": "https://shop.example.com/api/orders#x"}, "GET /api/orders"},
{"relative url.full", map[string]string{"http.request.method": "GET", "url.full": "api/orders"}, "GET /api/orders"},
}
for _, c := range cases {
if got := Extract(lookup(c.attrs)).Key(); got != c.want {
t.Errorf("%s: got %q want %q", c.name, got, c.want)
}
}
}
func TestExtractHostAndMountRoute(t *testing.T) {
e := Extract(lookup(map[string]string{"http.route": "/", "url.path": "/x", "server.address": "API.example.com:443"}))
if e.Host != "api.example.com" || e.Route != "" {
t.Fatalf("got %+v", e)
}
}
func TestStripPrefix(t *testing.T) {
p := []string{"/api", "/api/v2"}
for in, want := range map[string]string{"/api/orders": "/orders", "/api/v2/orders": "/orders", "/api": "/", "/apix": "/apix", "/other": "/other"} {
if got := StripPrefix(in, p); got != want {
t.Errorf("%s: got %q want %q", in, got, want)
}
}
}