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