-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patht.download_test.go
105 lines (88 loc) · 2.97 KB
/
t.download_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package docxplate
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
type errorTransport struct{}
func (et *errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return nil, errors.New("transport error")
}
func TestDownloadFileInvalidCases(t *testing.T) {
t.Run("invalid URL", func(t *testing.T) {
tmpFpath, err := DefaultDownloader.DownloadFile(context.Background(), "::invalid-url")
if err == nil {
t.Fatalf("Expected an error, but got nil")
}
_ = os.Remove(tmpFpath)
})
t.Run("transport error", func(t *testing.T) {
// Save original transport
originalTransport := http.DefaultTransport
// Set custom error transport
http.DefaultTransport = &errorTransport{}
// Restore original transport after the test
defer func() {
http.DefaultTransport = originalTransport
}()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer server.Close()
tmpFpath, err := DefaultDownloader.DownloadFile(context.Background(), server.URL)
// fmt.Println("remove tmp file", tmpFpath, server.URL)
if err == nil {
t.Fatalf("Expected an error, but got nil")
}
_ = os.Remove(tmpFpath)
})
t.Run("non-200 status code", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
tmpFpath, err := DefaultDownloader.DownloadFile(context.Background(), server.URL)
if !errors.Is(err, http.ErrMissingFile) {
t.Fatalf("Expected http.ErrMissingFile, but got: %v", err)
}
_ = os.Remove(tmpFpath)
})
t.Run("server read error", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "2")
_, _ = io.WriteString(w, "1") // #nosec G104
}))
defer server.Close()
tmpFpath, err := DefaultDownloader.DownloadFile(context.Background(), server.URL)
if err == nil {
t.Fatalf("Expected an error, but got nil")
}
_ = os.Remove(tmpFpath)
})
//
// t.Run("create temp file error", func(t *testing.T) {
// server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// io.WriteString(w, "test data")
// }))
// defer server.Close()
// // Create a temporary directory with no write permissions
// tempDir, err := os.MkdirTemp("", "downloadFileTest")
// if err != nil {
// t.Fatalf("Failed to create temporary directory: %v", err)
// }
// defer os.RemoveAll(tempDir)
// if err := os.Chmod(tempDir, 0555); err != nil {
// t.Fatalf("Failed to set permissions on temporary directory: %v", err)
// }
// // Temporarily replace os.TempDir function
// originalTempDirFunc := os.TempDir
// os.TempDir = func() string { return tempDir }
// defer func() { os.TempDir = originalTempDirFunc }()
// _, err = downloadFile(server.URL)
// if err == nil {
// t.Fatalf("Expected an error, but got nil")
// }
// })
}