github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Route", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("GetApplicationRoutes", func() {
    21  		Context("when there are routes in this space", func() {
    22  			BeforeEach(func() {
    23  				response1 := `{
    24  				"next_url": "/v2/apps/some-app-guid/routes?q=organization_guid:some-org-guid&page=2",
    25  				"resources": [
    26  					{
    27  						"metadata": {
    28  							"guid": "route-guid-1",
    29  							"updated_at": null
    30  						},
    31  						"entity": {
    32  							"host": "host-1",
    33  							"path": "path",
    34  							"port": null,
    35  							"domain_guid": "some-http-domain"
    36  						}
    37  					},
    38  					{
    39  						"metadata": {
    40  							"guid": "route-guid-2",
    41  							"updated_at": null
    42  						},
    43  						"entity": {
    44  							"host": "host-2",
    45  							"path": "",
    46  							"port": 3333,
    47  							"domain_guid": "some-tcp-domain"
    48  						}
    49  					}
    50  				]
    51  			}`
    52  				response2 := `{
    53  				"next_url": null,
    54  				"resources": [
    55  					{
    56  						"metadata": {
    57  							"guid": "route-guid-3",
    58  							"updated_at": null
    59  						},
    60  						"entity": {
    61  							"host": "host-3",
    62  							"path": "path",
    63  							"port": null,
    64  							"domain_guid": "some-http-domain"
    65  						}
    66  					},
    67  					{
    68  						"metadata": {
    69  							"guid": "route-guid-4",
    70  							"updated_at": null
    71  						},
    72  						"entity": {
    73  							"host": "host-4",
    74  							"path": "",
    75  							"port": 333,
    76  							"domain_guid": "some-tcp-domain"
    77  						}
    78  					}
    79  				]
    80  			}`
    81  				server.AppendHandlers(
    82  					CombineHandlers(
    83  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes", "q=organization_guid:some-org-guid"),
    84  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    85  					),
    86  				)
    87  				server.AppendHandlers(
    88  					CombineHandlers(
    89  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes", "q=organization_guid:some-org-guid&page=2"),
    90  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
    91  					),
    92  				)
    93  			})
    94  
    95  			It("returns all the routes and all warnings", func() {
    96  				routes, warnings, err := client.GetApplicationRoutes("some-app-guid", []Query{{
    97  					Filter:   OrganizationGUIDFilter,
    98  					Operator: EqualOperator,
    99  					Value:    "some-org-guid",
   100  				}})
   101  				Expect(err).NotTo(HaveOccurred())
   102  				Expect(routes).To(ConsistOf([]Route{
   103  					{
   104  						GUID:       "route-guid-1",
   105  						Host:       "host-1",
   106  						Path:       "path",
   107  						Port:       0,
   108  						DomainGUID: "some-http-domain",
   109  					},
   110  					{
   111  						GUID:       "route-guid-2",
   112  						Host:       "host-2",
   113  						Path:       "",
   114  						Port:       3333,
   115  						DomainGUID: "some-tcp-domain",
   116  					},
   117  					{
   118  						GUID:       "route-guid-3",
   119  						Host:       "host-3",
   120  						Path:       "path",
   121  						Port:       0,
   122  						DomainGUID: "some-http-domain",
   123  					},
   124  					{
   125  						GUID:       "route-guid-4",
   126  						Host:       "host-4",
   127  						Path:       "",
   128  						Port:       333,
   129  						DomainGUID: "some-tcp-domain",
   130  					},
   131  				}))
   132  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   133  			})
   134  		})
   135  
   136  		Context("when there are no routes bound to the app", func() {
   137  			BeforeEach(func() {
   138  				response := `{
   139  				"next_url": "",
   140  				"resources": []
   141  			}`
   142  				server.AppendHandlers(
   143  					CombineHandlers(
   144  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes"),
   145  						RespondWith(http.StatusOK, response),
   146  					),
   147  				)
   148  			})
   149  
   150  			It("returns an empty list of routes", func() {
   151  				routes, _, err := client.GetApplicationRoutes("some-app-guid", nil)
   152  				Expect(err).NotTo(HaveOccurred())
   153  				Expect(routes).To(BeEmpty())
   154  			})
   155  		})
   156  
   157  		Context("when the app is not found", func() {
   158  			BeforeEach(func() {
   159  				response := `{
   160  					"code": 10000,
   161  					"description": "The app could not be found: some-app-guid",
   162  					"error_code": "CF-AppNotFound"
   163  				}`
   164  				server.AppendHandlers(
   165  					CombineHandlers(
   166  						VerifyRequest(http.MethodGet, "/v2/apps/some-app-guid/routes"),
   167  						RespondWith(http.StatusNotFound, response),
   168  					),
   169  				)
   170  			})
   171  
   172  			It("returns an error", func() {
   173  				routes, _, err := client.GetApplicationRoutes("some-app-guid", nil)
   174  				Expect(err).To(MatchError(cloudcontroller.ResourceNotFoundError{
   175  					Message: "The app could not be found: some-app-guid",
   176  				}))
   177  				Expect(routes).To(BeEmpty())
   178  			})
   179  		})
   180  	})
   181  
   182  	Describe("GetSpaceRoutes", func() {
   183  		Context("when there are routes in this space", func() {
   184  			BeforeEach(func() {
   185  				response1 := `{
   186  				"next_url": "/v2/spaces/some-space-guid/routes?q=space_guid:some-space-guid&page=2",
   187  				"resources": [
   188  					{
   189  						"metadata": {
   190  							"guid": "route-guid-1",
   191  							"updated_at": null
   192  						},
   193  						"entity": {
   194  							"host": "host-1",
   195  							"path": "path",
   196  							"port": null,
   197  							"domain_guid": "some-http-domain"
   198  						}
   199  					},
   200  					{
   201  						"metadata": {
   202  							"guid": "route-guid-2",
   203  							"updated_at": null
   204  						},
   205  						"entity": {
   206  							"host": "host-2",
   207  							"path": "",
   208  							"port": 3333,
   209  							"domain_guid": "some-tcp-domain"
   210  						}
   211  					}
   212  				]
   213  			}`
   214  				response2 := `{
   215  				"next_url": null,
   216  				"resources": [
   217  					{
   218  						"metadata": {
   219  							"guid": "route-guid-3",
   220  							"updated_at": null
   221  						},
   222  						"entity": {
   223  							"host": "host-3",
   224  							"path": "path",
   225  							"port": null,
   226  							"domain_guid": "some-http-domain"
   227  						}
   228  					},
   229  					{
   230  						"metadata": {
   231  							"guid": "route-guid-4",
   232  							"updated_at": null
   233  						},
   234  						"entity": {
   235  							"host": "host-4",
   236  							"path": "",
   237  							"port": 333,
   238  							"domain_guid": "some-tcp-domain"
   239  						}
   240  					}
   241  				]
   242  			}`
   243  				server.AppendHandlers(
   244  					CombineHandlers(
   245  						VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes", "q=space_guid:some-space-guid"),
   246  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   247  					),
   248  				)
   249  				server.AppendHandlers(
   250  					CombineHandlers(
   251  						VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes", "q=space_guid:some-space-guid&page=2"),
   252  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   253  					),
   254  				)
   255  			})
   256  
   257  			It("returns all the routes and all warnings", func() {
   258  				routes, warnings, err := client.GetSpaceRoutes("some-space-guid", []Query{{
   259  					Filter:   SpaceGUIDFilter,
   260  					Operator: EqualOperator,
   261  					Value:    "some-space-guid",
   262  				}})
   263  				Expect(err).NotTo(HaveOccurred())
   264  				Expect(routes).To(ConsistOf([]Route{
   265  					{
   266  						GUID:       "route-guid-1",
   267  						Host:       "host-1",
   268  						Path:       "path",
   269  						Port:       0,
   270  						DomainGUID: "some-http-domain",
   271  					},
   272  					{
   273  						GUID:       "route-guid-2",
   274  						Host:       "host-2",
   275  						Path:       "",
   276  						Port:       3333,
   277  						DomainGUID: "some-tcp-domain",
   278  					},
   279  					{
   280  						GUID:       "route-guid-3",
   281  						Host:       "host-3",
   282  						Path:       "path",
   283  						Port:       0,
   284  						DomainGUID: "some-http-domain",
   285  					},
   286  					{
   287  						GUID:       "route-guid-4",
   288  						Host:       "host-4",
   289  						Path:       "",
   290  						Port:       333,
   291  						DomainGUID: "some-tcp-domain",
   292  					},
   293  				}))
   294  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   295  			})
   296  		})
   297  
   298  		Context("when there are no routes in this space", func() {
   299  			BeforeEach(func() {
   300  				response := `{
   301  				"next_url": "",
   302  				"resources": []
   303  			}`
   304  				server.AppendHandlers(
   305  					CombineHandlers(
   306  						VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes"),
   307  						RespondWith(http.StatusOK, response),
   308  					),
   309  				)
   310  			})
   311  
   312  			It("returns an empty list of routes", func() {
   313  				routes, _, err := client.GetSpaceRoutes("some-space-guid", nil)
   314  				Expect(err).NotTo(HaveOccurred())
   315  				Expect(routes).To(BeEmpty())
   316  			})
   317  		})
   318  
   319  		Context("when the space is not found", func() {
   320  			BeforeEach(func() {
   321  				response := `{
   322  					"code": 40004,
   323  					"description": "The app space could not be found: some-space-guid",
   324  					"error_code": "CF-SpaceNotFound"
   325  				}`
   326  				server.AppendHandlers(
   327  					CombineHandlers(
   328  						VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/routes"),
   329  						RespondWith(http.StatusNotFound, response),
   330  					),
   331  				)
   332  			})
   333  
   334  			It("returns an error", func() {
   335  				routes, _, err := client.GetSpaceRoutes("some-space-guid", nil)
   336  				Expect(err).To(MatchError(cloudcontroller.ResourceNotFoundError{
   337  					Message: "The app space could not be found: some-space-guid",
   338  				}))
   339  				Expect(routes).To(BeEmpty())
   340  			})
   341  		})
   342  	})
   343  
   344  	Describe("DeleteRoute", func() {
   345  		Context("when the route exists", func() {
   346  			BeforeEach(func() {
   347  				server.AppendHandlers(
   348  					CombineHandlers(
   349  						VerifyRequest(http.MethodDelete, "/v2/routes/some-route-guid"),
   350  						RespondWith(http.StatusNoContent, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   351  					),
   352  				)
   353  			})
   354  
   355  			It("deletes the route and returns all warnings", func() {
   356  				warnings, err := client.DeleteRoute("some-route-guid")
   357  				Expect(err).NotTo(HaveOccurred())
   358  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   359  			})
   360  		})
   361  
   362  		Context("when the route does not exist", func() {
   363  			BeforeEach(func() {
   364  				response := `{
   365  				"code": 210002,
   366  				"description": "The route could not be found: some-route-guid",
   367  				"error_code": "CF-RouteNotFound"
   368  			}`
   369  				server.AppendHandlers(
   370  					CombineHandlers(
   371  						VerifyRequest(http.MethodDelete, "/v2/routes/some-route-guid"),
   372  						RespondWith(http.StatusNotFound, response),
   373  					),
   374  				)
   375  			})
   376  
   377  			It("returns an error", func() {
   378  				_, err := client.DeleteRoute("some-route-guid")
   379  				Expect(err).To(MatchError(cloudcontroller.ResourceNotFoundError{
   380  					Message: "The route could not be found: some-route-guid",
   381  				}))
   382  			})
   383  		})
   384  	})
   385  })