-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflashduty_test.go
More file actions
593 lines (559 loc) · 20 KB
/
Copy pathflashduty_test.go
File metadata and controls
593 lines (559 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package flashduty
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// noopLogger keeps test output clean.
type noopLogger struct{}
func (noopLogger) Debug(string, ...any) {}
func (noopLogger) Info(string, ...any) {}
func (noopLogger) Warn(string, ...any) {}
func (noopLogger) Error(string, ...any) {}
func newTestClient(t *testing.T, handler http.HandlerFunc) *Client {
t.Helper()
srv := httptest.NewServer(handler)
t.Cleanup(srv.Close)
c, err := NewClient("KEY", WithBaseURL(srv.URL), WithLogger(noopLogger{}))
if err != nil {
t.Fatal(err)
}
return c
}
func TestNewRequestBuildsPostWithAppKeyAndJSON(t *testing.T) {
c, _ := NewClient("KEY", WithBaseURL("https://api.flashcat.cloud"), WithLogger(noopLogger{}))
req, err := c.newRequest(context.Background(), http.MethodPost, "/incident/list", map[string]any{"p": 1})
if err != nil {
t.Fatal(err)
}
if req.Method != http.MethodPost {
t.Fatalf("method = %s", req.Method)
}
if got := req.URL.Query().Get("app_key"); got != "KEY" {
t.Fatalf("app_key = %q", got)
}
if req.URL.Path != "/incident/list" {
t.Fatalf("path = %s", req.URL.Path)
}
if ct := req.Header.Get("Content-Type"); ct != "application/json" {
t.Fatalf("content-type = %s", ct)
}
body, _ := io.ReadAll(req.Body)
if !strings.Contains(string(body), `"p":1`) {
t.Fatalf("body = %s", body)
}
}
// TestOptionalObjectRequestFieldOmitsWhenUnset guards against a codegen
// regression: encoding/json's `,omitempty` never drops a bare struct value
// (only false/0/""/nil-slice/nil-map/nil-pointer count as "empty"), so an
// unset optional object request field used to always be sent as `{}`. For
// CreateSilenceRuleRequest.TimeFilter this made a recurring-only silence rule
// (TimeFilters set, TimeFilter unset) impossible: the server's binding
// validates StartTime/EndTime with `gt=0` whenever "time_filter" is present
// in the payload at all. The generator now emits `,omitzero` for optional
// struct-typed request fields, which correctly drops the zero value.
func TestOptionalObjectRequestFieldOmitsWhenUnset(t *testing.T) {
c, _ := NewClient("KEY", WithBaseURL("https://api.flashcat.cloud"), WithLogger(noopLogger{}))
req, err := c.newRequest(context.Background(), http.MethodPost, "/silence-rule/create", &CreateSilenceRuleRequest{
RuleName: "recurring only",
TimeFilters: []CreateSilenceRuleRequestTimeFiltersItem{
{Start: "09:00", End: "18:00"},
},
})
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(req.Body)
if strings.Contains(string(body), `"time_filter"`) {
t.Fatalf("unset TimeFilter must be omitted from the wire, got body = %s", body)
}
if !strings.Contains(string(body), `"time_filters"`) {
t.Fatalf("TimeFilters must be present, got body = %s", body)
}
req, err = c.newRequest(context.Background(), http.MethodPost, "/silence-rule/create", &CreateSilenceRuleRequest{
RuleName: "one-off only",
TimeFilter: CreateSilenceRuleRequestTimeFilter{StartTime: 1000, EndTime: 2000},
})
if err != nil {
t.Fatal(err)
}
body, _ = io.ReadAll(req.Body)
if !strings.Contains(string(body), `"time_filter"`) {
t.Fatalf("set TimeFilter must be present on the wire, got body = %s", body)
}
}
// TestResetPostMortemContentSendsZeroExpectedRevision guards a codegen
// contract: expected_revision is a required field where 0 is a valid value
// (first write to a never-saved document, per the spec's minimum: 0), so it
// must reach the wire even when zero. The spec models it as
// type: ["integer", "null"], which the generator rewrites to a pointer — a
// nil pointer means "unset" and is omitted, while Int64(0) is sent.
func TestResetPostMortemContentSendsZeroExpectedRevision(t *testing.T) {
c, _ := NewClient("KEY", WithBaseURL("https://api.flashcat.cloud"), WithLogger(noopLogger{}))
req, err := c.newRequest(context.Background(), http.MethodPost, "/incident/post-mortem/content/reset", &ResetPostMortemContentRequest{
PostMortemID: "pm_x",
Markdown: "## impact",
ExpectedRevision: Int64(0),
IdempotencyKey: "key-1",
})
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(req.Body)
if !strings.Contains(string(body), `"expected_revision":0`) {
t.Fatalf("ExpectedRevision=Int64(0) must be sent on the wire, got body = %s", body)
}
req, err = c.newRequest(context.Background(), http.MethodPost, "/incident/post-mortem/content/reset", &ResetPostMortemContentRequest{
PostMortemID: "pm_x",
Markdown: "## impact",
IdempotencyKey: "key-1",
})
if err != nil {
t.Fatal(err)
}
body, _ = io.ReadAll(req.Body)
if strings.Contains(string(body), `"expected_revision"`) {
t.Fatalf("nil ExpectedRevision must be omitted from the wire, got body = %s", body)
}
}
func TestIncidentNotificationOverridePreservesExplicitFalse(t *testing.T) {
c, _ := NewClient("KEY", WithBaseURL("https://api.flashcat.cloud"), WithLogger(noopLogger{}))
tests := []struct {
name string
path string
body any
notifyPath []string
}{
{
name: "create incident",
path: "/incident/create",
body: &CreateIncidentRequest{
IncidentSeverity: "Critical",
AssignedTo: CreateIncidentRequestAssignedTo{
PersonIDs: []int64{1},
Notify: CreateIncidentRequestAssignedToNotify{
FollowPreference: Bool(false),
PersonalChannels: []string{"sms"},
},
},
},
notifyPath: []string{"assigned_to", "notify"},
},
{
name: "add responder",
path: "/incident/responder/add",
body: &AddIncidentResponderRequest{
IncidentID: "0123456789abcdef01234567",
PersonIDs: []int64{1},
Notify: AddIncidentResponderRequestNotify{
FollowPreference: Bool(false),
PersonalChannels: []string{"sms"},
},
},
notifyPath: []string{"notify"},
},
{
name: "assign incident",
path: "/incident/assign",
body: &AssignIncidentRequest{
IncidentID: "0123456789abcdef01234567",
AssignedTo: AssignedTo{
PersonIDs: []int64{1},
Notify: AssignedToNotify{
FollowPreference: Bool(false),
PersonalChannels: []string{"sms"},
},
},
},
notifyPath: []string{"assigned_to", "notify"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := c.newRequest(context.Background(), http.MethodPost, tt.path, tt.body)
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
var payload map[string]any
if err := json.Unmarshal(body, &payload); err != nil {
t.Fatal(err)
}
notify := payload
for _, key := range tt.notifyPath {
value, ok := notify[key].(map[string]any)
if !ok {
t.Fatalf("%s is missing from request body: %s", key, body)
}
notify = value
}
follow, ok := notify["follow_preference"]
if !ok || follow != false {
t.Fatalf("follow_preference = %#v, present = %t, body = %s", follow, ok, body)
}
})
}
}
func TestIncidentNotificationOverrideOmitsUnsetPreference(t *testing.T) {
c, _ := NewClient("KEY", WithBaseURL("https://api.flashcat.cloud"), WithLogger(noopLogger{}))
req, err := c.newRequest(context.Background(), http.MethodPost, "/incident/create", &CreateIncidentRequest{
IncidentSeverity: "Critical",
AssignedTo: CreateIncidentRequestAssignedTo{
PersonIDs: []int64{1},
Notify: CreateIncidentRequestAssignedToNotify{
PersonalChannels: []string{"sms"},
},
},
})
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(body), `"follow_preference"`) {
t.Fatalf("nil FollowPreference must be omitted from the wire, got body = %s", body)
}
}
func TestNewRequestAppliesHookAndHeaders(t *testing.T) {
c, _ := NewClient("KEY",
WithRequestHeaders(map[string][]string{"X-Static": {"s"}}),
WithRequestHook(func(r *http.Request) { r.Header.Set("X-Hook", "h") }),
WithLogger(noopLogger{}),
)
req, _ := c.newRequest(context.Background(), http.MethodPost, "/x", nil)
if req.Header.Get("X-Static") != "s" || req.Header.Get("X-Hook") != "h" {
t.Fatalf("headers not applied: %+v", req.Header)
}
}
func TestDoDecodesDataAndPagination(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Flashcat-Request-Id", "RID1")
_, _ = io.WriteString(w, `{"request_id":"RID1","data":{"total":2,"has_next_page":true,"search_after_ctx":"cur","items":[{"incident_id":"i1"}]}}`)
})
var out struct {
Items []struct {
IncidentID string `json:"incident_id"`
} `json:"items"`
}
resp, err := c.do(context.Background(), "/incident/list", map[string]any{}, &out)
if err != nil {
t.Fatal(err)
}
if resp.RequestID != "RID1" || resp.Total != 2 || !resp.HasNextPage || resp.SearchAfterCtx != "cur" {
t.Fatalf("response meta = %+v", resp)
}
if len(out.Items) != 1 || out.Items[0].IncidentID != "i1" {
t.Fatalf("data not decoded: %+v", out)
}
}
func TestDoMapsEnvelopeError(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"request_id":"RID2","error":{"code":"incident_not_found","message":"nope"}}`)
})
_, err := c.do(context.Background(), "/incident/info", map[string]any{}, nil)
var apiErr *ErrorResponse
if !errors.As(err, &apiErr) || apiErr.Code != "incident_not_found" || apiErr.RequestID != "RID2" {
t.Fatalf("expected mapped ErrorResponse, got %v", err)
}
}
func TestDoMapsNon2xx(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
_, _ = io.WriteString(w, `{"error":{"code":"internal","message":"boom"}}`)
})
_, err := c.do(context.Background(), "/x", map[string]any{}, nil)
var apiErr *ErrorResponse
if !errors.As(err, &apiErr) || apiErr.Response.StatusCode != 500 {
t.Fatalf("expected 500 ErrorResponse, got %v", err)
}
}
func TestDoEmptyBodySuccess(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"request_id":"RID3"}`)
})
resp, err := c.do(context.Background(), "/incident/ack", map[string]any{}, nil)
if err != nil || resp.RequestID != "RID3" {
t.Fatalf("empty-data success failed: err=%v resp=%+v", err, resp)
}
}
func TestDoTreatsOKCodeAsSuccess(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
// Some success envelopes carry error.code "OK" rather than a null error.
_, _ = io.WriteString(w, `{"request_id":"RID","error":{"code":"OK","message":""},"data":{"items":[{"incident_id":"i1"}]}}`)
})
var out struct {
Items []struct {
IncidentID string `json:"incident_id"`
} `json:"items"`
}
resp, err := c.do(context.Background(), "/incident/list", map[string]any{}, &out)
if err != nil {
t.Fatalf("OK code must be treated as success, got %v", err)
}
if len(out.Items) != 1 || out.Items[0].IncidentID != "i1" || resp.RequestID != "RID" {
t.Fatalf("data not decoded on OK envelope: out=%+v resp=%+v", out, resp)
}
}
// TestDoReportsIntermediaryOnNon2xxNonJSONBody verifies that a non-JSON
// gateway timeout returns actionable diagnostics without echoing the
// untrusted response body.
func TestDoReportsIntermediaryOnNon2xxNonJSONBody(t *testing.T) {
marker := strings.Repeat("sensitive-", 2)
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusGatewayTimeout)
_, _ = io.WriteString(w, `<html><body>request /incident/list?app_key=`+marker+` timed out</body></html>`)
})
_, err := c.do(context.Background(), "/incident/list", map[string]any{}, nil)
if err == nil {
t.Fatal("expected an error")
}
msg := err.Error()
for _, want := range []string{"HTTP 504", "intermediary", "no request id", "gateway/proxy timeout"} {
if !strings.Contains(msg, want) {
t.Fatalf("error message = %q, want it to contain %q", msg, want)
}
}
if strings.Contains(msg, marker) || strings.Contains(msg, "incident/list") {
t.Fatalf("error message exposed the untrusted response body: %q", msg)
}
if strings.Contains(msg, "malformed response") {
t.Fatalf("error message = %q, must not use the old malformed-response wording", msg)
}
}
// TestDoReportsIntermediaryWithRequestID covers the case where the
// intermediary (or an upstream hop before it) did forward a request id.
func TestDoReportsIntermediaryWithRequestID(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Flashcat-Request-Id", "RID-504")
w.WriteHeader(http.StatusGatewayTimeout)
_, _ = io.WriteString(w, "<html>timeout</html>")
})
_, err := c.do(context.Background(), "/incident/list", map[string]any{}, nil)
if err == nil || !strings.Contains(err.Error(), "request id RID-504") {
t.Fatalf("expected error to include the forwarded request id, got %v", err)
}
}
// TestDoMapsNon2xxJSONEnvelopeUnaffectedByGatewaySplit confirms a real 504
// bearing a normal JSON error envelope is completely unaffected by the new
// non-JSON/intermediary branch: it still maps to *ErrorResponse as before.
func TestDoMapsNon2xxJSONEnvelopeUnaffectedByGatewaySplit(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Flashcat-Request-Id", "RID4")
w.WriteHeader(http.StatusGatewayTimeout)
_, _ = io.WriteString(w, `{"request_id":"RID4","error":{"code":"timeout","message":"upstream took too long"}}`)
})
_, err := c.do(context.Background(), "/incident/list", map[string]any{}, nil)
var apiErr *ErrorResponse
if !errors.As(err, &apiErr) || apiErr.Code != "timeout" || apiErr.Response.StatusCode != http.StatusGatewayTimeout || apiErr.RequestID != "RID4" {
t.Fatalf("expected mapped ErrorResponse for JSON envelope, got %v", err)
}
}
// TestDoExposesNonJSONBodyAsRaw also guards the 2xx branch against the
// non-2xx/intermediary-error split above: a non-JSON body on success must
// keep returning Response.Raw with no error, never the new gateway message.
func TestDoExposesNonJSONBodyAsRaw(t *testing.T) {
const csv = "id,title\n1,boom\n2,bam\n"
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
_, _ = io.WriteString(w, csv)
})
resp, err := c.do(context.Background(), "/insight/incident/export", map[string]any{}, nil)
if err != nil {
t.Fatalf("non-JSON success body must not error, got %v", err)
}
if string(resp.Raw) != csv {
t.Fatalf("Response.Raw = %q, want the CSV body", resp.Raw)
}
}
func TestDoReturnsRateLimitErrorOn429(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", "30")
w.Header().Set("X-RateLimit-Remaining", "0")
w.WriteHeader(429)
_, _ = io.WriteString(w, `{"error":{"code":"rate_limited","message":"slow down"}}`)
})
resp, err := c.do(context.Background(), "/incident/list", map[string]any{}, nil)
var rl *RateLimitError
if !errors.As(err, &rl) {
t.Fatalf("expected *RateLimitError, got %T: %v", err, err)
}
if rl.RetryAfter != 30*time.Second {
t.Fatalf("RetryAfter = %s, want 30s", rl.RetryAfter)
}
var generic *ErrorResponse
if !errors.As(err, &generic) || generic.Code != "rate_limited" {
t.Fatalf("RateLimitError must unwrap to *ErrorResponse")
}
if resp.RateLimit.RetryAfter != 30*time.Second || resp.RateLimit.Remaining != 0 {
t.Fatalf("Response.RateLimit not populated: %+v", resp.RateLimit)
}
}
// lookup walks a decoded JSON body along path and reports the value at the end
// of it, plus whether every segment existed.
func lookup(payload map[string]any, path ...string) (any, bool) {
var cur any = payload
for _, key := range path {
obj, ok := cur.(map[string]any)
if !ok {
return nil, false
}
cur, ok = obj[key]
if !ok {
return nil, false
}
}
return cur, true
}
// Fields whose zero value carries meaning are generated as pointers so the
// caller can send it. Sending the zero value must put the key on the wire.
func TestNullableRequestFieldsSendExplicitZero(t *testing.T) {
c, _ := NewClient("KEY", WithBaseURL("https://api.flashcat.cloud"), WithLogger(noopLogger{}))
tests := []struct {
name string
path string
body any
at []string
want any
}{
{
name: "rum application update clears is_private",
path: "/rum/application/update",
body: &RUMApplicationUpdateRequest{ApplicationID: "app-1", IsPrivate: Bool(false)},
at: []string{"is_private"},
want: false,
},
{
name: "rum application update re-enables geo inference",
path: "/rum/application/update",
body: &RUMApplicationUpdateRequest{ApplicationID: "app-1", NoGeo: Bool(false)},
at: []string{"no_geo"},
want: false,
},
{
name: "rum application update re-enables ip collection",
path: "/rum/application/update",
body: &RUMApplicationUpdateRequest{ApplicationID: "app-1", NoIP: Bool(false)},
at: []string{"no_ip"},
want: false,
},
{
// A minimal alerting override is entirely zero-valued apart from
// Enabled; the pointer keeps the omitzero container on the wire.
name: "rum application update disables alerting",
path: "/rum/application/update",
body: &RUMApplicationUpdateRequest{ApplicationID: "app-1", Alerting: RUMApplicationAlerting{Enabled: Bool(false)}},
at: []string{"alerting", "enabled"},
want: false,
},
{
name: "rum field list selects non-facet fields",
path: "/rum/field/list",
body: &RUMFieldListRequest{IsFacet: Bool(false)},
at: []string{"is_facet"},
want: false,
},
{
name: "schedule notifies exactly at shift start",
path: "/schedule/create",
body: &ScheduleUpsertRequest{Notify: ScheduleNotify{AdvanceInTime: Int64(0)}},
at: []string{"notify", "advance_in_time"},
want: float64(0),
},
{
name: "template update turns the feishu card table off",
path: "/template/update",
body: &TemplateUpdateRequest{TemplateID: "t-1", TemplateName: "t", FeishuAppCardV2TableEnabled: Bool(false)},
at: []string{"feishu_app_card_v2_table_enabled"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := c.newRequest(context.Background(), http.MethodPost, tt.path, tt.body)
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
var payload map[string]any
if err := json.Unmarshal(body, &payload); err != nil {
t.Fatal(err)
}
got, ok := lookup(payload, tt.at...)
if !ok {
t.Fatalf("%s missing from request body: %s", strings.Join(tt.at, "."), body)
}
if got != tt.want {
t.Fatalf("%s = %#v, want %#v, body = %s", strings.Join(tt.at, "."), got, tt.want, body)
}
})
}
}
// A nil pointer must stay off the wire so the server leaves the field alone.
func TestNullableRequestFieldsOmitUnsetValues(t *testing.T) {
c, _ := NewClient("KEY", WithBaseURL("https://api.flashcat.cloud"), WithLogger(noopLogger{}))
tests := []struct {
name string
path string
body any
absent []string
present []string
}{
{
name: "rum application update leaves privacy toggles alone",
path: "/rum/application/update",
body: &RUMApplicationUpdateRequest{ApplicationID: "app-1", ApplicationName: "renamed"},
absent: []string{`"is_private"`, `"no_geo"`, `"no_ip"`, `"alerting"`},
present: []string{`"application_name":"renamed"`},
},
{
name: "rum field list returns every field",
path: "/rum/field/list",
body: &RUMFieldListRequest{Scopes: []string{"session"}},
absent: []string{`"is_facet"`},
present: []string{`"scopes"`},
},
{
name: "template update leaves the feishu card table alone",
path: "/template/update",
body: &TemplateUpdateRequest{TemplateID: "t-1", TemplateName: "t"},
absent: []string{`"feishu_app_card_v2_table_enabled"`},
present: []string{`"template_id":"t-1"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := c.newRequest(context.Background(), http.MethodPost, tt.path, tt.body)
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
for _, key := range tt.absent {
if strings.Contains(string(body), key) {
t.Fatalf("unset %s must be omitted from the wire, got body = %s", key, body)
}
}
for _, key := range tt.present {
if !strings.Contains(string(body), key) {
t.Fatalf("%s missing from request body: %s", key, body)
}
}
})
}
}