github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/api/cloudcontroller/ccv2/space_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/ghttp"
    10  )
    11  
    12  var _ = Describe("Space", func() {
    13  	var client *Client
    14  
    15  	BeforeEach(func() {
    16  		client = NewTestClient()
    17  	})
    18  
    19  	Describe("GetSpaces", func() {
    20  		Context("when no errors are encountered", func() {
    21  			Context("when results are paginated", func() {
    22  				BeforeEach(func() {
    23  					response1 := `{
    24  					"next_url": "/v2/spaces?q=some-query:some-value&page=2",
    25  					"resources": [
    26  						{
    27  							"metadata": {
    28  								"guid": "space-guid-1"
    29  							},
    30  							"entity": {
    31  								"name": "space-1",
    32  								"allow_ssh": false
    33  							}
    34  						},
    35  						{
    36  							"metadata": {
    37  								"guid": "space-guid-2"
    38  							},
    39  							"entity": {
    40  								"name": "space-2",
    41  								"allow_ssh": true
    42  							}
    43  						}
    44  					]
    45  				}`
    46  					response2 := `{
    47  					"next_url": null,
    48  					"resources": [
    49  						{
    50  							"metadata": {
    51  								"guid": "space-guid-3"
    52  							},
    53  							"entity": {
    54  								"name": "space-3",
    55  								"allow_ssh": false
    56  							}
    57  						},
    58  						{
    59  							"metadata": {
    60  								"guid": "space-guid-4"
    61  							},
    62  							"entity": {
    63  								"name": "space-4",
    64  								"allow_ssh": true
    65  							}
    66  						}
    67  					]
    68  				}`
    69  					server.AppendHandlers(
    70  						CombineHandlers(
    71  							VerifyRequest(http.MethodGet, "/v2/spaces", "q=some-query:some-value"),
    72  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    73  						))
    74  					server.AppendHandlers(
    75  						CombineHandlers(
    76  							VerifyRequest(http.MethodGet, "/v2/spaces", "q=some-query:some-value&page=2"),
    77  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
    78  						))
    79  				})
    80  
    81  				It("returns paginated results and all warnings", func() {
    82  					spaces, warnings, err := client.GetSpaces([]Query{{
    83  						Filter:   "some-query",
    84  						Operator: EqualOperator,
    85  						Value:    "some-value",
    86  					}})
    87  
    88  					Expect(err).NotTo(HaveOccurred())
    89  					Expect(spaces).To(Equal([]Space{
    90  						{
    91  							GUID:     "space-guid-1",
    92  							Name:     "space-1",
    93  							AllowSSH: false,
    94  						},
    95  						{
    96  							GUID:     "space-guid-2",
    97  							Name:     "space-2",
    98  							AllowSSH: true,
    99  						},
   100  						{
   101  							GUID:     "space-guid-3",
   102  							Name:     "space-3",
   103  							AllowSSH: false,
   104  						},
   105  						{
   106  							GUID:     "space-guid-4",
   107  							Name:     "space-4",
   108  							AllowSSH: true,
   109  						},
   110  					}))
   111  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   112  				})
   113  			})
   114  		})
   115  
   116  		Context("when an error is encountered", func() {
   117  			BeforeEach(func() {
   118  				response := `{
   119    "code": 10001,
   120    "description": "Some Error",
   121    "error_code": "CF-SomeError"
   122  }`
   123  				server.AppendHandlers(
   124  					CombineHandlers(
   125  						VerifyRequest(http.MethodGet, "/v2/spaces"),
   126  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   127  					))
   128  			})
   129  
   130  			It("returns an error and all warnings", func() {
   131  				_, warnings, err := client.GetSpaces(nil)
   132  
   133  				Expect(err).To(MatchError(UnexpectedResponseError{
   134  					ResponseCode: http.StatusTeapot,
   135  					CCErrorResponse: CCErrorResponse{
   136  						Code:        10001,
   137  						Description: "Some Error",
   138  						ErrorCode:   "CF-SomeError",
   139  					},
   140  				}))
   141  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   142  			})
   143  		})
   144  	})
   145  })