|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package http_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "net/http" |
| 22 | + "net/url" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + |
| 28 | + rhttp "sigs.k8s.io/release-utils/http" |
| 29 | + "sigs.k8s.io/release-utils/http/httpfakes" |
| 30 | +) |
| 31 | + |
| 32 | +func TestGetRequest(t *testing.T) { |
| 33 | + for _, tc := range map[string]struct { |
| 34 | + prepare func(*httpfakes.FakeAgentImplementation) |
| 35 | + assert func(*http.Response, error) |
| 36 | + }{ |
| 37 | + "should succeed": { |
| 38 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 39 | + mock.SendGetRequestReturns(&http.Response{StatusCode: http.StatusOK}, nil) |
| 40 | + }, |
| 41 | + assert: func(response *http.Response, err error) { |
| 42 | + require.NoError(t, err) |
| 43 | + assert.Equal(t, http.StatusOK, response.StatusCode) |
| 44 | + }, |
| 45 | + }, |
| 46 | + "should succeed on retry": { |
| 47 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 48 | + mock.SendGetRequestReturnsOnCall(0, &http.Response{StatusCode: http.StatusInternalServerError}, nil) |
| 49 | + mock.SendGetRequestReturnsOnCall(1, &http.Response{StatusCode: http.StatusOK}, nil) |
| 50 | + }, |
| 51 | + assert: func(response *http.Response, err error) { |
| 52 | + require.NoError(t, err) |
| 53 | + assert.Equal(t, http.StatusOK, response.StatusCode) |
| 54 | + }, |
| 55 | + }, |
| 56 | + "should retry on internal server error": { |
| 57 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 58 | + mock.SendGetRequestReturns(&http.Response{StatusCode: http.StatusInternalServerError}, nil) |
| 59 | + }, |
| 60 | + assert: func(response *http.Response, err error) { |
| 61 | + require.Error(t, err) |
| 62 | + assert.NotNil(t, response) |
| 63 | + }, |
| 64 | + }, |
| 65 | + "should retry on too many requests": { |
| 66 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 67 | + mock.SendGetRequestReturns(&http.Response{StatusCode: http.StatusTooManyRequests}, nil) |
| 68 | + }, |
| 69 | + assert: func(response *http.Response, err error) { |
| 70 | + require.Error(t, err) |
| 71 | + assert.NotNil(t, response) |
| 72 | + }, |
| 73 | + }, |
| 74 | + "should retry on URL error": { |
| 75 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 76 | + mock.SendGetRequestReturns(nil, &url.Error{Err: errors.New("test")}) |
| 77 | + }, |
| 78 | + assert: func(response *http.Response, err error) { |
| 79 | + require.Error(t, err) |
| 80 | + require.Contains(t, err.Error(), "test") |
| 81 | + assert.Nil(t, response) |
| 82 | + }, |
| 83 | + }, |
| 84 | + } { |
| 85 | + agent := rhttp.NewAgent().WithWaitTime(0) |
| 86 | + mock := &httpfakes.FakeAgentImplementation{} |
| 87 | + agent.SetImplementation(mock) |
| 88 | + |
| 89 | + if tc.prepare != nil { |
| 90 | + tc.prepare(mock) |
| 91 | + } |
| 92 | + |
| 93 | + //nolint:bodyclose // no need to close for mocked tests |
| 94 | + tc.assert(agent.GetRequest("")) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestPostRequest(t *testing.T) { |
| 99 | + for _, tc := range map[string]struct { |
| 100 | + prepare func(*httpfakes.FakeAgentImplementation) |
| 101 | + assert func(*http.Response, error) |
| 102 | + }{ |
| 103 | + "should succeed": { |
| 104 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 105 | + mock.SendPostRequestReturns(&http.Response{StatusCode: http.StatusOK}, nil) |
| 106 | + }, |
| 107 | + assert: func(response *http.Response, err error) { |
| 108 | + require.NoError(t, err) |
| 109 | + assert.Equal(t, http.StatusOK, response.StatusCode) |
| 110 | + }, |
| 111 | + }, |
| 112 | + "should succeed on retry": { |
| 113 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 114 | + mock.SendPostRequestReturnsOnCall(0, &http.Response{StatusCode: http.StatusInternalServerError}, nil) |
| 115 | + mock.SendPostRequestReturnsOnCall(1, &http.Response{StatusCode: http.StatusOK}, nil) |
| 116 | + }, |
| 117 | + assert: func(response *http.Response, err error) { |
| 118 | + require.NoError(t, err) |
| 119 | + assert.Equal(t, http.StatusOK, response.StatusCode) |
| 120 | + }, |
| 121 | + }, |
| 122 | + "should retry on internal server error": { |
| 123 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 124 | + mock.SendPostRequestReturns(&http.Response{StatusCode: http.StatusInternalServerError}, nil) |
| 125 | + }, |
| 126 | + assert: func(response *http.Response, err error) { |
| 127 | + require.Error(t, err) |
| 128 | + assert.NotNil(t, response) |
| 129 | + }, |
| 130 | + }, |
| 131 | + "should retry on too many requests": { |
| 132 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 133 | + mock.SendPostRequestReturns(&http.Response{StatusCode: http.StatusTooManyRequests}, nil) |
| 134 | + }, |
| 135 | + assert: func(response *http.Response, err error) { |
| 136 | + require.Error(t, err) |
| 137 | + assert.NotNil(t, response) |
| 138 | + }, |
| 139 | + }, |
| 140 | + "should retry on URL error": { |
| 141 | + prepare: func(mock *httpfakes.FakeAgentImplementation) { |
| 142 | + mock.SendPostRequestReturns(nil, &url.Error{Err: errors.New("test")}) |
| 143 | + }, |
| 144 | + assert: func(response *http.Response, err error) { |
| 145 | + require.Error(t, err) |
| 146 | + require.Contains(t, err.Error(), "test") |
| 147 | + assert.Nil(t, response) |
| 148 | + }, |
| 149 | + }, |
| 150 | + } { |
| 151 | + agent := rhttp.NewAgent().WithWaitTime(0) |
| 152 | + mock := &httpfakes.FakeAgentImplementation{} |
| 153 | + agent.SetImplementation(mock) |
| 154 | + |
| 155 | + if tc.prepare != nil { |
| 156 | + tc.prepare(mock) |
| 157 | + } |
| 158 | + |
| 159 | + //nolint:bodyclose // no need to close for mocked tests |
| 160 | + tc.assert(agent.PostRequest("", nil)) |
| 161 | + } |
| 162 | +} |
0 commit comments