MCPcopy Create free account
hub / github.com/goadesign/goa / TestMiddleware

Function TestMiddleware

http/middleware/trace_test.go:24–101  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22}
23
24func TestMiddleware(t *testing.T) {
25 var (
26 traceID = "testTraceID"
27 spanID = "testSpanID"
28 newTraceID = func() string { return traceID }
29 newID = func() string { return spanID }
30 discard = regexp.MustCompile("^/$")
31 )
32
33 cases := map[string]struct {
34 Rate int
35 TraceID, ParentSpanID string
36 Discard *regexp.Regexp
37 // output
38 CtxTraceID, CtxSpanID, CtxParentID string
39 }{
40 "no-trace": {100, "", "", nil, traceID, spanID, ""},
41 "no-trace-discarded": {100, "", "", discard, "", "", ""},
42 "trace": {100, "trace", "", nil, "trace", spanID, ""},
43 "trace-not-discarded": {100, "trace", "", discard, "trace", spanID, ""},
44 "parent": {100, "trace", "parent", nil, "trace", spanID, "parent"},
45 "parent-not-discarded": {100, "trace", "parent", discard, "trace", spanID, "parent"},
46
47 "zero-rate-no-trace": {0, "", "", nil, "", "", ""},
48 "zero-rate-trace": {0, "trace", "", nil, "trace", spanID, ""},
49 "zero-rate-trace-not-discarded": {0, "trace", "", discard, "trace", spanID, ""},
50 "zero-rate-parent-not-discarded": {0, "trace", "parent", discard, "trace", spanID, "parent"},
51 }
52
53 for k, c := range cases {
54 traceOptions := []middleware.TraceOption{
55 httpm.SamplingPercent(c.Rate),
56 httpm.SpanIDFunc(newID),
57 httpm.TraceIDFunc(newTraceID),
58 }
59 if c.Discard != nil {
60 traceOptions = append(traceOptions, httpm.DiscardFromTrace(c.Discard))
61 }
62 var (
63 m = httpm.Trace(traceOptions...)
64 h = new(testHandler)
65 headers = make(http.Header)
66 )
67 if c.TraceID != "" {
68 headers.Set(httpm.TraceIDHeader, c.TraceID)
69 }
70 if c.ParentSpanID != "" {
71 headers.Set(httpm.ParentSpanIDHeader, c.ParentSpanID)
72 }
73 req, _ := http.NewRequest("GET", "/", nil)
74 req.Header = headers
75
76 m(h).ServeHTTP(httptest.NewRecorder(), req)
77
78 var ctxTraceID, ctxSpanID, ctxParentID string
79 {
80 ctx := h.Context
81 if traceID := ctx.Value(middleware.TraceIDKey); traceID != nil {

Callers

nothing calls this directly

Calls 2

SetMethod · 0.80
ServeHTTPMethod · 0.65

Tested by

no test coverage detected