github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/route_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 "code.cloudfoundry.org/cli/types" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/ghttp" 13 ) 14 15 var _ = Describe("Route", func() { 16 var client *Client 17 18 BeforeEach(func() { 19 client = NewTestClient() 20 }) 21 22 Describe("CreateRoute", func() { 23 When("route creation is successful", func() { 24 When("generate port is true", func() { 25 BeforeEach(func() { 26 response := ` 27 { 28 "metadata": { 29 "guid": "some-route-guid" 30 }, 31 "entity": { 32 "domain_guid": "some-domain-guid", 33 "host": "some-host", 34 "path": "some-path", 35 "port": 100000, 36 "space_guid": "some-space-guid" 37 } 38 }` 39 requestBody := map[string]interface{}{ 40 "domain_guid": "some-domain-guid", 41 "host": "some-host", 42 "path": "some-path", 43 "port": 42, 44 "space_guid": "some-space-guid", 45 } 46 server.AppendHandlers( 47 CombineHandlers( 48 VerifyRequest(http.MethodPost, "/v2/routes", "generate_port=true"), 49 VerifyJSONRepresenting(requestBody), 50 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 51 ), 52 ) 53 }) 54 55 It("creates the route with a random port", func() { 56 route, warnings, err := client.CreateRoute(Route{ 57 DomainGUID: "some-domain-guid", 58 Host: "some-host", 59 Path: "some-path", 60 Port: types.NullInt{IsSet: true, Value: 42}, 61 SpaceGUID: "some-space-guid", 62 }, true) 63 64 Expect(err).ToNot(HaveOccurred()) 65 Expect(warnings).To(ConsistOf("this is a warning")) 66 Expect(route).To(Equal(Route{ 67 DomainGUID: "some-domain-guid", 68 GUID: "some-route-guid", 69 Host: "some-host", 70 Path: "some-path", 71 Port: types.NullInt{IsSet: true, Value: 100000}, 72 SpaceGUID: "some-space-guid", 73 })) 74 }) 75 }) 76 77 When("generate route is false", func() { 78 BeforeEach(func() { 79 response := ` 80 { 81 "metadata": { 82 "guid": "some-route-guid" 83 }, 84 "entity": { 85 "domain_guid": "some-domain-guid", 86 "host": "some-host", 87 "path": "some-path", 88 "port": 42, 89 "space_guid": "some-space-guid" 90 } 91 }` 92 requestBody := map[string]interface{}{ 93 "domain_guid": "some-domain-guid", 94 "host": "some-host", 95 "path": "some-path", 96 "port": 42, 97 "space_guid": "some-space-guid", 98 } 99 server.AppendHandlers( 100 CombineHandlers( 101 VerifyRequest(http.MethodPost, "/v2/routes"), 102 VerifyJSONRepresenting(requestBody), 103 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 104 ), 105 ) 106 }) 107 108 It("creates the route with the given port", func() { 109 route, warnings, err := client.CreateRoute(Route{ 110 DomainGUID: "some-domain-guid", 111 Host: "some-host", 112 Path: "some-path", 113 Port: types.NullInt{IsSet: true, Value: 42}, 114 SpaceGUID: "some-space-guid", 115 }, false) 116 117 Expect(err).ToNot(HaveOccurred()) 118 Expect(warnings).To(ConsistOf("this is a warning")) 119 Expect(route).To(Equal(Route{ 120 DomainGUID: "some-domain-guid", 121 GUID: "some-route-guid", 122 Host: "some-host", 123 Path: "some-path", 124 Port: types.NullInt{IsSet: true, Value: 42}, 125 SpaceGUID: "some-space-guid", 126 })) 127 }) 128 }) 129 130 When("sending a basic route", func() { 131 BeforeEach(func() { 132 response := ` 133 { 134 "metadata": { 135 "guid": "some-route-guid" 136 }, 137 "entity": { 138 "domain_guid": "some-domain-guid", 139 "space_guid": "some-space-guid" 140 } 141 }` 142 requestBody := map[string]interface{}{ 143 "port": nil, 144 "domain_guid": "some-domain-guid", 145 "space_guid": "some-space-guid", 146 } 147 server.AppendHandlers( 148 CombineHandlers( 149 VerifyRequest(http.MethodPost, "/v2/routes"), 150 VerifyJSONRepresenting(requestBody), 151 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 152 ), 153 ) 154 }) 155 156 It("creates the route with only the space and domain guids", func() { 157 route, warnings, err := client.CreateRoute(Route{ 158 DomainGUID: "some-domain-guid", 159 SpaceGUID: "some-space-guid", 160 }, false) 161 162 Expect(err).ToNot(HaveOccurred()) 163 Expect(warnings).To(ConsistOf("this is a warning")) 164 Expect(route).To(Equal(Route{ 165 DomainGUID: "some-domain-guid", 166 GUID: "some-route-guid", 167 SpaceGUID: "some-space-guid", 168 })) 169 }) 170 }) 171 }) 172 173 When("the cc returns an error", func() { 174 BeforeEach(func() { 175 response := `{ 176 "code": 10001, 177 "description": "Some Error", 178 "error_code": "CF-SomeError" 179 }` 180 server.AppendHandlers( 181 CombineHandlers( 182 VerifyRequest(http.MethodPost, "/v2/routes"), 183 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 184 ), 185 ) 186 }) 187 188 It("returns an error", func() { 189 _, warnings, err := client.CreateRoute(Route{}, false) 190 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 191 ResponseCode: http.StatusTeapot, 192 V2ErrorResponse: ccerror.V2ErrorResponse{ 193 Code: 10001, 194 Description: "Some Error", 195 ErrorCode: "CF-SomeError", 196 }, 197 })) 198 Expect(warnings).To(ConsistOf("this is a warning")) 199 }) 200 }) 201 }) 202 203 Describe("DeleteRoute", func() { 204 When("the route exists", func() { 205 BeforeEach(func() { 206 server.AppendHandlers( 207 CombineHandlers( 208 VerifyRequest(http.MethodDelete, "/v2/routes/some-route-guid"), 209 RespondWith(http.StatusNoContent, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 210 ), 211 ) 212 }) 213 214 It("deletes the route and returns all warnings", func() { 215 warnings, err := client.DeleteRoute("some-route-guid") 216 Expect(err).NotTo(HaveOccurred()) 217 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 218 }) 219 }) 220 221 When("the route does not exist", func() { 222 BeforeEach(func() { 223 response := `{ 224 "code": 210002, 225 "description": "The route could not be found: some-route-guid", 226 "error_code": "CF-RouteNotFound" 227 }` 228 server.AppendHandlers( 229 CombineHandlers( 230 VerifyRequest(http.MethodDelete, "/v2/routes/some-route-guid"), 231 RespondWith(http.StatusNotFound, response), 232 ), 233 ) 234 }) 235 236 It("returns an error", func() { 237 _, err := client.DeleteRoute("some-route-guid") 238 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 239 Message: "The route could not be found: some-route-guid", 240 })) 241 }) 242 }) 243 }) 244 245 Describe("DeleteRouteApplication", func() { 246 When("the delete is successful", func() { 247 BeforeEach(func() { 248 server.AppendHandlers( 249 CombineHandlers( 250 VerifyRequest(http.MethodDelete, "/v2/routes/some-route-guid/apps/some-app-guid"), 251 RespondWith(http.StatusNoContent, nil, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 252 ), 253 ) 254 }) 255 256 It("returns the route and warnings", func() { 257 warnings, err := client.DeleteRouteApplication("some-route-guid", "some-app-guid") 258 Expect(err).ToNot(HaveOccurred()) 259 Expect(warnings).To(ConsistOf("this is a warning")) 260 }) 261 }) 262 263 When("the cc returns an error", func() { 264 BeforeEach(func() { 265 response := `{ 266 "code": 10001, 267 "description": "Some Error", 268 "error_code": "CF-SomeError" 269 }` 270 server.AppendHandlers( 271 CombineHandlers( 272 VerifyRequest(http.MethodDelete, "/v2/routes/some-route-guid/apps/some-app-guid"), 273 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 274 ), 275 ) 276 }) 277 278 It("returns an error", func() { 279 warnings, err := client.DeleteRouteApplication("some-route-guid", "some-app-guid") 280 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 281 ResponseCode: http.StatusTeapot, 282 V2ErrorResponse: ccerror.V2ErrorResponse{ 283 Code: 10001, 284 Description: "Some Error", 285 ErrorCode: "CF-SomeError", 286 }, 287 })) 288 Expect(warnings).To(ConsistOf("this is a warning")) 289 }) 290 }) 291 }) 292 293 Describe("CheckRoute", func() { 294 var ( 295 route Route 296 exists bool 297 warnings Warnings 298 executeErr error 299 ) 300 301 JustBeforeEach(func() { 302 exists, warnings, executeErr = client.CheckRoute(route) 303 }) 304 305 When("the CC errors", func() { 306 BeforeEach(func() { 307 response := `{ 308 "code": 777, 309 "description": "The route could not be found: some-route-guid", 310 "error_code": "CF-WUT" 311 }` 312 server.AppendHandlers( 313 CombineHandlers( 314 VerifyRequest(http.MethodGet, "/v2/routes/reserved/domain/some-domain-guid", "host=some-host"), 315 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 316 ), 317 ) 318 route = Route{Host: "some-host", DomainGUID: "some-domain-guid"} 319 }) 320 321 It("returns the error", func() { 322 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 323 V2ErrorResponse: ccerror.V2ErrorResponse{ 324 Code: 777, 325 Description: "The route could not be found: some-route-guid", 326 ErrorCode: "CF-WUT", 327 }, 328 ResponseCode: http.StatusTeapot, 329 })) 330 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 331 }) 332 }) 333 334 When("the CC does not error", func() { 335 When("the route exists", func() { 336 Context("with minimum params", func() { 337 BeforeEach(func() { 338 server.AppendHandlers( 339 CombineHandlers( 340 VerifyRequest(http.MethodGet, "/v2/routes/reserved/domain/some-domain-guid"), 341 RespondWith(http.StatusNoContent, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 342 ), 343 ) 344 route = Route{DomainGUID: "some-domain-guid"} 345 }) 346 347 It("does not contain any params", func() { 348 Expect(executeErr).NotTo(HaveOccurred()) 349 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 350 }) 351 }) 352 353 Context("with all the params", func() { 354 BeforeEach(func() { 355 server.AppendHandlers( 356 CombineHandlers( 357 VerifyRequest(http.MethodGet, "/v2/routes/reserved/domain/some-domain-guid", "host=some-host&path=some-path&port=42"), 358 RespondWith(http.StatusNoContent, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 359 ), 360 ) 361 route = Route{ 362 Host: "some-host", 363 DomainGUID: "some-domain-guid", 364 Path: "some-path", 365 Port: types.NullInt{IsSet: true, Value: 42}, 366 } 367 }) 368 369 It("contains all requested parameters", func() { 370 Expect(executeErr).NotTo(HaveOccurred()) 371 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 372 Expect(exists).To(BeTrue()) 373 }) 374 }) 375 }) 376 377 When("the route does not exist", func() { 378 BeforeEach(func() { 379 server.AppendHandlers( 380 CombineHandlers( 381 VerifyRequest(http.MethodGet, "/v2/routes/reserved/domain/some-domain-guid", "host=some-host"), 382 RespondWith(http.StatusNotFound, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 383 ), 384 ) 385 route = Route{Host: "some-host", DomainGUID: "some-domain-guid"} 386 }) 387 388 It("returns false", func() { 389 Expect(executeErr).NotTo(HaveOccurred()) 390 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 391 Expect(exists).To(BeFalse()) 392 }) 393 }) 394 }) 395 }) 396 397 Describe("GetApplicationRoutes", func() { 398 When("there are routes in this space", func() { 399 BeforeEach(func() { 400 response1 := `{ 401 "next_url": "/v2/apps/some-app-guid/routes?q=organization_guid:some-org-guid&page=2", 402 "resources": [ 403 { 404 "metadata": { 405 "guid": "route-guid-1", 406 "updated_at": null 407 }, 408 "entity": { 409 "host": "host-1", 410 "path": "path", 411 "port": null, 412 "domain_guid": "some-http-domain", 413 "space_guid": "some-space-guid-1" 414 } 415 }, 416 { 417 "metadata": { 418 "guid": "route-guid-2", 419 "updated_at": null 420 }, 421 "entity": { 422 "host": "host-2", 423 "path": "", 424 "port": 3333, 425 "domain_guid": "some-tcp-domain", 426 "space_guid": "some-space-guid-1" 427 } 428 } 429 ] 430 }` 431 response2 := `{ 432 "next_url": null, 433 "resources": [ 434 { 435 "metadata": { 436 "guid": "route-guid-3", 437 "updated_at": null 438 }, 439 "entity": { 440 "host": "host-3", 441 "path": "path", 442 "port": null, 443 "domain_guid": "some-http-domain", 444 "space_guid": "some-space-guid-1" 445 } 446 }, 447 { 448 "metadata": { 449 "guid": "route-guid-4", 450 "updated_at": null 451 }, 452 "entity": { 453 "host": "host-4", 454 "path": "", 455 "port": 333, 456 "domain_guid": "some-tcp-domain", 457 "space_guid": "some-space-guid-1" 458 } 459 } 460 ] 461 }` 462 server.AppendHandlers( 463 CombineHandlers( 464 VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes", "q=organization_guid:some-org-guid"), 465 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 466 ), 467 ) 468 server.AppendHandlers( 469 CombineHandlers( 470 VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes", "q=organization_guid:some-org-guid&page=2"), 471 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 472 ), 473 ) 474 }) 475 476 It("returns all the routes and all warnings", func() { 477 routes, warnings, err := client.GetApplicationRoutes("some-app-guid", Filter{ 478 Type: constant.OrganizationGUIDFilter, 479 Operator: constant.EqualOperator, 480 Values: []string{"some-org-guid"}, 481 }) 482 Expect(err).NotTo(HaveOccurred()) 483 Expect(routes).To(ConsistOf([]Route{ 484 { 485 GUID: "route-guid-1", 486 Host: "host-1", 487 Path: "path", 488 Port: types.NullInt{IsSet: false}, 489 DomainGUID: "some-http-domain", 490 SpaceGUID: "some-space-guid-1", 491 }, 492 { 493 GUID: "route-guid-2", 494 Host: "host-2", 495 Path: "", 496 Port: types.NullInt{IsSet: true, Value: 3333}, 497 DomainGUID: "some-tcp-domain", 498 SpaceGUID: "some-space-guid-1", 499 }, 500 { 501 GUID: "route-guid-3", 502 Host: "host-3", 503 Path: "path", 504 Port: types.NullInt{IsSet: false}, 505 DomainGUID: "some-http-domain", 506 SpaceGUID: "some-space-guid-1", 507 }, 508 { 509 GUID: "route-guid-4", 510 Host: "host-4", 511 Path: "", 512 Port: types.NullInt{IsSet: true, Value: 333}, 513 DomainGUID: "some-tcp-domain", 514 SpaceGUID: "some-space-guid-1", 515 }, 516 })) 517 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 518 }) 519 }) 520 521 When("there are no routes bound to the app", func() { 522 BeforeEach(func() { 523 response := `{ 524 "next_url": "", 525 "resources": [] 526 }` 527 server.AppendHandlers( 528 CombineHandlers( 529 VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes"), 530 RespondWith(http.StatusOK, response), 531 ), 532 ) 533 }) 534 535 It("returns an empty list of routes", func() { 536 routes, _, err := client.GetApplicationRoutes("some-app-guid") 537 Expect(err).NotTo(HaveOccurred()) 538 Expect(routes).To(BeEmpty()) 539 }) 540 }) 541 542 When("the app is not found", func() { 543 BeforeEach(func() { 544 response := `{ 545 "code": 10000, 546 "description": "The app could not be found: some-app-guid", 547 "error_code": "CF-AppNotFound" 548 }` 549 server.AppendHandlers( 550 CombineHandlers( 551 VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes"), 552 RespondWith(http.StatusNotFound, response), 553 ), 554 ) 555 }) 556 557 It("returns an error", func() { 558 routes, _, err := client.GetApplicationRoutes("some-app-guid") 559 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 560 Message: "The app could not be found: some-app-guid", 561 })) 562 Expect(routes).To(BeEmpty()) 563 }) 564 }) 565 }) 566 567 Describe("GetRoute", func() { 568 var ( 569 route Route 570 warnings Warnings 571 executeErr error 572 ) 573 574 JustBeforeEach(func() { 575 route, warnings, executeErr = client.GetRoute("some-route-guid") 576 }) 577 578 When("the cc returns an error", func() { 579 BeforeEach(func() { 580 response := `{ 581 "code": 1, 582 "description": "some error description", 583 "error_code": "CF-SomeError" 584 }` 585 server.AppendHandlers( 586 CombineHandlers( 587 VerifyRequest(http.MethodGet, "/v2/routes/some-route-guid"), 588 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 589 ), 590 ) 591 }) 592 593 It("returns the error", func() { 594 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 595 V2ErrorResponse: ccerror.V2ErrorResponse{ 596 Code: 1, 597 Description: "some error description", 598 ErrorCode: "CF-SomeError", 599 }, 600 ResponseCode: http.StatusTeapot, 601 })) 602 603 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 604 }) 605 }) 606 607 When("there are no errors", func() { 608 BeforeEach(func() { 609 response := `{ 610 "metadata": { 611 "guid": "route-guid-1", 612 "updated_at": null 613 }, 614 "entity": { 615 "host": "host-1", 616 "path": "path", 617 "port": null, 618 "domain_guid": "some-http-domain", 619 "space_guid": "some-space-guid-1" 620 } 621 }` 622 server.AppendHandlers( 623 CombineHandlers( 624 VerifyRequest(http.MethodGet, "/v2/routes/some-route-guid"), 625 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 626 ), 627 ) 628 }) 629 630 It("returns the route", func() { 631 Expect(executeErr).ToNot(HaveOccurred()) 632 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 633 634 Expect(route).To(Equal(Route{ 635 GUID: "route-guid-1", 636 Host: "host-1", 637 Path: "path", 638 Port: types.NullInt{IsSet: false}, 639 DomainGUID: "some-http-domain", 640 SpaceGUID: "some-space-guid-1", 641 })) 642 }) 643 }) 644 }) 645 646 Describe("GetRoutes", func() { 647 When("there are routes", func() { 648 BeforeEach(func() { 649 response1 := `{ 650 "next_url": "/v2/routes?q=organization_guid:some-org-guid&page=2", 651 "resources": [ 652 { 653 "metadata": { 654 "guid": "route-guid-1", 655 "updated_at": null 656 }, 657 "entity": { 658 "host": "host-1", 659 "path": "path", 660 "port": null, 661 "domain_guid": "some-http-domain", 662 "space_guid": "some-space-guid-1" 663 } 664 }, 665 { 666 "metadata": { 667 "guid": "route-guid-2", 668 "updated_at": null 669 }, 670 "entity": { 671 "host": "host-2", 672 "path": "", 673 "port": 3333, 674 "domain_guid": "some-tcp-domain", 675 "space_guid": "some-space-guid-1" 676 } 677 } 678 ] 679 }` 680 response2 := `{ 681 "next_url": null, 682 "resources": [ 683 { 684 "metadata": { 685 "guid": "route-guid-3", 686 "updated_at": null 687 }, 688 "entity": { 689 "host": "host-3", 690 "path": "path", 691 "port": null, 692 "domain_guid": "some-http-domain", 693 "space_guid": "some-space-guid-2" 694 } 695 }, 696 { 697 "metadata": { 698 "guid": "route-guid-4", 699 "updated_at": null 700 }, 701 "entity": { 702 "host": "host-4", 703 "path": "", 704 "port": 333, 705 "domain_guid": "some-tcp-domain", 706 "space_guid": "some-space-guid-2" 707 } 708 } 709 ] 710 }` 711 server.AppendHandlers( 712 CombineHandlers( 713 VerifyRequest(http.MethodGet, "/v2/routes", "q=organization_guid:some-org-guid"), 714 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 715 ), 716 ) 717 server.AppendHandlers( 718 CombineHandlers( 719 VerifyRequest(http.MethodGet, "/v2/routes", "q=organization_guid:some-org-guid&page=2"), 720 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 721 ), 722 ) 723 }) 724 725 It("returns all the routes and all warnings", func() { 726 routes, warnings, err := client.GetRoutes(Filter{ 727 Type: constant.OrganizationGUIDFilter, 728 Operator: constant.EqualOperator, 729 Values: []string{"some-org-guid"}, 730 }) 731 Expect(err).NotTo(HaveOccurred()) 732 Expect(routes).To(ConsistOf([]Route{ 733 { 734 GUID: "route-guid-1", 735 Host: "host-1", 736 Path: "path", 737 Port: types.NullInt{IsSet: false}, 738 DomainGUID: "some-http-domain", 739 SpaceGUID: "some-space-guid-1", 740 }, 741 { 742 GUID: "route-guid-2", 743 Host: "host-2", 744 Path: "", 745 Port: types.NullInt{IsSet: true, Value: 3333}, 746 DomainGUID: "some-tcp-domain", 747 SpaceGUID: "some-space-guid-1", 748 }, 749 { 750 GUID: "route-guid-3", 751 Host: "host-3", 752 Path: "path", 753 Port: types.NullInt{IsSet: false}, 754 DomainGUID: "some-http-domain", 755 SpaceGUID: "some-space-guid-2", 756 }, 757 { 758 GUID: "route-guid-4", 759 Host: "host-4", 760 Path: "", 761 Port: types.NullInt{IsSet: true, Value: 333}, 762 DomainGUID: "some-tcp-domain", 763 SpaceGUID: "some-space-guid-2", 764 }, 765 })) 766 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 767 }) 768 }) 769 770 When("the cc returns an error", func() { 771 BeforeEach(func() { 772 response := `{ 773 "code": 10001, 774 "description": "Some Error", 775 "error_code": "CF-SomeError" 776 }` 777 server.AppendHandlers( 778 CombineHandlers( 779 VerifyRequest(http.MethodGet, "/v2/routes"), 780 RespondWith(http.StatusTeapot, response), 781 ), 782 ) 783 }) 784 785 It("returns an error", func() { 786 _, _, err := client.GetRoutes() 787 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 788 ResponseCode: http.StatusTeapot, 789 V2ErrorResponse: ccerror.V2ErrorResponse{ 790 Code: 10001, 791 Description: "Some Error", 792 ErrorCode: "CF-SomeError", 793 }, 794 })) 795 }) 796 }) 797 }) 798 799 Describe("GetSpaceRoutes", func() { 800 When("there are routes in this space", func() { 801 BeforeEach(func() { 802 response1 := `{ 803 "next_url": "/v2/spaces/some-space-guid/routes?q=space_guid:some-space-guid&page=2", 804 "resources": [ 805 { 806 "metadata": { 807 "guid": "route-guid-1", 808 "updated_at": null 809 }, 810 "entity": { 811 "host": "host-1", 812 "path": "path", 813 "port": null, 814 "domain_guid": "some-http-domain", 815 "space_guid": "some-space-guid-1" 816 } 817 }, 818 { 819 "metadata": { 820 "guid": "route-guid-2", 821 "updated_at": null 822 }, 823 "entity": { 824 "host": "host-2", 825 "path": "", 826 "port": 3333, 827 "domain_guid": "some-tcp-domain", 828 "space_guid": "some-space-guid-1" 829 } 830 } 831 ] 832 }` 833 response2 := `{ 834 "next_url": null, 835 "resources": [ 836 { 837 "metadata": { 838 "guid": "route-guid-3", 839 "updated_at": null 840 }, 841 "entity": { 842 "host": "host-3", 843 "path": "path", 844 "port": null, 845 "domain_guid": "some-http-domain", 846 "space_guid": "some-space-guid-1" 847 } 848 }, 849 { 850 "metadata": { 851 "guid": "route-guid-4", 852 "updated_at": null 853 }, 854 "entity": { 855 "host": "host-4", 856 "path": "", 857 "port": 333, 858 "domain_guid": "some-tcp-domain", 859 "space_guid": "some-space-guid-1" 860 } 861 } 862 ] 863 }` 864 server.AppendHandlers( 865 CombineHandlers( 866 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes", "q=space_guid:some-space-guid"), 867 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 868 ), 869 ) 870 server.AppendHandlers( 871 CombineHandlers( 872 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes", "q=space_guid:some-space-guid&page=2"), 873 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 874 ), 875 ) 876 }) 877 878 It("returns all the routes and all warnings", func() { 879 routes, warnings, err := client.GetSpaceRoutes("some-space-guid", Filter{ 880 Type: constant.SpaceGUIDFilter, 881 Operator: constant.EqualOperator, 882 Values: []string{"some-space-guid"}, 883 }) 884 Expect(err).NotTo(HaveOccurred()) 885 Expect(routes).To(ConsistOf([]Route{ 886 { 887 GUID: "route-guid-1", 888 Host: "host-1", 889 Path: "path", 890 Port: types.NullInt{IsSet: false}, 891 DomainGUID: "some-http-domain", 892 SpaceGUID: "some-space-guid-1", 893 }, 894 { 895 GUID: "route-guid-2", 896 Host: "host-2", 897 Path: "", 898 Port: types.NullInt{IsSet: true, Value: 3333}, 899 DomainGUID: "some-tcp-domain", 900 SpaceGUID: "some-space-guid-1", 901 }, 902 { 903 GUID: "route-guid-3", 904 Host: "host-3", 905 Path: "path", 906 Port: types.NullInt{IsSet: false}, 907 DomainGUID: "some-http-domain", 908 SpaceGUID: "some-space-guid-1", 909 }, 910 { 911 GUID: "route-guid-4", 912 Host: "host-4", 913 Path: "", 914 Port: types.NullInt{IsSet: true, Value: 333}, 915 DomainGUID: "some-tcp-domain", 916 SpaceGUID: "some-space-guid-1", 917 }, 918 })) 919 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 920 }) 921 }) 922 923 When("there are no routes in this space", func() { 924 BeforeEach(func() { 925 response := `{ 926 "next_url": "", 927 "resources": [] 928 }` 929 server.AppendHandlers( 930 CombineHandlers( 931 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes"), 932 RespondWith(http.StatusOK, response), 933 ), 934 ) 935 }) 936 937 It("returns an empty list of routes", func() { 938 routes, _, err := client.GetSpaceRoutes("some-space-guid") 939 Expect(err).NotTo(HaveOccurred()) 940 Expect(routes).To(BeEmpty()) 941 }) 942 }) 943 944 When("the space is not found", func() { 945 BeforeEach(func() { 946 response := `{ 947 "code": 40004, 948 "description": "The app space could not be found: some-space-guid", 949 "error_code": "CF-SpaceNotFound" 950 }` 951 server.AppendHandlers( 952 CombineHandlers( 953 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes"), 954 RespondWith(http.StatusNotFound, response), 955 ), 956 ) 957 }) 958 959 It("returns an error", func() { 960 routes, _, err := client.GetSpaceRoutes("some-space-guid") 961 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 962 Message: "The app space could not be found: some-space-guid", 963 })) 964 Expect(routes).To(BeEmpty()) 965 }) 966 }) 967 }) 968 969 Describe("UpdateRouteApplication", func() { 970 When("route mapping is successful", func() { 971 BeforeEach(func() { 972 response := ` 973 { 974 "metadata": { 975 "guid": "some-route-guid" 976 }, 977 "entity": { 978 "domain_guid": "some-domain-guid", 979 "host": "some-host", 980 "path": "some-path", 981 "port": 42, 982 "space_guid": "some-space-guid" 983 } 984 }` 985 server.AppendHandlers( 986 CombineHandlers( 987 VerifyRequest(http.MethodPut, "/v2/routes/some-route-guid/apps/some-app-guid"), 988 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 989 ), 990 ) 991 }) 992 993 It("returns the route and warnings", func() { 994 route, warnings, err := client.UpdateRouteApplication("some-route-guid", "some-app-guid") 995 Expect(err).ToNot(HaveOccurred()) 996 Expect(warnings).To(ConsistOf("this is a warning")) 997 998 Expect(route).To(Equal(Route{ 999 DomainGUID: "some-domain-guid", 1000 GUID: "some-route-guid", 1001 Host: "some-host", 1002 Path: "some-path", 1003 Port: types.NullInt{IsSet: true, Value: 42}, 1004 SpaceGUID: "some-space-guid", 1005 })) 1006 }) 1007 }) 1008 1009 When("the cc returns an error", func() { 1010 BeforeEach(func() { 1011 response := `{ 1012 "code": 10001, 1013 "description": "Some Error", 1014 "error_code": "CF-SomeError" 1015 }` 1016 server.AppendHandlers( 1017 CombineHandlers( 1018 VerifyRequest(http.MethodPut, "/v2/routes/some-route-guid/apps/some-app-guid"), 1019 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 1020 ), 1021 ) 1022 }) 1023 1024 It("returns an error", func() { 1025 _, warnings, err := client.UpdateRouteApplication("some-route-guid", "some-app-guid") 1026 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 1027 ResponseCode: http.StatusTeapot, 1028 V2ErrorResponse: ccerror.V2ErrorResponse{ 1029 Code: 10001, 1030 Description: "Some Error", 1031 ErrorCode: "CF-SomeError", 1032 }, 1033 })) 1034 Expect(warnings).To(ConsistOf("this is a warning")) 1035 }) 1036 }) 1037 }) 1038 })