(t *testing.T)
| 425 | } |
| 426 | |
| 427 | func TestWithResponseHook(t *testing.T) { |
| 428 | const hdrKey = "X-Test-Header" |
| 429 | const hdrVal = "hello-world" |
| 430 | |
| 431 | t.Run("single hook", func(t *testing.T) { |
| 432 | var got string |
| 433 | c, err := New( |
| 434 | WithResponseHook(func(resp *http.Response) { |
| 435 | got = resp.Header.Get(hdrKey) |
| 436 | }), |
| 437 | WithBaseMockClient(func(req *http.Request) (*http.Response, error) { |
| 438 | resp := &http.Response{ |
| 439 | StatusCode: http.StatusOK, |
| 440 | Header: make(http.Header), |
| 441 | } |
| 442 | resp.Header.Set(hdrKey, hdrVal) |
| 443 | return resp, nil |
| 444 | }), |
| 445 | ) |
| 446 | assert.NilError(t, err) |
| 447 | |
| 448 | _, err = c.Ping(t.Context(), PingOptions{}) |
| 449 | assert.NilError(t, err) |
| 450 | assert.Check(t, is.Equal(got, hdrVal)) |
| 451 | |
| 452 | assert.NilError(t, c.Close()) |
| 453 | }) |
| 454 | |
| 455 | t.Run("invalid hook", func(t *testing.T) { |
| 456 | _, err := New(WithResponseHook(nil)) |
| 457 | assert.Error(t, err, "invalid response hook: hook is nil") |
| 458 | }) |
| 459 | |
| 460 | t.Run("multiple hooks", func(t *testing.T) { |
| 461 | var triggered []string |
| 462 | |
| 463 | c, err := New( |
| 464 | WithResponseHook(func(*http.Response) { |
| 465 | triggered = append(triggered, "hook 1: "+hdrVal) |
| 466 | }), |
| 467 | WithResponseHook(func(*http.Response) { |
| 468 | triggered = append(triggered, "hook 2: "+hdrVal) |
| 469 | }), |
| 470 | WithBaseMockClient(func(req *http.Request) (*http.Response, error) { |
| 471 | resp := &http.Response{ |
| 472 | StatusCode: http.StatusOK, |
| 473 | Header: make(http.Header), |
| 474 | } |
| 475 | resp.Header.Set(hdrKey, hdrVal) |
| 476 | return resp, nil |
| 477 | }), |
| 478 | ) |
| 479 | assert.NilError(t, err) |
| 480 | |
| 481 | _, err = c.Ping(t.Context(), PingOptions{}) |
| 482 | assert.NilError(t, err) |
| 483 | assert.Check(t, is.DeepEqual(triggered, []string{"hook 1: " + hdrVal, "hook 2: " + hdrVal})) |
| 484 |
nothing calls this directly
no test coverage detected
searching dependent graphs…