github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v7/shared/new_clients_test.go (about) 1 package shared_test 2 3 import ( 4 "net/http" 5 "runtime" 6 "time" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 . "code.cloudfoundry.org/cli/command/v7/shared" 12 "code.cloudfoundry.org/cli/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 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 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 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("dial"))) 65 }) 66 }) 67 68 When("the error is a cloud controller api not found error", func() { 69 BeforeEach(func() { 70 server.AppendHandlers( 71 CombineHandlers( 72 VerifyRequest(http.MethodGet, "/"), 73 RespondWith(http.StatusNotFound, "something which is not json"), 74 ), 75 ) 76 }) 77 78 It("returns a command api not found error", func() { 79 _, _, err := NewClients(fakeConfig, testUI, true, "") 80 Expect(err).To(MatchError(ccerror.APINotFoundError{URL: server.URL()})) 81 }) 82 }) 83 84 When("the error is a V3UnexpectedResponseError and the status code is 404", func() { 85 BeforeEach(func() { 86 server.AppendHandlers( 87 CombineHandlers( 88 VerifyRequest(http.MethodGet, "/"), 89 RespondWith(http.StatusNotFound, "{}"), 90 ), 91 ) 92 }) 93 94 It("returns a V3APIDoesNotExistError", func() { 95 _, _, err := NewClients(fakeConfig, testUI, true, "") 96 expectedErr := ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusNotFound} 97 Expect(err).To(MatchError(expectedErr)) 98 }) 99 }) 100 101 When("the error is generic and the body is valid json", func() { 102 BeforeEach(func() { 103 server.AppendHandlers( 104 CombineHandlers( 105 VerifyRequest(http.MethodGet, "/"), 106 RespondWith(http.StatusTeapot, `{ "some-error": "invalid" }`), 107 ), 108 ) 109 }) 110 111 It("returns a V3UnexpectedResponseError", func() { 112 _, _, err := NewClients(fakeConfig, testUI, true, "") 113 Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot})) 114 }) 115 }) 116 }) 117 118 When("the DialTimeout is set", func() { 119 BeforeEach(func() { 120 if runtime.GOOS == "windows" { 121 Skip("due to timing issues on windows") 122 } 123 fakeConfig.TargetReturns("https://potato.bananapants11122.co.uk") 124 fakeConfig.DialTimeoutReturns(time.Nanosecond) 125 }) 126 127 It("passes the value to the target", func() { 128 _, _, err := NewClients(fakeConfig, testUI, true, "") 129 Expect(err.Error()).To(MatchRegexp("timeout")) 130 }) 131 }) 132 133 When("not targetting", func() { 134 It("does not target and returns no UAA client", func() { 135 ccClient, uaaClient, err := NewClients(fakeConfig, testUI, false, "") 136 Expect(err).ToNot(HaveOccurred()) 137 Expect(ccClient).ToNot(BeNil()) 138 Expect(uaaClient).To(BeNil()) 139 Expect(fakeConfig.SkipSSLValidationCallCount()).To(Equal(0)) 140 }) 141 }) 142 })