github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/router/wrapper/error_wrapper_test.go (about) 1 package wrapper_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/router" 7 "code.cloudfoundry.org/cli/api/router/routererror" 8 "code.cloudfoundry.org/cli/api/router/routerfakes" 9 . "code.cloudfoundry.org/cli/api/router/wrapper" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Error Wrapper", func() { 15 var ( 16 fakeConnection *routerfakes.FakeConnection 17 wrapper router.Connection 18 request *router.Request 19 response *router.Response 20 makeErr error 21 fakeConnectionErr routererror.RawHTTPStatusError 22 ) 23 24 BeforeEach(func() { 25 fakeConnection = new(routerfakes.FakeConnection) 26 wrapper = NewErrorWrapper().Wrap(fakeConnection) 27 request = &router.Request{} 28 response = &router.Response{} 29 fakeConnectionErr = routererror.RawHTTPStatusError{} 30 }) 31 32 JustBeforeEach(func() { 33 makeErr = wrapper.Make(request, response) 34 }) 35 36 Describe("Make", func() { 37 When("the HTTP error code is unexpected", func() { 38 BeforeEach(func() { 39 fakeConnectionErr.StatusCode = http.StatusTeapot 40 fakeConnectionErr.RawResponse = []byte("something else blew up") 41 fakeConnection.MakeReturns(fakeConnectionErr) 42 }) 43 44 It("returns a RawHTTPStatusError", func() { 45 Expect(fakeConnection.MakeCallCount()).To(Equal(1)) 46 requestCall, responseCall := fakeConnection.MakeArgsForCall(0) 47 Expect(requestCall).To(Equal(request)) 48 Expect(responseCall).To(Equal(response)) 49 50 Expect(makeErr).To(MatchError(fakeConnectionErr)) 51 }) 52 }) 53 54 Context("401 Unauthorized", func() { 55 BeforeEach(func() { 56 fakeConnectionErr.StatusCode = http.StatusUnauthorized 57 }) 58 59 When("the token has expired", func() { 60 BeforeEach(func() { 61 fakeConnectionErr.RawResponse = []byte(`{"name":"UnauthorizedError","message":"Token is expired"}`) 62 fakeConnection.MakeReturns(fakeConnectionErr) 63 }) 64 65 It("returns an InvalidAuthTokenError", func() { 66 Expect(fakeConnection.MakeCallCount()).To(Equal(1)) 67 68 Expect(makeErr).To(MatchError(routererror.InvalidAuthTokenError{Message: "Token is expired"})) 69 }) 70 }) 71 72 When("the error is a unauthorized message", func() { 73 BeforeEach(func() { 74 fakeConnectionErr.RawResponse = []byte(`{"name":"UnauthorizedError","message":"You are not authorized to perform the requested action"}`) 75 fakeConnection.MakeReturns(fakeConnectionErr) 76 }) 77 78 It("returns an InvalidAuthTokenError", func() { 79 Expect(fakeConnection.MakeCallCount()).To(Equal(1)) 80 81 Expect(makeErr).To(MatchError(routererror.UnauthorizedError{Message: "You are not authorized to perform the requested action"})) 82 }) 83 }) 84 85 When("the error is a generic 401", func() { 86 BeforeEach(func() { 87 fakeConnectionErr.RawResponse = []byte(`{"name":"UnauthorizedError","message":"no you can't"}`) 88 fakeConnection.MakeReturns(fakeConnectionErr) 89 }) 90 91 It("returns a RawHTTPStatusError", func() { 92 Expect(fakeConnection.MakeCallCount()).To(Equal(1)) 93 94 Expect(makeErr).To(MatchError(routererror.RawHTTPStatusError{ 95 StatusCode: http.StatusUnauthorized, 96 RawResponse: fakeConnectionErr.RawResponse, 97 })) 98 }) 99 }) 100 101 When("the response cannot be parsed", func() { 102 BeforeEach(func() { 103 fakeConnectionErr.RawResponse = []byte(`not valid JSON}`) 104 fakeConnection.MakeReturns(fakeConnectionErr) 105 }) 106 107 It("returns the RawHTTPStatusError", func() { 108 Expect(fakeConnection.MakeCallCount()).To(Equal(1)) 109 110 Expect(makeErr).To(MatchError(routererror.RawHTTPStatusError{ 111 StatusCode: http.StatusUnauthorized, 112 RawResponse: fakeConnectionErr.RawResponse, 113 })) 114 }) 115 }) 116 }) 117 118 Context("404 Not Found", func() { 119 BeforeEach(func() { 120 fakeConnectionErr.RawResponse = []byte(`{"name":"ResourceNotFoundError","message":"Router Group 'not-a-thing' not found"}`) 121 fakeConnectionErr.StatusCode = http.StatusNotFound 122 fakeConnection.MakeReturns(fakeConnectionErr) 123 }) 124 125 It("returns a ResourceNotFoundError", func() { 126 Expect(fakeConnection.MakeCallCount()).To(Equal(1)) 127 128 Expect(makeErr).To(MatchError(routererror.ResourceNotFoundError{ 129 Message: "Router Group 'not-a-thing' not found", 130 })) 131 }) 132 }) 133 }) 134 })