github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/api/uaa/wrapper/retry_request_test.go (about)

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