mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [netutil] reject failed download responses (#388)
This commit is contained in:
1 parent
be9b167a9b
commit
f1db13387b
2 files changed
+43
No files matched your search
@@ -0,0 +1,40 @@
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDownloadFileStatus(t *testing.T) {
|
||||
for _, status := range []int{http.StatusOK, http.StatusNotFound, http.StatusInternalServerError} {
|
||||
t.Run(http.StatusText(status), func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte("response"))
|
||||
}))
|
||||
defer server.Close()
|
||||
path := filepath.Join(t.TempDir(), "download.txt")
|
||||
if err := os.WriteFile(path, []byte("original"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := DownloadFile(path, server.URL)
|
||||
if (err != nil) != (status != http.StatusOK) {
|
||||
t.Errorf("unexpected result: %v", err)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "original"
|
||||
if status == http.StatusOK {
|
||||
want = "response"
|
||||
}
|
||||
if string(data) != want {
|
||||
t.Errorf("got %q, want %q", data, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user