MCPcopy Index your code
hub / github.com/docker/docker-agent / TestWrapHTTPError

Function TestWrapHTTPError

pkg/modelerrors/modelerrors_test.go:717–774  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

715}
716
717func TestWrapHTTPError(t *testing.T) {
718 t.Parallel()
719
720 t.Run("nil error returns nil", func(t *testing.T) {
721 t.Parallel()
722 require.NoError(t, WrapHTTPError(429, nil, nil))
723 })
724
725 t.Run("status < 400 passes through unchanged", func(t *testing.T) {
726 t.Parallel()
727 origErr := errors.New("original")
728 result := WrapHTTPError(200, nil, origErr)
729 assert.Equal(t, origErr, result)
730 var se *StatusError
731 assert.NotErrorAs(t, result, &se)
732 })
733
734 t.Run("429 without response has zero RetryAfter", func(t *testing.T) {
735 t.Parallel()
736 origErr := errors.New("rate limited")
737 result := WrapHTTPError(429, nil, origErr)
738 var se *StatusError
739 require.ErrorAs(t, result, &se)
740 assert.Equal(t, 429, se.StatusCode)
741 assert.Equal(t, time.Duration(0), se.RetryAfter)
742 assert.Equal(t, "HTTP 429: rate limited", se.Error())
743 })
744
745 t.Run("429 with Retry-After header sets RetryAfter", func(t *testing.T) {
746 t.Parallel()
747 origErr := errors.New("rate limited")
748 respHeader := http.Header{}
749 respHeader.Set("Retry-After", "30")
750 resp := &http.Response{Header: respHeader}
751 result := WrapHTTPError(429, resp, origErr)
752 var se *StatusError
753 require.ErrorAs(t, result, &se)
754 assert.Equal(t, 429, se.StatusCode)
755 assert.Equal(t, 30*time.Second, se.RetryAfter)
756 })
757
758 t.Run("500 wraps correctly", func(t *testing.T) {
759 t.Parallel()
760 origErr := errors.New("internal server error")
761 result := WrapHTTPError(500, nil, origErr)
762 var se *StatusError
763 require.ErrorAs(t, result, &se)
764 assert.Equal(t, 500, se.StatusCode)
765 assert.Equal(t, time.Duration(0), se.RetryAfter)
766 })
767
768 t.Run("original error still accessible via Unwrap", func(t *testing.T) {
769 t.Parallel()
770 sentinel := errors.New("sentinel")
771 result := WrapHTTPError(429, nil, sentinel)
772 assert.ErrorIs(t, result, sentinel)
773 })
774}

Callers

nothing calls this directly

Calls 6

ErrorMethod · 0.95
WrapHTTPErrorFunction · 0.85
DurationMethod · 0.80
RunMethod · 0.65
NewMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected