github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/curl_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "net/http" 7 "os" 8 9 "github.com/LukasHeimann/cloudfoundrycli/v8/command/flag" 10 v7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 11 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 12 13 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 14 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("curl Command", func() { 21 var ( 22 cmd v7.CurlCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v7fakes.FakeActor 27 binaryName string 28 29 CustomHeaders []string 30 HTTPMethod string 31 HTTPData flag.PathWithAt 32 FailOnHTTPError bool 33 IncludeReponseHeaders bool 34 OutputFile flag.Path 35 executeErr error 36 ) 37 38 BeforeEach(func() { 39 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 40 fakeConfig = new(commandfakes.FakeConfig) 41 fakeSharedActor = new(commandfakes.FakeSharedActor) 42 fakeActor = new(v7fakes.FakeActor) 43 44 CustomHeaders = []string{"/v3/apps"} 45 cmd = v7.CurlCommand{ 46 BaseCommand: v7.BaseCommand{ 47 UI: testUI, 48 Config: fakeConfig, 49 SharedActor: fakeSharedActor, 50 Actor: fakeActor, 51 }, 52 RequiredArgs: flag.APIPath{Path: "/"}, 53 CustomHeaders: CustomHeaders, 54 HTTPMethod: HTTPMethod, 55 HTTPData: HTTPData, 56 FailOnHTTPError: FailOnHTTPError, 57 IncludeReponseHeaders: IncludeReponseHeaders, 58 OutputFile: OutputFile, 59 } 60 61 binaryName = "faceman" 62 fakeConfig.BinaryNameReturns(binaryName) 63 fakeActor.MakeCurlRequestReturns([]byte("sarah, teal, and reid were here"), &http.Response{ 64 Header: http.Header{ 65 "X-Name": []string{"athleisure"}, 66 }, 67 }, nil) 68 }) 69 70 JustBeforeEach(func() { 71 executeErr = cmd.Execute(nil) 72 }) 73 74 It("makes a request with the given flags", func() { 75 Expect(fakeActor.MakeCurlRequestCallCount()).To(Equal(1)) 76 httpMethod, path, customHeaders, httpData, failOnHTTPError := fakeActor.MakeCurlRequestArgsForCall(0) 77 Expect(httpMethod).To(Equal(HTTPMethod)) 78 Expect(path).To(Equal(cmd.RequiredArgs.Path)) 79 Expect(customHeaders).To(Equal(CustomHeaders)) 80 Expect(httpData).To(Equal(string(HTTPData))) 81 Expect(failOnHTTPError).To(Equal(FailOnHTTPError)) 82 Expect(executeErr).ToNot(HaveOccurred()) 83 }) 84 85 When("the verbose flag is set", func() { 86 BeforeEach(func() { 87 fakeConfig.VerboseReturns(true, nil) 88 }) 89 90 It("does not write any additional output", func() { 91 Expect(executeErr).NotTo(HaveOccurred()) 92 Expect(testUI.Out).NotTo(Say(".+")) 93 }) 94 }) 95 96 It("writes the response to stdout", func() { 97 Expect(executeErr).NotTo(HaveOccurred()) 98 Expect(testUI.Out).To(Say("sarah, teal, and reid were here")) 99 }) 100 101 When("the request errors", func() { 102 BeforeEach(func() { 103 fakeActor.MakeCurlRequestReturns([]byte{}, &http.Response{}, errors.New("this sucks")) 104 }) 105 106 It("returns the error", func() { 107 Expect(executeErr).To(MatchError("this sucks")) 108 }) 109 }) 110 111 When("an output file is given", func() { 112 BeforeEach(func() { 113 outputFile, err := ioutil.TempFile("", "") 114 Expect(err).NotTo(HaveOccurred()) 115 outputFileName := outputFile.Name() 116 cmd.OutputFile = flag.Path(outputFileName) 117 }) 118 119 AfterEach(func() { 120 os.RemoveAll(string(cmd.OutputFile)) 121 }) 122 123 It("writes the output to the file", func() { 124 fileContents, err := ioutil.ReadFile(string(cmd.OutputFile)) 125 Expect(string(fileContents)).To(Equal("sarah, teal, and reid were here")) 126 Expect(err).ToNot(HaveOccurred()) 127 }) 128 129 When("the include-response-headers flag is set", func() { 130 BeforeEach(func() { 131 cmd.IncludeReponseHeaders = true 132 }) 133 134 It("includes the headers in the output", func() { 135 fileContents, err := ioutil.ReadFile(string(cmd.OutputFile)) 136 Expect(string(fileContents)).To(ContainSubstring("X-Name: athleisure")) 137 Expect(string(fileContents)).To(ContainSubstring("sarah, teal, and reid were here")) 138 Expect(err).ToNot(HaveOccurred()) 139 }) 140 }) 141 142 When("writing the file fails", func() { 143 BeforeEach(func() { 144 cmd.OutputFile = flag.Path("/🕵/sleuthin/for/a/file") 145 }) 146 147 It("returns the error", func() { 148 Expect(executeErr.Error()).To(ContainSubstring("Error creating file")) 149 }) 150 }) 151 }) 152 153 })