github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/curl_test.go (about) 1 package v7action_test 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "net/http" 7 "os" 8 9 . "code.cloudfoundry.org/cli/actor/v7action" 10 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Curl Actions", func() { 17 Describe("MakeCurlRequest", func() { 18 var ( 19 actor *Actor 20 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 21 fakeConfig *v7actionfakes.FakeConfig 22 23 method string 24 path string 25 customHeaders []string 26 data string 27 failOnHTTPError bool 28 29 mockResponseBody []byte 30 mockHTTPResponse *http.Response 31 mockErr error 32 33 responseBody []byte 34 httpResponse *http.Response 35 executeErr error 36 ) 37 38 BeforeEach(func() { 39 actor, fakeCloudControllerClient, fakeConfig, _, _, _, _ = NewTestActor() 40 41 fakeConfig.TargetReturns("api.com") 42 43 mockResponseBody = []byte(`{"response":"yep"}`) 44 mockHTTPResponse = &http.Response{} 45 mockErr = nil 46 47 method = "" 48 path = "/v3/is/great" 49 customHeaders = []string{} 50 data = "" 51 failOnHTTPError = false 52 }) 53 54 JustBeforeEach(func() { 55 fakeCloudControllerClient.MakeRequestSendReceiveRawReturns( 56 mockResponseBody, 57 mockHTTPResponse, 58 mockErr, 59 ) 60 61 responseBody, httpResponse, executeErr = actor.MakeCurlRequest(method, path, customHeaders, data, failOnHTTPError) 62 }) 63 64 When("no method is given", func() { 65 It("makes the request", func() { 66 Expect(executeErr).NotTo(HaveOccurred()) 67 Expect(fakeCloudControllerClient.MakeRequestSendReceiveRawCallCount()).To(Equal(1)) 68 69 givenMethod, givenURL, givenHeaders, givenBody := fakeCloudControllerClient.MakeRequestSendReceiveRawArgsForCall(0) 70 Expect(givenMethod).To(Equal("")) 71 Expect(givenURL).To(Equal("api.com/v3/is/great")) 72 Expect(givenHeaders).To(Equal(http.Header{})) 73 Expect(givenBody).To(Equal([]byte(""))) 74 75 Expect(responseBody).To(Equal(mockResponseBody)) 76 Expect(httpResponse).To(Equal(mockHTTPResponse)) 77 }) 78 }) 79 80 When("method and data are given", func() { 81 BeforeEach(func() { 82 method = "PATCH" 83 data = `{"name": "cool"}` 84 }) 85 86 It("uses the given method", func() { 87 Expect(executeErr).NotTo(HaveOccurred()) 88 Expect(fakeCloudControllerClient.MakeRequestSendReceiveRawCallCount()).To(Equal(1)) 89 90 givenMethod, givenURL, givenHeaders, givenBody := fakeCloudControllerClient.MakeRequestSendReceiveRawArgsForCall(0) 91 Expect(givenMethod).To(Equal("PATCH")) 92 Expect(givenURL).To(Equal("api.com/v3/is/great")) 93 Expect(givenHeaders).To(Equal(http.Header{})) 94 Expect(givenBody).To(Equal([]byte(`{"name": "cool"}`))) 95 96 Expect(responseBody).To(Equal(mockResponseBody)) 97 Expect(httpResponse).To(Equal(mockHTTPResponse)) 98 }) 99 }) 100 101 When("data is given, but no method", func() { 102 BeforeEach(func() { 103 data = `{"name": "cool"}` 104 }) 105 106 It("uses method POST", func() { 107 Expect(executeErr).NotTo(HaveOccurred()) 108 Expect(fakeCloudControllerClient.MakeRequestSendReceiveRawCallCount()).To(Equal(1)) 109 110 givenMethod, givenURL, givenHeaders, givenBody := fakeCloudControllerClient.MakeRequestSendReceiveRawArgsForCall(0) 111 Expect(givenMethod).To(Equal("POST")) 112 Expect(givenURL).To(Equal("api.com/v3/is/great")) 113 Expect(givenHeaders).To(Equal(http.Header{})) 114 Expect(givenBody).To(Equal([]byte(`{"name": "cool"}`))) 115 116 Expect(responseBody).To(Equal(mockResponseBody)) 117 Expect(httpResponse).To(Equal(mockHTTPResponse)) 118 }) 119 }) 120 121 When("data is a path to a file", func() { 122 When("the file exists", func() { 123 var tempFile string 124 125 BeforeEach(func() { 126 file, err := ioutil.TempFile("", "") 127 Expect(err).NotTo(HaveOccurred()) 128 tempFile = file.Name() 129 130 _, err = file.WriteString(`{"file": "wow"}`) 131 Expect(err).NotTo(HaveOccurred()) 132 133 data = "@" + tempFile 134 }) 135 136 AfterEach(func() { 137 os.RemoveAll(tempFile) 138 }) 139 140 It("reads the file and uses the contents as the request body", func() { 141 Expect(executeErr).NotTo(HaveOccurred()) 142 Expect(fakeCloudControllerClient.MakeRequestSendReceiveRawCallCount()).To(Equal(1)) 143 144 givenMethod, givenURL, givenHeaders, givenBody := fakeCloudControllerClient.MakeRequestSendReceiveRawArgsForCall(0) 145 Expect(givenMethod).To(Equal("POST")) 146 Expect(givenURL).To(Equal("api.com/v3/is/great")) 147 Expect(givenHeaders).To(Equal(http.Header{})) 148 Expect(givenBody).To(Equal([]byte(`{"file": "wow"}`))) 149 150 Expect(responseBody).To(Equal(mockResponseBody)) 151 Expect(httpResponse).To(Equal(mockHTTPResponse)) 152 }) 153 }) 154 155 When("the file exists, but the given path has extra quotes", func() { 156 var tempFile string 157 158 BeforeEach(func() { 159 file, err := ioutil.TempFile("", "") 160 Expect(err).NotTo(HaveOccurred()) 161 tempFile = file.Name() 162 163 _, err = file.WriteString(`{"file": "wow"}`) 164 Expect(err).NotTo(HaveOccurred()) 165 166 data = `'@"` + tempFile + `"'` 167 }) 168 169 AfterEach(func() { 170 os.RemoveAll(tempFile) 171 }) 172 173 It("reads the file and uses the contents as the request body", func() { 174 Expect(executeErr).NotTo(HaveOccurred()) 175 Expect(fakeCloudControllerClient.MakeRequestSendReceiveRawCallCount()).To(Equal(1)) 176 177 givenMethod, givenURL, givenHeaders, givenBody := fakeCloudControllerClient.MakeRequestSendReceiveRawArgsForCall(0) 178 Expect(givenMethod).To(Equal("POST")) 179 Expect(givenURL).To(Equal("api.com/v3/is/great")) 180 Expect(givenHeaders).To(Equal(http.Header{})) 181 Expect(givenBody).To(Equal([]byte(`{"file": "wow"}`))) 182 183 Expect(responseBody).To(Equal(mockResponseBody)) 184 Expect(httpResponse).To(Equal(mockHTTPResponse)) 185 }) 186 }) 187 188 When("the file doesn't exist", func() { 189 BeforeEach(func() { 190 data = "@/not/real" 191 }) 192 193 It("returns a helpful error", func() { 194 Expect(executeErr.Error()).To(MatchRegexp("Error creating request")) 195 }) 196 }) 197 }) 198 199 When("valid headers are given", func() { 200 BeforeEach(func() { 201 customHeaders = append(customHeaders, "X-Wow: Amazing") 202 customHeaders = append(customHeaders, "X-Wow: Cool") 203 customHeaders = append(customHeaders, "X-Great: Yeah") 204 }) 205 206 It("uses those headers for the request", func() { 207 Expect(executeErr).NotTo(HaveOccurred()) 208 Expect(fakeCloudControllerClient.MakeRequestSendReceiveRawCallCount()).To(Equal(1)) 209 210 givenMethod, givenURL, givenHeaders, givenBody := fakeCloudControllerClient.MakeRequestSendReceiveRawArgsForCall(0) 211 Expect(givenMethod).To(Equal("")) 212 Expect(givenURL).To(Equal("api.com/v3/is/great")) 213 Expect(givenHeaders).To(Equal(http.Header{ 214 "X-Wow": {"Amazing", "Cool"}, 215 "X-Great": {"Yeah"}, 216 })) 217 Expect(givenBody).To(Equal([]byte(""))) 218 }) 219 }) 220 221 When("invalid headers are given", func() { 222 BeforeEach(func() { 223 customHeaders = append(customHeaders, "notformattedcorrectly") 224 }) 225 226 It("returns a helpful error", func() { 227 Expect(executeErr.Error()).To(MatchRegexp("Error creating request")) 228 }) 229 }) 230 231 When("the request fails", func() { 232 BeforeEach(func() { 233 mockErr = errors.New("uh oh") 234 mockHTTPResponse.StatusCode = 500 235 }) 236 237 It("does not return the error by default", func() { 238 Expect(executeErr).NotTo(HaveOccurred()) 239 }) 240 241 When("the fail-on-http-errors flag is set", func() { 242 BeforeEach(func() { 243 failOnHTTPError = true 244 }) 245 246 It("returns an error containing the status code", func() { 247 Expect(executeErr).To(MatchError(translatableerror.CurlExit22Error{StatusCode: 500})) 248 }) 249 }) 250 }) 251 }) 252 })