|
| 1 | +package ldmiddleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + ld "github.com/launchdarkly/go-server-sdk/v7" |
| 11 | + "github.com/launchdarkly/go-server-sdk/v7/ldcomponents" |
| 12 | + "github.com/launchdarkly/go-server-sdk/v7/ldhooks" |
| 13 | +) |
| 14 | + |
| 15 | +type recordingHook struct { |
| 16 | + ldhooks.Unimplemented |
| 17 | + events []ldhooks.TrackSeriesContext |
| 18 | +} |
| 19 | + |
| 20 | +func (h *recordingHook) Metadata() ldhooks.Metadata { return ldhooks.NewMetadata("rec") } |
| 21 | +func (h *recordingHook) AfterTrack(_ context.Context, sc ldhooks.TrackSeriesContext) error { |
| 22 | + h.events = append(h.events, sc) |
| 23 | + return nil |
| 24 | +} |
| 25 | + |
| 26 | +func makeClientWithHook(t *testing.T, hook ldhooks.Hook) *ld.LDClient { |
| 27 | + t.Helper() |
| 28 | + config := ld.Config{ |
| 29 | + DataSource: ldcomponents.ExternalUpdatesOnly(), |
| 30 | + Events: ldcomponents.SendEvents().FlushInterval(time.Hour), |
| 31 | + DiagnosticOptOut: true, |
| 32 | + Hooks: []ldhooks.Hook{hook}, |
| 33 | + } |
| 34 | + client, err := ld.MakeCustomClient("sdk-key", config, time.Second) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("failed to make client: %v", err) |
| 37 | + } |
| 38 | + t.Cleanup(func() { _ = client.Close() }) |
| 39 | + return client |
| 40 | +} |
| 41 | + |
| 42 | +func TestAddScopedClientForRequest_SetsScopedClientAndContext(t *testing.T) { |
| 43 | + client := makeClientWithHook(t, &recordingHook{}) |
| 44 | + handler := AddScopedClientForRequest(client)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 | + sc, ok := ld.GetScopedClient(r.Context()) |
| 46 | + if !ok { |
| 47 | + t.Fatalf("scoped client not found in request context") |
| 48 | + } |
| 49 | + ctx := sc.CurrentContext() |
| 50 | + if string(ctx.Kind()) != "request" { |
| 51 | + t.Fatalf("unexpected kind: %s", ctx.Kind()) |
| 52 | + } |
| 53 | + if ctx.Key() == "" { |
| 54 | + t.Fatalf("expected non-empty context key") |
| 55 | + } |
| 56 | + if ctx.GetValue("method").StringValue() != r.Method { |
| 57 | + t.Fatalf("method attribute mismatch") |
| 58 | + } |
| 59 | + if ctx.GetValue("path").StringValue() != r.URL.Path { |
| 60 | + t.Fatalf("path attribute mismatch") |
| 61 | + } |
| 62 | + w.WriteHeader(204) |
| 63 | + })) |
| 64 | + req := httptest.NewRequest("GET", "http://test/path?q=1", nil) |
| 65 | + rr := httptest.NewRecorder() |
| 66 | + handler.ServeHTTP(rr, req) |
| 67 | + if rr.Code != 204 { |
| 68 | + t.Fatalf("unexpected status: %d", rr.Code) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestTrackTiming_AfterTrackReceivesDurationMetric(t *testing.T) { |
| 73 | + rec := &recordingHook{} |
| 74 | + client := makeClientWithHook(t, rec) |
| 75 | + |
| 76 | + leaf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 77 | + time.Sleep(5 * time.Millisecond) |
| 78 | + w.WriteHeader(204) |
| 79 | + }) |
| 80 | + handler := AddScopedClientForRequest(client)(TrackTiming(leaf)) |
| 81 | + |
| 82 | + req := httptest.NewRequest("GET", "http://test/path", nil) |
| 83 | + rr := httptest.NewRecorder() |
| 84 | + handler.ServeHTTP(rr, req) |
| 85 | + if rr.Code != 204 { |
| 86 | + t.Fatalf("unexpected status: %d", rr.Code) |
| 87 | + } |
| 88 | + if len(rec.events) != 1 { |
| 89 | + t.Fatalf("expected 1 track event, got %d", len(rec.events)) |
| 90 | + } |
| 91 | + e := rec.events[0] |
| 92 | + if e.Key() != "http.request.duration_ms" { |
| 93 | + t.Fatalf("unexpected key: %s", e.Key()) |
| 94 | + } |
| 95 | + if e.MetricValue() == nil || *e.MetricValue() <= 0 { |
| 96 | + t.Fatalf("expected positive metric value") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestTrackErrorResponses_AfterTrackReceivesErrorKeys(t *testing.T) { |
| 101 | + rec := &recordingHook{} |
| 102 | + client := makeClientWithHook(t, rec) |
| 103 | + |
| 104 | + for _, status := range []int{404, 503} { |
| 105 | + rec.events = nil |
| 106 | + leaf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(status) }) |
| 107 | + handler := AddScopedClientForRequest(client)(TrackErrorResponses(leaf)) |
| 108 | + |
| 109 | + req := httptest.NewRequest("GET", "http://test/path", nil) |
| 110 | + rr := httptest.NewRecorder() |
| 111 | + handler.ServeHTTP(rr, req) |
| 112 | + if rr.Code != status { |
| 113 | + t.Fatalf("unexpected status: %d", rr.Code) |
| 114 | + } |
| 115 | + if len(rec.events) != 1 { |
| 116 | + t.Fatalf("expected 1 track event, got %d", len(rec.events)) |
| 117 | + } |
| 118 | + want := "http.response.5xx" |
| 119 | + if status < 500 { |
| 120 | + want = "http.response.4xx" |
| 121 | + } |
| 122 | + if rec.events[0].Key() != want { |
| 123 | + t.Fatalf("unexpected key: %s", rec.events[0].Key()) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments