github.com/sap/cf-mta-plugin@v2.6.3+incompatible/clients/baseclient/client_util_test.go (about) 1 package baseclient 2 3 import ( 4 "fmt" 5 "time" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 ) 10 11 var _ = Describe("ClientUtil", func() { 12 Describe("ShouldRetry", func() { 13 Context("when zero value pointer error(uninitialized value - nil) is passed as argument", func() { 14 It("Retry operation call shouldn't be made", func() { 15 result := shouldRetry(nil) 16 Expect(result).To(Equal(false)) 17 }) 18 }) 19 20 Context("when the backend returns status code with first digit 1 (1xx)", func() { 21 It("Retry operation call is expected to be made", func() { 22 err := ClientError{102, "Processing", "The request has been accepted but it's not yet complete"} 23 result := shouldRetry(&err) 24 Expect(result).To(Equal(true)) 25 }) 26 }) 27 28 Context("when the backend returns status code with first digit 2 (2xx)", func() { 29 It("Retry operation call shouldn't be made", func() { 30 err := ClientError{200, "OK", "The request has succeeded"} 31 result := shouldRetry(&err) 32 Expect(result).To(Equal(false)) 33 }) 34 }) 35 36 Context("when the backend returns status code with first digit 3 (3xx)", func() { 37 It("Retry operation call is expected to be made", func() { 38 err := ClientError{301, "Moved Permanently", "URI of requested resource has been changed"} 39 result := shouldRetry(&err) 40 Expect(result).To(Equal(true)) 41 }) 42 }) 43 44 Context("when the backend returns status code with first digit 4 (4xx)", func() { 45 It("Retry operation call is expected to be made", func() { 46 err := ClientError{404, "Not Found", "The server cannot find requested resource"} 47 result := shouldRetry(&err) 48 Expect(result).To(Equal(true)) 49 }) 50 }) 51 52 Context("when the backend returns status code with first digit 5 (5xx)", func() { 53 It("Retry operation call is expected to be made", func() { 54 err := ClientError{500, "Internal Server Error", "The server got an invalid response"} 55 result := shouldRetry(&err) 56 Expect(result).To(Equal(true)) 57 }) 58 }) 59 60 Context("when the passed error is not of type 'ClientError'", func() { 61 It("Retry operation call is expected to be made", func() { 62 err := MockError{999, "Not ClientError"} 63 result := shouldRetry(&err) 64 Expect(result).To(Equal(true)) 65 }) 66 }) 67 }) 68 }) 69 70 var _ = Describe("ClientUtil", func() { 71 Describe("CallWithRetry", func() { 72 Context("when retry operation call is Not needed", func() { 73 It("Just retrun required response", func() { 74 getInterface := func() (interface{}, error) { 75 toReturn := testStruct{} 76 return toReturn, nil 77 } 78 result, err := CallWithRetry(getInterface, 4, (time.Duration(0) * time.Second)) 79 Expect(err).NotTo(HaveOccurred()) 80 Expect(result).To(Equal(result)) 81 }) 82 }) 83 Context("when retry operation call IS needed", func() { 84 It("Callback with retry", func() { 85 mockCallback := func() (interface{}, error) { 86 toReturn := testStruct{} 87 err := ClientError{404, "Not Found", "The server cannot find requesred resource"} 88 return toReturn, &err 89 } 90 result, err := CallWithRetry(mockCallback, 4, (time.Duration(0) * time.Second)) 91 Expect(err).To(HaveOccurred()) 92 Expect(result).To(Equal(result)) 93 }) 94 }) 95 }) 96 }) 97 98 type MockError struct { 99 Code int 100 Status string 101 } 102 103 func (ce *MockError) Error() string { 104 return fmt.Sprintf("%s (status %d)", ce.Status, ce.Code) 105 } 106 107 type testStruct struct { 108 }