github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/routing_api_test.go (about) 1 package api_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "strconv" 7 "time" 8 9 "code.cloudfoundry.org/cli/cf/api" 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/errors" 12 "code.cloudfoundry.org/cli/cf/models" 13 "code.cloudfoundry.org/cli/cf/net" 14 "code.cloudfoundry.org/cli/cf/terminal/terminalfakes" 15 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 16 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 "github.com/onsi/gomega/ghttp" 20 ) 21 22 var _ = Describe("RoutingApi", func() { 23 24 var ( 25 repo api.RoutingAPIRepository 26 configRepo coreconfig.Repository 27 routingAPIServer *ghttp.Server 28 ) 29 30 BeforeEach(func() { 31 configRepo = testconfig.NewRepositoryWithDefaults() 32 gateway := net.NewRoutingAPIGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 33 34 repo = api.NewRoutingAPIRepository(configRepo, gateway) 35 }) 36 37 AfterEach(func() { 38 routingAPIServer.Close() 39 }) 40 41 Describe("ListRouterGroups", func() { 42 43 Context("when routing api return router groups", func() { 44 BeforeEach(func() { 45 routingAPIServer = ghttp.NewServer() 46 routingAPIServer.RouteToHandler("GET", "/v1/router_groups", 47 func(w http.ResponseWriter, req *http.Request) { 48 responseBody := []byte(`[ 49 { 50 "guid": "bad25cff-9332-48a6-8603-b619858e7992", 51 "name": "default-tcp", 52 "type": "tcp" 53 }]`) 54 w.Header().Set("Content-Length", strconv.Itoa(len(responseBody))) 55 w.Header().Set("Content-Type", "application/json") 56 w.WriteHeader(http.StatusOK) 57 w.Write(responseBody) 58 }) 59 configRepo.SetRoutingAPIEndpoint(routingAPIServer.URL()) 60 }) 61 62 It("lists routing groups", func() { 63 cb := func(grp models.RouterGroup) bool { 64 Expect(grp).To(Equal(models.RouterGroup{ 65 GUID: "bad25cff-9332-48a6-8603-b619858e7992", 66 Name: "default-tcp", 67 Type: "tcp", 68 })) 69 return true 70 } 71 err := repo.ListRouterGroups(cb) 72 Expect(err).ToNot(HaveOccurred()) 73 }) 74 }) 75 76 Context("when routing api returns an empty response ", func() { 77 BeforeEach(func() { 78 routingAPIServer = ghttp.NewServer() 79 routingAPIServer.RouteToHandler("GET", "/v1/router_groups", 80 func(w http.ResponseWriter, req *http.Request) { 81 responseBody := []byte("[]") 82 w.Header().Set("Content-Length", strconv.Itoa(len(responseBody))) 83 w.Header().Set("Content-Type", "application/json") 84 w.WriteHeader(http.StatusOK) 85 w.Write(responseBody) 86 }) 87 configRepo.SetRoutingAPIEndpoint(routingAPIServer.URL()) 88 }) 89 90 It("doesn't list any router groups", func() { 91 cb := func(grp models.RouterGroup) bool { 92 Fail(fmt.Sprintf("Not expected to receive callback for router group:%#v", grp)) 93 return false 94 } 95 err := repo.ListRouterGroups(cb) 96 Expect(err).ToNot(HaveOccurred()) 97 }) 98 }) 99 100 Context("when routing api returns an error ", func() { 101 BeforeEach(func() { 102 routingAPIServer = ghttp.NewServer() 103 routingAPIServer.RouteToHandler("GET", "/v1/router_groups", 104 func(w http.ResponseWriter, req *http.Request) { 105 w.WriteHeader(http.StatusUnauthorized) 106 w.Write([]byte(`{"name":"UnauthorizedError","message":"token is expired"}`)) 107 }) 108 configRepo.SetRoutingAPIEndpoint(routingAPIServer.URL()) 109 }) 110 111 It("doesn't list any router groups and displays error message", func() { 112 cb := func(grp models.RouterGroup) bool { 113 Fail(fmt.Sprintf("Not expected to receive callback for router group:%#v", grp)) 114 return false 115 } 116 117 err := repo.ListRouterGroups(cb) 118 Expect(err).To(HaveOccurred()) 119 Expect(err.Error()).To(ContainSubstring("token is expired")) 120 Expect(err.(errors.HTTPError).ErrorCode()).To(ContainSubstring("UnauthorizedError")) 121 Expect(err.(errors.HTTPError).StatusCode()).To(Equal(http.StatusUnauthorized)) 122 }) 123 }) 124 }) 125 })