mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
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
This commit is contained in:
parent
fa4938e590
commit
ea45136c3d
41
pkg/http/downloader.go
Normal file
41
pkg/http/downloader.go
Normal file
@ -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
|
||||
}
|
43
pkg/http/downloader_test.go
Normal file
43
pkg/http/downloader_test.go
Normal file
@ -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))
|
||||
}
|
@ -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"))
|
||||
|
Loading…
Reference in New Issue
Block a user