github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/downloader/file_download_test.go (about)

     1  package downloader_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"os"
     7  	"path"
     8  
     9  	"code.cloudfoundry.org/cli/cf/util/downloader"
    10  
    11  	"github.com/onsi/gomega/ghttp"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Downloader", func() {
    18  	var (
    19  		d       downloader.Downloader
    20  		tempDir string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		var err error
    25  		tempDir, err = ioutil.TempDir("", "file-download-test")
    26  		Expect(err).NotTo(HaveOccurred())
    27  		d = downloader.NewDownloader(tempDir)
    28  	})
    29  
    30  	AfterEach(func() {
    31  		os.RemoveAll(tempDir)
    32  	})
    33  
    34  	Describe("DownloadFile", func() {
    35  		var server *ghttp.Server
    36  
    37  		BeforeEach(func() {
    38  			server = ghttp.NewServer()
    39  		})
    40  
    41  		AfterEach(func() {
    42  			server.Close()
    43  
    44  			err := d.RemoveFile()
    45  			Expect(err).NotTo(HaveOccurred())
    46  		})
    47  
    48  		Context("when the server responds with the file", func() {
    49  			BeforeEach(func() {
    50  				server.AppendHandlers(
    51  					ghttp.CombineHandlers(
    52  						ghttp.VerifyRequest("GET", "/abc.zip"),
    53  						ghttp.RespondWith(http.StatusOK, "abc123"),
    54  					),
    55  				)
    56  			})
    57  
    58  			It("saves file with name found in URL in provided dir", func() {
    59  				_, _, err := d.DownloadFile(server.URL() + "/abc.zip")
    60  				Expect(err).NotTo(HaveOccurred())
    61  
    62  				_, err = os.Stat(path.Join(tempDir, "abc.zip"))
    63  				Expect(err).NotTo(HaveOccurred())
    64  			})
    65  
    66  			It("returns the number of bytes written to the file", func() {
    67  				n, _, err := d.DownloadFile(server.URL() + "/abc.zip")
    68  				Expect(err).NotTo(HaveOccurred())
    69  				Expect(n).To(Equal(int64(len("abc123"))))
    70  			})
    71  
    72  			It("returns the name of the file that was downloaded", func() {
    73  				_, name, err := d.DownloadFile(server.URL() + "/abc.zip")
    74  				Expect(err).NotTo(HaveOccurred())
    75  				Expect(name).To(Equal("abc.zip"))
    76  			})
    77  		})
    78  
    79  		Context("when the server responds with the filename in the header", func() {
    80  			BeforeEach(func() {
    81  				server.AppendHandlers(
    82  					ghttp.CombineHandlers(
    83  						ghttp.VerifyRequest("GET", "/abc.zip"),
    84  						ghttp.RespondWith(http.StatusOK, "abc123", http.Header{
    85  							"Content-Disposition": []string{"attachment; filename=header.zip"},
    86  						}),
    87  					),
    88  				)
    89  			})
    90  
    91  			It("downloads the file named in the header to the provided dir, and trims spaces appropriately", func() {
    92  				_, _, err := d.DownloadFile(server.URL() + "/abc.zip")
    93  				Expect(err).NotTo(HaveOccurred())
    94  
    95  				_, err = os.Stat(path.Join(tempDir, "header.zip"))
    96  				Expect(err).NotTo(HaveOccurred())
    97  
    98  				_, err = os.Stat(path.Join(tempDir, "abc.zip"))
    99  				Expect(err).To(HaveOccurred())
   100  			})
   101  
   102  			It("returns the number of bytes written to the file", func() {
   103  				n, _, err := d.DownloadFile(server.URL() + "/abc.zip")
   104  				Expect(err).NotTo(HaveOccurred())
   105  				Expect(n).To(Equal(int64(len("abc123"))))
   106  			})
   107  
   108  			It("returns the name of the file that was downloaded", func() {
   109  				_, name, err := d.DownloadFile(server.URL() + "/abc.zip")
   110  				Expect(err).NotTo(HaveOccurred())
   111  				Expect(name).To(Equal("header.zip"))
   112  			})
   113  		})
   114  
   115  		Context("when the server returns a redirect to a file", func() {
   116  			BeforeEach(func() {
   117  				server.AppendHandlers(
   118  					ghttp.CombineHandlers(
   119  						ghttp.VerifyRequest("GET", "/abc.zip"),
   120  						ghttp.RespondWith(http.StatusFound, "", http.Header{
   121  							"Location": []string{server.URL() + "/redirect.zip"},
   122  						}),
   123  					),
   124  					ghttp.CombineHandlers(
   125  						ghttp.VerifyRequest("GET", "/redirect.zip"),
   126  						ghttp.RespondWith(http.StatusOK, "abc123"),
   127  					),
   128  				)
   129  			})
   130  
   131  			It("downloads the file from the redirect to the provided dir", func() {
   132  				_, _, err := d.DownloadFile(server.URL() + "/abc.zip")
   133  				Expect(err).NotTo(HaveOccurred())
   134  
   135  				_, err = os.Stat(path.Join(tempDir, "redirect.zip"))
   136  				Expect(err).NotTo(HaveOccurred())
   137  
   138  				_, err = os.Stat(path.Join(tempDir, "abc.zip"))
   139  				Expect(err).To(HaveOccurred())
   140  			})
   141  
   142  			It("returns the number of bytes written to the file", func() {
   143  				n, _, err := d.DownloadFile(server.URL() + "/abc.zip")
   144  				Expect(err).NotTo(HaveOccurred())
   145  				Expect(n).To(Equal(int64(len("abc123"))))
   146  			})
   147  
   148  			It("returns the name of the file that was downloaded", func() {
   149  				_, name, err := d.DownloadFile(server.URL() + "/abc.zip")
   150  				Expect(err).NotTo(HaveOccurred())
   151  				Expect(name).To(Equal("redirect.zip"))
   152  			})
   153  		})
   154  
   155  		Context("when the URL is invalid", func() {
   156  			It("returns an error", func() {
   157  				_, _, err := d.DownloadFile("http://going.nowwhere/abc.zip")
   158  				Expect(err).To(HaveOccurred())
   159  			})
   160  		})
   161  	})
   162  
   163  	Describe("RemoveFile", func() {
   164  		var server *ghttp.Server
   165  
   166  		BeforeEach(func() {
   167  			server = ghttp.NewServer()
   168  			server.AppendHandlers(
   169  				ghttp.CombineHandlers(
   170  					ghttp.VerifyRequest("GET", "/abc.zip"),
   171  					ghttp.RespondWith(http.StatusOK, "abc123"),
   172  				),
   173  			)
   174  		})
   175  
   176  		AfterEach(func() {
   177  			server.Close()
   178  		})
   179  
   180  		Context("when a file has been downloaded", func() {
   181  			BeforeEach(func() {
   182  				_, _, err := d.DownloadFile(server.URL() + "/abc.zip")
   183  				Expect(err).NotTo(HaveOccurred())
   184  			})
   185  
   186  			It("removes the downloaded file", func() {
   187  				_, err := os.Stat(path.Join(tempDir, "abc.zip"))
   188  				Expect(err).NotTo(HaveOccurred())
   189  
   190  				err = d.RemoveFile()
   191  				Expect(err).NotTo(HaveOccurred())
   192  
   193  				_, err = os.Stat(path.Join(tempDir, "abc.zip"))
   194  				Expect(err).To(HaveOccurred())
   195  			})
   196  		})
   197  
   198  		It("does not return an error when a file has not been downloaded", func() {
   199  			err := d.RemoveFile()
   200  			Expect(err).NotTo(HaveOccurred())
   201  		})
   202  	})
   203  })