-
Notifications
You must be signed in to change notification settings - Fork 0
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
Patrick Birchall
committed
Dec 10, 2024
1 parent
6b5ef9f
commit a2613e3
Showing
1 changed file
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestDownloadImage(t *testing.T) { | ||
downloader := RealImageDownloader{} | ||
|
||
// Create a temporary directory for testing | ||
tempDir := t.TempDir() | ||
|
||
// Test case: Successful download | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("test image content")) | ||
})) | ||
defer server.Close() | ||
|
||
filename := "test_image.jpg" | ||
err := downloader.DownloadImage(server.URL, tempDir, filename) | ||
if err != nil { | ||
t.Errorf("expected no error, got %v", err) | ||
} | ||
|
||
// Check if the file was created | ||
filePath := filepath.Join(tempDir, filename) | ||
if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
t.Errorf("expected file %s to be created, but it does not exist", filePath) | ||
} | ||
|
||
// Check the file content | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
t.Errorf("expected no error opening file, got %v", err) | ||
} | ||
defer file.Close() | ||
|
||
content, err := io.ReadAll(file) | ||
if err != nil { | ||
t.Errorf("expected no error reading file, got %v", err) | ||
} | ||
|
||
expectedContent := "test image content" | ||
if string(content) != expectedContent { | ||
t.Errorf("expected file content %s, got %s", expectedContent, string(content)) | ||
} | ||
} |