github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/route_mapping_test.go (about) 1 package ccv2_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("RouteMappings", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("GetRouteMapping", func() { 22 var ( 23 routeMapping RouteMapping 24 warnings Warnings 25 executeErr error 26 ) 27 28 JustBeforeEach(func() { 29 routeMapping, warnings, executeErr = client.GetRouteMapping("some-route-mapping-guid") 30 }) 31 32 When("the cc returns an error", func() { 33 BeforeEach(func() { 34 response := `{ 35 "code": 1, 36 "description": "some error description", 37 "error_code": "CF-SomeError" 38 }` 39 server.AppendHandlers( 40 CombineHandlers( 41 VerifyRequest(http.MethodGet, "/v2/route_mappings/some-route-mapping-guid"), 42 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 43 ), 44 ) 45 }) 46 47 It("returns the error", func() { 48 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 49 V2ErrorResponse: ccerror.V2ErrorResponse{ 50 Code: 1, 51 Description: "some error description", 52 ErrorCode: "CF-SomeError", 53 }, 54 ResponseCode: http.StatusTeapot, 55 })) 56 57 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 58 }) 59 }) 60 61 When("there are no errors", func() { 62 BeforeEach(func() { 63 response := `{ 64 "metadata": { 65 "guid": "route-mapping-guid-1", 66 "updated_at": null 67 }, 68 "entity": { 69 "app_port": 8888, 70 "app_guid": "some-app-guid-1", 71 "route_guid": "some-route-guid-1", 72 "app_url": "/v2/apps/some-app-guid-1", 73 "route_url": "/v2/routes/some-route-guid-1" 74 } 75 }` 76 server.AppendHandlers( 77 CombineHandlers( 78 VerifyRequest(http.MethodGet, "/v2/route_mappings/some-route-mapping-guid"), 79 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 80 ), 81 ) 82 }) 83 84 It("returns the route mapping", func() { 85 Expect(executeErr).ToNot(HaveOccurred()) 86 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 87 88 Expect(routeMapping).To(Equal(RouteMapping{ 89 GUID: "route-mapping-guid-1", 90 AppGUID: "some-app-guid-1", 91 RouteGUID: "some-route-guid-1", 92 })) 93 }) 94 }) 95 }) 96 97 Describe("GetRouteMappings", func() { 98 When("there are routes", func() { 99 BeforeEach(func() { 100 response1 := `{ 101 "next_url": "/v2/route_mappings?q=organization_guid:some-org-guid&page=2", 102 "resources": [ 103 { 104 "metadata": { 105 "guid": "route-mapping-guid-1", 106 "updated_at": null 107 }, 108 "entity": { 109 "app_port": 8888, 110 "app_guid": "some-app-guid-1", 111 "route_guid": "some-route-guid-1", 112 "app_url": "/v2/apps/some-app-guid-1", 113 "route_url": "/v2/routes/some-route-guid-1" 114 } 115 }, 116 { 117 "metadata": { 118 "guid": "route-mapping-guid-2", 119 "updated_at": null 120 }, 121 "entity": { 122 "app_port": 8888, 123 "app_guid": "some-app-guid-2", 124 "route_guid": "some-route-guid-2", 125 "app_url": "/v2/apps/some-app-guid-2", 126 "route_url": "/v2/routes/some-route-guid-2" 127 } 128 } 129 ] 130 }` 131 response2 := `{ 132 "next_url": null, 133 "resources": [ 134 { 135 "metadata": { 136 "guid": "route-mapping-guid-3", 137 "updated_at": null 138 }, 139 "entity": { 140 "app_port": 8888, 141 "app_guid": "some-app-guid-3", 142 "route_guid": "some-route-guid-3", 143 "app_url": "/v2/apps/some-app-guid-3", 144 "route_url": "/v2/routes/some-route-guid-3" 145 } 146 }, 147 { 148 "metadata": { 149 "guid": "route-mapping-guid-4", 150 "updated_at": null 151 }, 152 "entity": { 153 "app_port": 8888, 154 "app_guid": "some-app-guid-4", 155 "route_guid": "some-route-guid-4", 156 "app_url": "/v2/apps/some-app-guid-4", 157 "route_url": "/v2/routes/some-route-guid-4" 158 } 159 } 160 ] 161 }` 162 server.AppendHandlers( 163 CombineHandlers( 164 VerifyRequest(http.MethodGet, "/v2/route_mappings", "q=organization_guid:some-org-guid"), 165 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 166 ), 167 ) 168 server.AppendHandlers( 169 CombineHandlers( 170 VerifyRequest(http.MethodGet, "/v2/route_mappings", "q=organization_guid:some-org-guid&page=2"), 171 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 172 ), 173 ) 174 }) 175 176 It("returns all the route mappings and all warnings", func() { 177 routeMappings, warnings, err := client.GetRouteMappings(Filter{ 178 Type: constant.OrganizationGUIDFilter, 179 Operator: constant.EqualOperator, 180 Values: []string{"some-org-guid"}, 181 }) 182 Expect(err).NotTo(HaveOccurred()) 183 Expect(routeMappings).To(ConsistOf([]RouteMapping{ 184 { 185 GUID: "route-mapping-guid-1", 186 AppGUID: "some-app-guid-1", 187 RouteGUID: "some-route-guid-1", 188 }, 189 { 190 GUID: "route-mapping-guid-2", 191 AppGUID: "some-app-guid-2", 192 RouteGUID: "some-route-guid-2", 193 }, 194 { 195 GUID: "route-mapping-guid-3", 196 AppGUID: "some-app-guid-3", 197 RouteGUID: "some-route-guid-3", 198 }, 199 { 200 GUID: "route-mapping-guid-4", 201 AppGUID: "some-app-guid-4", 202 RouteGUID: "some-route-guid-4", 203 }, 204 })) 205 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 206 }) 207 }) 208 209 When("the cc returns an error", func() { 210 BeforeEach(func() { 211 response := `{ 212 "code": 10001, 213 "description": "Some Error", 214 "error_code": "CF-SomeError" 215 }` 216 server.AppendHandlers( 217 CombineHandlers( 218 VerifyRequest(http.MethodGet, "/v2/route_mappings"), RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning, this is another warning"}}))) 219 }) 220 221 It("returns an error", func() { 222 _, warnings, err := client.GetRouteMappings() 223 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 224 ResponseCode: http.StatusTeapot, 225 V2ErrorResponse: ccerror.V2ErrorResponse{ 226 Code: 10001, 227 Description: "Some Error", 228 ErrorCode: "CF-SomeError", 229 }, 230 })) 231 232 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 233 }) 234 }) 235 }) 236 })