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

     1  package download_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"strings"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  
    13  	. "code.cloudfoundry.org/cli/util/download"
    14  	"code.cloudfoundry.org/cli/util/download/downloadfakes"
    15  )
    16  
    17  var _ = Describe("Downloader", func() {
    18  	var (
    19  		fakeHTTPClient *downloadfakes.FakeHTTPClient
    20  		downloader     *Downloader
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeHTTPClient = new(downloadfakes.FakeHTTPClient)
    25  		downloader = &Downloader{
    26  			HTTPClient: fakeHTTPClient,
    27  		}
    28  	})
    29  
    30  	Describe("Download", func() {
    31  		var (
    32  			url        string
    33  			tmpDirPath string
    34  			file       string
    35  			executeErr error
    36  		)
    37  
    38  		BeforeEach(func() {
    39  			url = "https://some.url"
    40  
    41  			var err error
    42  			tmpDirPath, err = ioutil.TempDir("", "bpdir-")
    43  			Expect(err).ToNot(HaveOccurred())
    44  		})
    45  
    46  		AfterEach(func() {
    47  			Expect(os.RemoveAll(tmpDirPath)).ToNot(HaveOccurred())
    48  		})
    49  
    50  		JustBeforeEach(func() {
    51  			file, executeErr = downloader.Download(url, tmpDirPath)
    52  		})
    53  
    54  		When("the download is successful", func() {
    55  			var responseBody string
    56  
    57  			BeforeEach(func() {
    58  				responseBody = "some response body"
    59  				response := &http.Response{
    60  					Body:          ioutil.NopCloser(strings.NewReader(responseBody)),
    61  					ContentLength: int64(len(responseBody)),
    62  					StatusCode:    http.StatusOK,
    63  				}
    64  				fakeHTTPClient.GetReturns(response, nil)
    65  			})
    66  
    67  			It("returns correct path to the downloaded file", func() {
    68  				Expect(executeErr).ToNot(HaveOccurred())
    69  
    70  				raw, err := ioutil.ReadFile(file)
    71  				Expect(err).ToNot(HaveOccurred())
    72  				Expect(string(raw)).To(Equal(responseBody))
    73  			})
    74  
    75  			It("downloads the file from the provided URL", func() {
    76  				Expect(executeErr).ToNot(HaveOccurred())
    77  
    78  				Expect(fakeHTTPClient.GetCallCount()).To(Equal(1))
    79  				Expect(fakeHTTPClient.GetArgsForCall(0)).To(Equal(url))
    80  			})
    81  		})
    82  
    83  		When("the client returns an error", func() {
    84  			BeforeEach(func() {
    85  				fakeHTTPClient.GetReturns(nil, errors.New("stop all the downloading"))
    86  			})
    87  
    88  			It("returns the error", func() {
    89  				Expect(executeErr).To(MatchError("stop all the downloading"))
    90  			})
    91  		})
    92  
    93  		When("HTTP request returns 4xx or 5xx error", func() {
    94  			var responseBody string
    95  
    96  			BeforeEach(func() {
    97  				responseBody = "not found"
    98  				response := &http.Response{
    99  					Body:       ioutil.NopCloser(strings.NewReader(responseBody)),
   100  					StatusCode: http.StatusNotFound,
   101  					Status:     "404 Not Found",
   102  				}
   103  				fakeHTTPClient.GetReturns(response, nil)
   104  			})
   105  
   106  			It("returns an error", func() {
   107  				Expect(executeErr).To(MatchError(RawHTTPStatusError{Status: "404 Not Found", RawResponse: []byte(responseBody)}))
   108  			})
   109  		})
   110  	})
   111  })