-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_downloader_test.go
82 lines (69 loc) · 2.18 KB
/
image_downloader_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
package main
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestDownloadImage(t *testing.T) {
// Create a temporary directory for test downloads
downloadDir, err := ioutil.TempDir("/tmp", "test-*-downloads")
if err != nil {
t.Fatalf("Failed to create temporary download directory: %v", err)
}
defer os.RemoveAll(downloadDir)
// Set up a mock server to serve the image file
mockServer := createMockServer()
defer mockServer.Close()
// Create a mock image URL
imageURL := mockServer.URL + "/image.jpg"
// Download the image
err = downloadImage(imageURL, downloadDir)
if err != nil {
t.Fatalf("Failed to download image: %v", err)
}
// Verify that the downloaded file exists
downloadedFilePath := filepath.Join(downloadDir, "image.jpg")
_, err = os.Stat(downloadedFilePath)
if os.IsNotExist(err) {
t.Errorf("Downloaded file does not exist: %s", downloadedFilePath)
}
}
func createMockServer() *httptest.Server {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Serve a sample image file
imageFile := filepath.Join("/tmp", "sample_image.jpg")
_, err := os.OpenFile(imageFile, os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
return
}
http.ServeFile(w, r, imageFile)
}))
return mockServer
}
func createMockImageURLFile(mockURL string) string {
imageURLFile := filepath.Join("testdata", "image_urls.txt")
// Create a temporary copy of the mock image URL file
tempFile, err := ioutil.TempFile("", "mock-image-urls")
if err != nil {
log.Fatalf("Failed to create temporary image URL file: %v", err)
}
defer tempFile.Close()
// Read the contents of the mock image URL file
contents, err := ioutil.ReadFile(imageURLFile)
if err != nil {
log.Fatalf("Failed to read mock image URL file: %v", err)
}
// Replace the image URLs in the contents with the mock URL
modifiedContents := strings.Replace(string(contents), "IMAGE_URL", mockURL, -1)
// Write the modified contents to the temporary file
_, err = tempFile.WriteString(modifiedContents)
if err != nil {
log.Fatalf("Failed to write to temporary image URL file: %v", err)
}
return tempFile.Name()
}