github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/plugin/download_plugin_test.go (about) 1 package plugin_test 2 3 import ( 4 "io" 5 "io/ioutil" 6 "net/http" 7 "net/url" 8 "os" 9 10 . "code.cloudfoundry.org/cli/api/plugin" 11 "code.cloudfoundry.org/cli/api/plugin/pluginerror" 12 "code.cloudfoundry.org/cli/api/plugin/pluginfakes" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/ghttp" 16 ) 17 18 var _ = Describe("DownloadPlugin", func() { 19 var ( 20 client *Client 21 tempPath string 22 ) 23 24 BeforeEach(func() { 25 client = NewTestClient() 26 27 tempFile, err := ioutil.TempFile("", "") 28 Expect(err).NotTo(HaveOccurred()) 29 tempPath = tempFile.Name() 30 31 err = tempFile.Close() 32 Expect(err).NotTo(HaveOccurred()) 33 }) 34 35 AfterEach(func() { 36 err := os.Remove(tempPath) 37 Expect(err).NotTo(HaveOccurred()) 38 }) 39 40 Context("when there are no errors", func() { 41 var ( 42 data []byte 43 ) 44 45 BeforeEach(func() { 46 data = []byte("some test data") 47 server.AppendHandlers( 48 CombineHandlers( 49 VerifyRequest(http.MethodGet, "/"), 50 RespondWith(http.StatusOK, data), 51 ), 52 ) 53 }) 54 55 It("downloads the plugin, and writes the plugin file to the specified path", func() { 56 fakeProxyReader := new(pluginfakes.FakeProxyReader) 57 58 fakeProxyReader.WrapStub = func(reader io.Reader) io.ReadCloser { 59 return ioutil.NopCloser(reader) 60 } 61 err := client.DownloadPlugin(server.URL(), tempPath, fakeProxyReader) 62 Expect(err).ToNot(HaveOccurred()) 63 64 fileData, err := ioutil.ReadFile(tempPath) 65 Expect(err).ToNot(HaveOccurred()) 66 Expect(fileData).To(Equal(data)) 67 68 Expect(fakeProxyReader.WrapCallCount()).To(Equal(1)) 69 }) 70 }) 71 72 Context("when the URL is invalid", func() { 73 It("returns an URL error", func() { 74 err := client.DownloadPlugin("://", tempPath, nil) 75 _, isURLError := err.(*url.Error) 76 Expect(isURLError).To(BeTrue()) 77 }) 78 }) 79 80 Context("when downloading the plugin errors", func() { 81 BeforeEach(func() { 82 server.AppendHandlers( 83 CombineHandlers( 84 VerifyRequest(http.MethodGet, "/"), 85 RespondWith(http.StatusTeapot, nil), 86 ), 87 ) 88 }) 89 90 It("returns a RawHTTPStatusError", func() { 91 err := client.DownloadPlugin(server.URL(), tempPath, nil) 92 Expect(err).To(MatchError(pluginerror.RawHTTPStatusError{Status: "418 I'm a teapot", RawResponse: []byte("")})) 93 }) 94 }) 95 96 Context("when the path is not writeable", func() { 97 BeforeEach(func() { 98 server.AppendHandlers( 99 CombineHandlers( 100 VerifyRequest(http.MethodGet, "/"), 101 RespondWith(http.StatusOK, nil), 102 ), 103 ) 104 }) 105 106 It("returns some error", func() { 107 err := client.DownloadPlugin(server.URL(), "/a/path/that/does/not/exist", nil) 108 _, isPathError := err.(*os.PathError) 109 Expect(isPathError).To(BeTrue()) 110 }) 111 }) 112 })