2020-02-19 20:26:47 +02:00
|
|
|
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)
|
2020-02-25 17:52:12 +02:00
|
|
|
targetFile := filepath.Join(workingDir, "abc/123/abc.xml")
|
2020-02-19 20:26:47 +02:00
|
|
|
|
|
|
|
// function under test
|
|
|
|
err = client.DownloadFile(server.URL, targetFile, nil, nil)
|
|
|
|
// asserts
|
2020-09-24 07:41:06 +02:00
|
|
|
assert.NoError(t, err, "Error occurred but none expected")
|
2020-02-19 20:26:47 +02:00
|
|
|
assert.FileExists(t, targetFile, "File not found")
|
|
|
|
bytes, err := ioutil.ReadFile(targetFile)
|
2020-09-24 07:41:06 +02:00
|
|
|
assert.NoError(t, err, "Error occurred but none expected")
|
2020-02-19 20:26:47 +02:00
|
|
|
assert.Equal(t, "my fancy file content", string(bytes))
|
|
|
|
}
|