2016-07-07 02:07:55 +03:00
|
|
|
package download_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2016-08-01 23:44:25 +03:00
|
|
|
"github.com/Azure/custom-script-extension-linux/pkg/download"
|
2016-07-07 02:07:55 +03:00
|
|
|
"github.com/ahmetalpbalkan/go-httpbin"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2016-07-13 21:36:14 +03:00
|
|
|
type badDownloader struct{ calls int }
|
2016-07-07 02:07:55 +03:00
|
|
|
|
2016-07-13 21:36:14 +03:00
|
|
|
func (b *badDownloader) GetRequest() (*http.Request, error) {
|
|
|
|
b.calls++
|
2016-07-07 02:07:55 +03:00
|
|
|
return nil, errors.New("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownload_wrapsGetRequestError(t *testing.T) {
|
2019-07-25 03:57:36 +03:00
|
|
|
_, _, err := download.Download(new(badDownloader))
|
2016-07-07 02:07:55 +03:00
|
|
|
require.NotNil(t, err)
|
2016-07-13 00:48:11 +03:00
|
|
|
require.EqualError(t, err, "failed to create the request: expected error")
|
2016-07-07 02:07:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownload_wrapsHTTPError(t *testing.T) {
|
2019-07-25 03:57:36 +03:00
|
|
|
_, _, err := download.Download(download.NewURLDownload("bad url"))
|
2016-07-07 02:07:55 +03:00
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Contains(t, err.Error(), "http request failed:")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownload_badStatusCodeFails(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(httpbin.GetMux())
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
for _, code := range []int{
|
|
|
|
http.StatusNotFound,
|
|
|
|
http.StatusForbidden,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusBadGateway,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
} {
|
2019-07-25 03:57:36 +03:00
|
|
|
_, _, err := download.Download(download.NewURLDownload(fmt.Sprintf("%s/status/%d", srv.URL, code)))
|
2016-07-07 02:07:55 +03:00
|
|
|
require.NotNil(t, err, "not failed for code:%d", code)
|
|
|
|
require.Contains(t, err.Error(), "unexpected status code", "wrong message for code %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownload_statusOKSucceeds(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(httpbin.GetMux())
|
|
|
|
defer srv.Close()
|
|
|
|
|
2019-07-25 03:57:36 +03:00
|
|
|
_, body, err := download.Download(download.NewURLDownload(srv.URL + "/status/200"))
|
2016-07-07 02:07:55 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
defer body.Close()
|
|
|
|
require.NotNil(t, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownload_retrievesBody(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(httpbin.GetMux())
|
|
|
|
defer srv.Close()
|
|
|
|
|
2019-07-25 03:57:36 +03:00
|
|
|
_, body, err := download.Download(download.NewURLDownload(srv.URL + "/bytes/65536"))
|
2016-07-07 02:07:55 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
defer body.Close()
|
|
|
|
b, err := ioutil.ReadAll(body)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.EqualValues(t, 65536, len(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownload_bodyClosesWithoutError(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(httpbin.GetMux())
|
|
|
|
defer srv.Close()
|
|
|
|
|
2019-07-25 03:57:36 +03:00
|
|
|
_, body, err := download.Download(download.NewURLDownload(srv.URL + "/get"))
|
2016-07-07 02:07:55 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, body.Close(), "body should close fine")
|
|
|
|
}
|