github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/plugin/wrapper/retry_request_test.go (about) 1 package wrapper_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 9 "code.cloudfoundry.org/cli/api/plugin" 10 "code.cloudfoundry.org/cli/api/plugin/pluginerror" 11 "code.cloudfoundry.org/cli/api/plugin/pluginfakes" 12 . "code.cloudfoundry.org/cli/api/plugin/wrapper" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/ginkgo/extensions/table" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Retry Request", func() { 19 DescribeTable("number of retries", 20 func(requestMethod string, responseStatusCode int, expectedNumberOfRetries int) { 21 request, err := http.NewRequest(requestMethod, "https://foo.bar.com/banana", nil) 22 Expect(err).NotTo(HaveOccurred()) 23 24 rawRequestBody := "banana pants" 25 request.Body = ioutil.NopCloser(strings.NewReader(rawRequestBody)) 26 27 response := &plugin.Response{ 28 HTTPResponse: &http.Response{ 29 StatusCode: responseStatusCode, 30 }, 31 } 32 33 fakeConnection := new(pluginfakes.FakeConnection) 34 expectedErr := pluginerror.RawHTTPStatusError{ 35 Status: fmt.Sprintf("%d", responseStatusCode), 36 } 37 fakeConnection.MakeStub = func(req *http.Request, passedResponse *plugin.Response, proxyReader plugin.ProxyReader) error { 38 defer req.Body.Close() 39 body, readErr := ioutil.ReadAll(request.Body) 40 Expect(readErr).ToNot(HaveOccurred()) 41 Expect(string(body)).To(Equal(rawRequestBody)) 42 return expectedErr 43 } 44 45 wrapper := NewRetryRequest(2).Wrap(fakeConnection) 46 err = wrapper.Make(request, response, nil) 47 Expect(err).To(MatchError(expectedErr)) 48 Expect(fakeConnection.MakeCallCount()).To(Equal(expectedNumberOfRetries)) 49 }, 50 51 Entry("maxRetries for Non-Post (500) Internal Server Error", http.MethodGet, http.StatusInternalServerError, 3), 52 Entry("maxRetries for Non-Post (502) Bad Gateway", http.MethodGet, http.StatusBadGateway, 3), 53 Entry("maxRetries for Non-Post (503) Service Unavailable", http.MethodGet, http.StatusServiceUnavailable, 3), 54 Entry("maxRetries for Non-Post (504) Gateway Timeout", http.MethodGet, http.StatusGatewayTimeout, 3), 55 56 Entry("1 for Post (500) Internal Server Error", http.MethodPost, http.StatusInternalServerError, 1), 57 Entry("1 for Post (502) Bad Gateway", http.MethodPost, http.StatusBadGateway, 1), 58 Entry("1 for Post (503) Service Unavailable", http.MethodPost, http.StatusServiceUnavailable, 1), 59 Entry("1 for Post (504) Gateway Timeout", http.MethodPost, http.StatusGatewayTimeout, 1), 60 61 Entry("1 for Get 4XX Errors", http.MethodGet, http.StatusNotFound, 1), 62 ) 63 64 It("does not retry on success", func() { 65 request, err := http.NewRequest(http.MethodGet, "https://foo.bar.com/banana", nil) 66 Expect(err).NotTo(HaveOccurred()) 67 response := &plugin.Response{ 68 HTTPResponse: &http.Response{ 69 StatusCode: http.StatusOK, 70 }, 71 } 72 73 fakeConnection := new(pluginfakes.FakeConnection) 74 wrapper := NewRetryRequest(2).Wrap(fakeConnection) 75 fakeProxyReader := new(pluginfakes.FakeProxyReader) 76 77 err = wrapper.Make(request, response, fakeProxyReader) 78 Expect(err).ToNot(HaveOccurred()) 79 Expect(fakeConnection.MakeCallCount()).To(Equal(1)) 80 _, _, proxyReader := fakeConnection.MakeArgsForCall(0) 81 Expect(proxyReader).To(Equal(fakeProxyReader)) 82 }) 83 })