github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v3/shared/new_clients_test.go (about) 1 package shared_test 2 3 import ( 4 "net/http" 5 "runtime" 6 "time" 7 8 "github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror" 9 "github.com/liamawhite/cli-with-i18n/command/commandfakes" 10 "github.com/liamawhite/cli-with-i18n/command/translatableerror" 11 . "github.com/liamawhite/cli-with-i18n/command/v3/shared" 12 "github.com/liamawhite/cli-with-i18n/util/ui" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 . "github.com/onsi/gomega/ghttp" 18 ) 19 20 var _ = Describe("New Clients", func() { 21 var ( 22 binaryName string 23 fakeConfig *commandfakes.FakeConfig 24 testUI *ui.UI 25 ) 26 27 BeforeEach(func() { 28 binaryName = "faceman" 29 fakeConfig = new(commandfakes.FakeConfig) 30 fakeConfig.BinaryNameReturns(binaryName) 31 32 testUI = ui.NewTestUI(NewBuffer(), NewBuffer(), NewBuffer()) 33 }) 34 35 Context("when the api endpoint is not set", func() { 36 It("returns the NoAPISetError", func() { 37 _, _, err := NewClients(fakeConfig, testUI, true) 38 Expect(err).To(MatchError(translatableerror.NoAPISetError{ 39 BinaryName: binaryName, 40 })) 41 }) 42 }) 43 44 Context("when hitting the target returns an error", func() { 45 var server *Server 46 BeforeEach(func() { 47 server = NewTLSServer() 48 49 fakeConfig.TargetReturns(server.URL()) 50 fakeConfig.SkipSSLValidationReturns(true) 51 }) 52 53 AfterEach(func() { 54 server.Close() 55 }) 56 57 Context("when the error is a cloud controller request error", func() { 58 BeforeEach(func() { 59 fakeConfig.TargetReturns("https://127.0.0.1:9876") 60 }) 61 62 It("returns a command api request error", func() { 63 _, _, err := NewClients(fakeConfig, testUI, true) 64 Expect(err).To(MatchError(ContainSubstring("Request error:"))) 65 }) 66 67 }) 68 69 Context("when the error is a cloud controller api not found error", func() { 70 BeforeEach(func() { 71 server.AppendHandlers( 72 CombineHandlers( 73 VerifyRequest(http.MethodGet, "/"), 74 RespondWith(http.StatusNotFound, "something which is not json"), 75 ), 76 ) 77 }) 78 79 It("returns a command api not found error", func() { 80 _, _, err := NewClients(fakeConfig, testUI, true) 81 Expect(err).To(MatchError(translatableerror.APINotFoundError{URL: server.URL()})) 82 }) 83 }) 84 85 Context("when the error is a V3UnexpectedResponseError and the status code is 404", func() { 86 BeforeEach(func() { 87 server.AppendHandlers( 88 CombineHandlers( 89 VerifyRequest(http.MethodGet, "/"), 90 RespondWith(http.StatusNotFound, "{}"), 91 ), 92 ) 93 }) 94 95 It("returns a V3APIDoesNotExistError", func() { 96 _, _, err := NewClients(fakeConfig, testUI, true) 97 expectedErr := ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusNotFound} 98 Expect(err).To(MatchError(expectedErr)) 99 }) 100 }) 101 102 Context("when the error is generic and the body is valid json", func() { 103 BeforeEach(func() { 104 server.AppendHandlers( 105 CombineHandlers( 106 VerifyRequest(http.MethodGet, "/"), 107 RespondWith(http.StatusTeapot, `{ "some-error": "invalid" }`), 108 ), 109 ) 110 }) 111 112 It("returns a V3UnexpectedResponseError", func() { 113 _, _, err := NewClients(fakeConfig, testUI, true) 114 Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot})) 115 }) 116 }) 117 }) 118 119 Context("when the DialTimeout is set", func() { 120 BeforeEach(func() { 121 if runtime.GOOS == "windows" { 122 Skip("due to timing issues on windows") 123 } 124 fakeConfig.TargetReturns("https://potato.bananapants11122.co.uk") 125 fakeConfig.DialTimeoutReturns(time.Nanosecond) 126 }) 127 128 It("passes the value to the target", func() { 129 _, _, err := NewClients(fakeConfig, testUI, true) 130 Expect(err.Error()).To(MatchRegexp("TIP: If you are behind a firewall")) 131 }) 132 }) 133 134 Context("when not targetting", func() { 135 It("does not target and returns no UAA client", func() { 136 ccClient, uaaClient, err := NewClients(fakeConfig, testUI, false) 137 Expect(err).ToNot(HaveOccurred()) 138 Expect(ccClient).ToNot(BeNil()) 139 Expect(uaaClient).To(BeNil()) 140 Expect(fakeConfig.SkipSSLValidationCallCount()).To(Equal(0)) 141 }) 142 }) 143 })