From ea45136c3d2be5cc8ce70299a9ad59021661c1f9 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Date: Wed, 19 Feb 2020 19:26:47 +0100 Subject: [PATCH] feat(go): add download file function (#1200) * add download file function * add test case * Update pkg/piperutils/FileUtils.go * correct test case * remove FileUtils.Download * add Downloader * add Downloader * fix error * respect header and cookies * add test case * rename files * correct test case * remove SendRequest * correct test case --- pkg/http/downloader.go | 41 ++++++++++++++++++++++++++++++ pkg/http/downloader_test.go | 43 ++++++++++++++++++++++++++++++++ pkg/piperutils/fileUtils_test.go | 8 +++--- 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 pkg/http/downloader.go create mode 100644 pkg/http/downloader_test.go diff --git a/pkg/http/downloader.go b/pkg/http/downloader.go new file mode 100644 index 000000000..8db92f82d --- /dev/null +++ b/pkg/http/downloader.go @@ -0,0 +1,41 @@ +package http + +import ( + "io" + "net/http" + "os" + + "github.com/pkg/errors" +) + +//Downloader ... +type Downloader interface { + SetOptions(options ClientOptions) + DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error +} + +// DownloadFile downloads a file's content as GET request from the specified URL to the specified file +func (c *Client) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error { + return c.DownloadRequest(http.MethodPost, url, filename, header, cookies) +} + +// DownloadRequest ... +func (c *Client) DownloadRequest(method, url, filename string, header http.Header, cookies []*http.Cookie) error { + response, err := c.SendRequest(method, url, nil, header, cookies) + if err != nil { + return errors.Wrapf(err, "HTTP %v request to %v failed with error", method, url) + } + defer response.Body.Close() + + fileHandler, err := os.Create(filename) + if err != nil { + return errors.Wrapf(err, "unable to create file %v", filename) + } + defer fileHandler.Close() + + _, err = io.Copy(fileHandler, response.Body) + if err != nil { + return errors.Wrapf(err, "unable to copy content from url to file %v", filename) + } + return err +} diff --git a/pkg/http/downloader_test.go b/pkg/http/downloader_test.go new file mode 100644 index 000000000..af13b7878 --- /dev/null +++ b/pkg/http/downloader_test.go @@ -0,0 +1,43 @@ +package http + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/SAP/jenkins-library/pkg/log" + "github.com/stretchr/testify/assert" +) + +func TestDownloadRequest(t *testing.T) { + // Start a local HTTP server + server := httptest.NewServer( + http.HandlerFunc( + func(rw http.ResponseWriter, req *http.Request) { rw.Write([]byte("my fancy file content")) })) + // Close the server when test finishes + defer server.Close() + + client := Client{ + logger: log.Entry().WithField("package", "SAP/jenkins-library/pkg/http"), + } + + workingDir, err := ioutil.TempDir("", "test detailed results") + if err != nil { + t.Fatal("Failed to create temporary directory") + } + // clean up tmp dir + defer os.RemoveAll(workingDir) + targetFile := filepath.Join(workingDir, "abc.xml") + + // function under test + err = client.DownloadFile(server.URL, targetFile, nil, nil) + // asserts + assert.NoError(t, err, "Error occured but none expected") + assert.FileExists(t, targetFile, "File not found") + bytes, err := ioutil.ReadFile(targetFile) + assert.NoError(t, err, "Error occured but none expected") + assert.Equal(t, "my fancy file content", string(bytes)) +} diff --git a/pkg/piperutils/fileUtils_test.go b/pkg/piperutils/fileUtils_test.go index 53f493531..8b971bb67 100644 --- a/pkg/piperutils/fileUtils_test.go +++ b/pkg/piperutils/fileUtils_test.go @@ -11,9 +11,9 @@ import ( func TestFileExists(t *testing.T) { dir, err := ioutil.TempDir("", "dir") if err != nil { - t.Fatal("Failed to create temporary workspace directory") + t.Fatal("Failed to create temporary workspace directory") } - // clean up tmp dir + // clean up tmp dir defer os.RemoveAll(dir) result, err := FileExists(dir) @@ -32,9 +32,9 @@ func TestCopy(t *testing.T) { file := filepath.Join(dir, "testFile") err = ioutil.WriteFile(file, []byte{byte(1), byte(2), byte(3)}, 0700) if err != nil { - t.Fatal("Failed to create temporary workspace directory") + t.Fatal("Failed to create temporary workspace directory") } - // clean up tmp dir + // clean up tmp dir defer os.RemoveAll(dir) result, err := Copy(file, filepath.Join(dir, "testFile2"))