github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/api/cloudcontroller/ccv3/space_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror"
     9  	. "github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv3"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Spaces", func() {
    16  	var client *Client
    17  
    18  	BeforeEach(func() {
    19  		client = NewTestClient()
    20  	})
    21  
    22  	Describe("GetSpaces", func() {
    23  		Context("when spaces exist", func() {
    24  			BeforeEach(func() {
    25  				response1 := fmt.Sprintf(`{
    26  	"pagination": {
    27  		"next": {
    28  			"href": "%s/v3/spaces?names=some-space-name&page=2&per_page=2"
    29  		}
    30  	},
    31    "resources": [
    32      {
    33        "name": "space-name-1",
    34        "guid": "space-guid-1"
    35      },
    36      {
    37        "name": "space-name-2",
    38        "guid": "space-guid-2"
    39      }
    40    ]
    41  }`, server.URL())
    42  				response2 := `{
    43  	"pagination": {
    44  		"next": null
    45  	},
    46  	"resources": [
    47  	  {
    48        "name": "space-name-3",
    49  		  "guid": "space-guid-3"
    50  		}
    51  	]
    52  }`
    53  
    54  				server.AppendHandlers(
    55  					CombineHandlers(
    56  						VerifyRequest(http.MethodGet, "/v3/spaces", "names=some-space-name"),
    57  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    58  					),
    59  				)
    60  				server.AppendHandlers(
    61  					CombineHandlers(
    62  						VerifyRequest(http.MethodGet, "/v3/spaces", "names=some-space-name&page=2&per_page=2"),
    63  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("returns the queried spaces and all warnings", func() {
    69  				spaces, warnings, err := client.GetSpaces(url.Values{
    70  					NameFilter: []string{"some-space-name"},
    71  				})
    72  				Expect(err).NotTo(HaveOccurred())
    73  
    74  				Expect(spaces).To(ConsistOf(
    75  					Space{Name: "space-name-1", GUID: "space-guid-1"},
    76  					Space{Name: "space-name-2", GUID: "space-guid-2"},
    77  					Space{Name: "space-name-3", GUID: "space-guid-3"},
    78  				))
    79  				Expect(warnings).To(ConsistOf("this is a warning", "this is another warning"))
    80  			})
    81  		})
    82  
    83  		Context("when the cloud controller returns errors and warnings", func() {
    84  			BeforeEach(func() {
    85  				response := `{
    86    "errors": [
    87      {
    88        "code": 10008,
    89        "detail": "The request is semantically invalid: command presence",
    90        "title": "CF-UnprocessableEntity"
    91      },
    92      {
    93        "code": 10010,
    94        "detail": "Space not found",
    95        "title": "CF-SpaceNotFound"
    96      }
    97    ]
    98  }`
    99  				server.AppendHandlers(
   100  					CombineHandlers(
   101  						VerifyRequest(http.MethodGet, "/v3/spaces"),
   102  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   103  					),
   104  				)
   105  			})
   106  
   107  			It("returns the error and all warnings", func() {
   108  				_, warnings, err := client.GetSpaces(nil)
   109  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
   110  					ResponseCode: http.StatusTeapot,
   111  					V3ErrorResponse: ccerror.V3ErrorResponse{
   112  						Errors: []ccerror.V3Error{
   113  							{
   114  								Code:   10008,
   115  								Detail: "The request is semantically invalid: command presence",
   116  								Title:  "CF-UnprocessableEntity",
   117  							},
   118  							{
   119  								Code:   10010,
   120  								Detail: "Space not found",
   121  								Title:  "CF-SpaceNotFound",
   122  							},
   123  						},
   124  					},
   125  				}))
   126  				Expect(warnings).To(ConsistOf("this is a warning"))
   127  			})
   128  		})
   129  	})
   130  })