github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/net/cloud_controller_gateway_test.go (about) 1 package net_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "time" 8 9 "github.com/cloudfoundry/cli/cf/configuration/coreconfig" 10 "github.com/cloudfoundry/cli/cf/errors" 11 . "github.com/cloudfoundry/cli/cf/net" 12 "github.com/cloudfoundry/cli/cf/terminal/terminalfakes" 13 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 14 15 "github.com/cloudfoundry/cli/cf/trace/tracefakes" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var failingCloudControllerRequest = func(writer http.ResponseWriter, request *http.Request) { 21 writer.WriteHeader(http.StatusBadRequest) 22 jsonResponse := `{ "code": 210003, "description": "The host is taken: test1" }` 23 fmt.Fprintln(writer, jsonResponse) 24 } 25 26 var invalidTokenCloudControllerRequest = func(writer http.ResponseWriter, request *http.Request) { 27 writer.WriteHeader(http.StatusBadRequest) 28 jsonResponse := `{ "code": 1000, "description": "The token is invalid" }` 29 fmt.Fprintln(writer, jsonResponse) 30 } 31 32 var _ = Describe("Cloud Controller Gateway", func() { 33 var gateway Gateway 34 var config coreconfig.Reader 35 var timeout string 36 37 BeforeEach(func() { 38 timeout = "1" 39 }) 40 41 JustBeforeEach(func() { 42 config = testconfig.NewRepository() 43 gateway = NewCloudControllerGateway(config, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), timeout) 44 }) 45 46 It("parses error responses", func() { 47 ts := httptest.NewTLSServer(http.HandlerFunc(failingCloudControllerRequest)) 48 defer ts.Close() 49 gateway.SetTrustedCerts(ts.TLS.Certificates) 50 51 request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil) 52 _, apiErr = gateway.PerformRequest(request) 53 54 Expect(apiErr).NotTo(BeNil()) 55 Expect(apiErr.Error()).To(ContainSubstring("The host is taken: test1")) 56 Expect(apiErr.(errors.HTTPError).ErrorCode()).To(ContainSubstring("210003")) 57 }) 58 59 It("parses invalid token responses", func() { 60 ts := httptest.NewTLSServer(http.HandlerFunc(invalidTokenCloudControllerRequest)) 61 defer ts.Close() 62 gateway.SetTrustedCerts(ts.TLS.Certificates) 63 64 request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil) 65 _, apiErr = gateway.PerformRequest(request) 66 67 Expect(apiErr).NotTo(BeNil()) 68 Expect(apiErr.Error()).To(ContainSubstring("The token is invalid")) 69 Expect(apiErr.(*errors.InvalidTokenError)).To(HaveOccurred()) 70 }) 71 72 It("uses the set dial timeout", func() { 73 Expect(gateway.DialTimeout).To(Equal(1 * time.Second)) 74 }) 75 76 Context("with an invalid timeout", func() { 77 BeforeEach(func() { 78 timeout = "" 79 }) 80 81 It("uses the default dial timeout", func() { 82 Expect(gateway.DialTimeout).To(Equal(5 * time.Second)) 83 }) 84 }) 85 })