MCPcopy
hub / github.com/moby/moby / TestWithResponseHook

Function TestWithResponseHook

client/client_options_test.go:427–487  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

425}
426
427func 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

Callers

nothing calls this directly

Calls 12

WithResponseHookFunction · 0.85
WithBaseMockClientFunction · 0.85
CheckMethod · 0.80
EqualMethod · 0.80
NewFunction · 0.70
RunMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
PingMethod · 0.65
ContextMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…