── Step executors ─────────────────────────────────────────────────
(t *testing.T, s *step)
| 327 | // ── Step executors ───────────────────────────────────────────────── |
| 328 | |
| 329 | func (r *runner) execRequest(t *testing.T, s *step) { |
| 330 | t.Helper() |
| 331 | path := r.resolve(s.Path) |
| 332 | |
| 333 | var bodyReader io.Reader |
| 334 | if s.Body != nil { |
| 335 | data, _ := json.Marshal(r.resolveValue(s.Body)) |
| 336 | bodyReader = bytes.NewReader(data) |
| 337 | } |
| 338 | req, err := http.NewRequest(s.Method, r.env.baseURL+path, bodyReader) |
| 339 | if err != nil { |
| 340 | t.Fatalf("step %s: create request: %v", s.ID, err) |
| 341 | } |
| 342 | if auth, ok := r.authHeader(s); ok { |
| 343 | req.Header.Set("Authorization", auth) |
| 344 | } |
| 345 | if s.Body != nil { |
| 346 | req.Header.Set("Content-Type", "application/json") |
| 347 | } |
| 348 | |
| 349 | resp, err := http.DefaultClient.Do(req) |
| 350 | if err != nil { |
| 351 | t.Fatalf("step %s: %s %s: %v", s.ID, s.Method, path, err) |
| 352 | } |
| 353 | defer resp.Body.Close() |
| 354 | |
| 355 | rawBody, _ := io.ReadAll(resp.Body) |
| 356 | |
| 357 | if s.Expect == nil { |
| 358 | return |
| 359 | } |
| 360 | |
| 361 | // Status assertion |
| 362 | if s.Expect.Status != 0 && resp.StatusCode != s.Expect.Status { |
| 363 | t.Fatalf("step %s: status = %d, want %d; body: %s", s.ID, resp.StatusCode, s.Expect.Status, rawBody) |
| 364 | } |
| 365 | |
| 366 | // JSON body assertions |
| 367 | if len(s.Expect.BodyContains) == 0 && len(s.Expect.BodyMatch) == 0 && len(s.Expect.BodyExcludes) == 0 { |
| 368 | return |
| 369 | } |
| 370 | |
| 371 | var jsonBody map[string]interface{} |
| 372 | if err := json.Unmarshal(rawBody, &jsonBody); err != nil { |
| 373 | t.Fatalf("step %s: unmarshal response: %v; raw: %s", s.ID, err, rawBody) |
| 374 | } |
| 375 | |
| 376 | for _, key := range s.Expect.BodyContains { |
| 377 | resolvedKey := r.resolve(key) |
| 378 | if _, ok := jsonBody[resolvedKey]; !ok { |
| 379 | t.Fatalf("step %s: expected key %q in response", s.ID, resolvedKey) |
| 380 | } |
| 381 | } |
| 382 | for _, key := range s.Expect.BodyExcludes { |
| 383 | resolvedKey := r.resolve(key) |
| 384 | if _, ok := jsonBody[resolvedKey]; ok { |
| 385 | t.Fatalf("step %s: unexpected key %q in response", s.ID, resolvedKey) |
| 386 | } |
no test coverage detected