-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,56 @@ | ||
package cwhub | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/crowdsecurity/go-cs-lib/cstest" | ||
) | ||
|
||
func TestDownloadFile(t *testing.T) { | ||
examplePath := "./example.txt" | ||
defer os.Remove(examplePath) | ||
|
||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
// OK | ||
httpmock.RegisterResponder( | ||
"GET", | ||
"https://example.com/xx", | ||
httpmock.NewStringResponder(200, "example content oneoneone"), | ||
) | ||
|
||
httpmock.RegisterResponder( | ||
"GET", | ||
"https://example.com/x", | ||
httpmock.NewStringResponder(404, "not found"), | ||
) | ||
|
||
err := downloadFile("https://example.com/xx", examplePath) | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case "/xx": | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = io.WriteString(w, "example content oneoneone") | ||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
_, _ = io.WriteString(w, "not found") | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
dest := filepath.Join(t.TempDir(), "example.txt") | ||
defer os.Remove(dest) | ||
|
||
err := downloadFile(ts.URL+"/xx", dest) | ||
require.NoError(t, err) | ||
|
||
content, err := os.ReadFile(examplePath) | ||
content, err := os.ReadFile(dest) | ||
assert.Equal(t, "example content oneoneone", string(content)) | ||
require.NoError(t, err) | ||
|
||
// bad uri | ||
err = downloadFile("https://zz.com", examplePath) | ||
require.Error(t, err) | ||
err = downloadFile("https://zz.com", dest) | ||
cstest.RequireErrorContains(t, err, "lookup zz.com") | ||
cstest.RequireErrorContains(t, err, "no such host") | ||
|
||
// 404 | ||
err = downloadFile("https://example.com/x", examplePath) | ||
require.Error(t, err) | ||
err = downloadFile(ts.URL+"/x", dest) | ||
cstest.RequireErrorContains(t, err, "bad http code 404") | ||
|
||
// bad target | ||
err = downloadFile("https://example.com/xx", "") | ||
require.Error(t, err) | ||
err = downloadFile(ts.URL+"/xx", "") | ||
cstest.RequireErrorContains(t, err, "no such file or directory") | ||
|
||
// destination directory does not exist | ||
err = downloadFile(ts.URL+"/xx", filepath.Join(t.TempDir(), "missing/example.txt")) | ||
cstest.RequireErrorContains(t, err, "no such file or directory") | ||
} |