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