github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/endpoints_test.go (about) 1 package api_test 2 3 import ( 4 "bytes" 5 "crypto/tls" 6 "fmt" 7 "log" 8 "net/http" 9 "net/http/httptest" 10 "strings" 11 "time" 12 13 . "code.cloudfoundry.org/cli/cf/api" 14 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 15 "code.cloudfoundry.org/cli/cf/models" 16 "code.cloudfoundry.org/cli/cf/net" 17 "code.cloudfoundry.org/cli/cf/terminal/terminalfakes" 18 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 19 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 20 testnet "code.cloudfoundry.org/cli/util/testhelpers/net" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 func validAPIInfoEndpoint(w http.ResponseWriter, r *http.Request) { 26 if r.URL.Path != "/v2/info" { 27 w.WriteHeader(http.StatusNotFound) 28 return 29 } 30 31 fmt.Fprintf(w, ` 32 { 33 "name": "vcap", 34 "build": "2222", 35 "support": "http://support.cloudfoundry.com", 36 "version": 2, 37 "description": "Cloud Foundry sponsored by Pivotal", 38 "app_ssh_oauth_client": "ssh-client-id", 39 "authorization_endpoint": "https://login.example.com", 40 "logging_endpoint": "wss://loggregator.foo.example.org:443", 41 "doppler_logging_endpoint": "wss://doppler.foo.example.org:4443", 42 "routing_endpoint": "http://api.example.com/routing", 43 "api_version": "42.0.0", 44 "min_cli_version": "6.5.0", 45 "min_recommended_cli_version": "6.7.0" 46 }`) 47 } 48 49 func apiInfoEndpointWithoutLogURL(w http.ResponseWriter, r *http.Request) { 50 if !strings.HasSuffix(r.URL.Path, "/v2/info") { 51 w.WriteHeader(http.StatusNotFound) 52 return 53 } 54 55 fmt.Fprintln(w, ` 56 { 57 "name": "vcap", 58 "build": "2222", 59 "support": "http://support.cloudfoundry.com", 60 "routing_endpoint": "http://api.example.com/routing", 61 "version": 2, 62 "description": "Cloud Foundry sponsored by Pivotal", 63 "authorization_endpoint": "https://login.example.com", 64 "api_version": "42.0.0" 65 }`) 66 } 67 68 var _ = Describe("Endpoints Repository", func() { 69 var ( 70 coreConfig coreconfig.ReadWriter 71 gateway net.Gateway 72 testServer *httptest.Server 73 repo RemoteInfoRepository 74 testServerFn func(w http.ResponseWriter, r *http.Request) 75 ) 76 77 BeforeEach(func() { 78 coreConfig = testconfig.NewRepository() 79 testServer = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 80 testServerFn(w, r) 81 })) 82 testServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0) 83 gateway = net.NewCloudControllerGateway(coreConfig, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 84 gateway.SetTrustedCerts(testServer.TLS.Certificates) 85 repo = NewEndpointRepository(gateway) 86 }) 87 88 AfterEach(func() { 89 testServer.Close() 90 }) 91 92 Describe("updating the endpoints", func() { 93 Context("when the API request is successful", func() { 94 var ( 95 org models.OrganizationFields 96 space models.SpaceFields 97 ) 98 BeforeEach(func() { 99 org.Name = "my-org" 100 org.GUID = "my-org-guid" 101 102 space.Name = "my-space" 103 space.GUID = "my-space-guid" 104 105 coreConfig.SetOrganizationFields(org) 106 coreConfig.SetSpaceFields(space) 107 testServerFn = validAPIInfoEndpoint 108 }) 109 110 It("returns the configuration info from /info", func() { 111 config, endpoint, err := repo.GetCCInfo(testServer.URL) 112 113 Expect(err).NotTo(HaveOccurred()) 114 Expect(config.AuthorizationEndpoint).To(Equal("https://login.example.com")) 115 Expect(config.DopplerEndpoint).To(Equal("wss://doppler.foo.example.org:4443")) 116 Expect(endpoint).To(Equal(testServer.URL)) 117 Expect(config.SSHOAuthClient).To(Equal("ssh-client-id")) 118 Expect(config.APIVersion).To(Equal("42.0.0")) 119 Expect(config.MinCLIVersion).To(Equal("6.5.0")) 120 Expect(config.MinRecommendedCLIVersion).To(Equal("6.7.0")) 121 Expect(config.RoutingAPIEndpoint).To(Equal("http://api.example.com/routing")) 122 }) 123 }) 124 125 Context("when the API request fails", func() { 126 BeforeEach(func() { 127 coreConfig.SetAPIEndpoint("example.com") 128 }) 129 130 It("returns a failure response when the server has a bad certificate", func() { 131 testServer.TLS.Certificates = []tls.Certificate{testnet.MakeExpiredTLSCert()} 132 133 _, _, apiErr := repo.GetCCInfo(testServer.URL) 134 Expect(apiErr).NotTo(BeNil()) 135 }) 136 137 It("returns a failure response when the API request fails", func() { 138 testServerFn = func(w http.ResponseWriter, r *http.Request) { 139 w.WriteHeader(http.StatusNotFound) 140 } 141 142 _, _, apiErr := repo.GetCCInfo(testServer.URL) 143 144 Expect(apiErr).NotTo(BeNil()) 145 }) 146 147 It("returns a failure response when the API returns invalid JSON", func() { 148 testServerFn = func(w http.ResponseWriter, r *http.Request) { 149 fmt.Fprintln(w, `Foo`) 150 } 151 152 _, _, apiErr := repo.GetCCInfo(testServer.URL) 153 154 Expect(apiErr).NotTo(BeNil()) 155 }) 156 }) 157 158 Describe("when the specified API url doesn't have a scheme", func() { 159 It("uses https if possible", func() { 160 testServerFn = validAPIInfoEndpoint 161 162 schemelessURL := strings.Replace(testServer.URL, "https://", "", 1) 163 config, endpoint, apiErr := repo.GetCCInfo(schemelessURL) 164 Expect(endpoint).To(Equal(testServer.URL)) 165 166 Expect(apiErr).NotTo(HaveOccurred()) 167 168 Expect(config.AuthorizationEndpoint).To(Equal("https://login.example.com")) 169 Expect(config.APIVersion).To(Equal("42.0.0")) 170 }) 171 }) 172 }) 173 })