diff --git a/downloadImage.go b/downloadImage.go index d5c6f86..80fb8aa 100644 --- a/downloadImage.go +++ b/downloadImage.go @@ -6,8 +6,14 @@ import ( "os" ) +type ImageDownloader interface { + DownloadImage(url, dir, filename string) error +} + +type RealImageDownloader struct{} + // DownloadImage downloads an image from a URL and saves it to a file in a subdirectory -func DownloadImage(url string, subdirectory string, filename string) error { +func (d RealImageDownloader) DownloadImage(url string, subdirectory string, filename string) error { // Create the subdirectory if it doesn't exist if _, err := os.Stat(subdirectory); os.IsNotExist(err) { os.Mkdir(subdirectory, 0755) diff --git a/fetchAPOD.go b/fetchAPOD.go index 116ac18..187bfdb 100644 --- a/fetchAPOD.go +++ b/fetchAPOD.go @@ -2,15 +2,20 @@ package main import ( "encoding/json" + "fmt" "io" "net/http" "os" ) // FetchAPOD fetches the Astronomy Picture of the Day (APOD) from the NASA API -func FetchAPOD() (Response, error) { +func FetchAPOD(apiURL string) (Response, error) { apiKey := os.Getenv("NASA_KEY") - url := "https://api.nasa.gov/planetary/apod?api_key=" + apiKey + if apiKey == "" { + return Response{}, fmt.Errorf("NASA_KEY environment variable not set") + } + + url := apiURL + "?api_key=" + apiKey resp, err := http.Get(url) if err != nil { diff --git a/fetchAPOD_test.go b/fetchAPOD_test.go new file mode 100644 index 0000000..3699876 --- /dev/null +++ b/fetchAPOD_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "io" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" +) + +func MockResponse(statusCode int, body string) *http.Response { + return &http.Response{ + StatusCode: statusCode, + Body: io.NopCloser(strings.NewReader(body)), + } +} + +func TestFetchAPOD(t *testing.T) { + os.Unsetenv("NASA_KEY") + _, err := FetchAPOD("https://api.nasa.gov/planetary/apod") + if err == nil || err.Error() != "NASA_KEY environment variable not set" { + t.Errorf("expected error 'NASA_KEY environment variable not set', got %v", err) + } + + os.Setenv("NASA_KEY", "DEMO_KEY") + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"date":"2023-10-01","explanation":"Test explanation","media_type":"image","service_version":"v1","title":"Test Title","url":"https://example.com/test.jpg"}`)) + })) + defer server.Close() + + resp, err := FetchAPOD(server.URL) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if resp.Title != "Test Title" { + t.Errorf("expected title 'Test Title', got %v", resp.Title) + } +} diff --git a/main.go b/main.go index 189d254..ec9254d 100644 --- a/main.go +++ b/main.go @@ -17,10 +17,16 @@ type Response struct { } func main() { + downloader := RealImageDownloader{} + if err := run(downloader); err != nil { + panic(err) + } +} - result, err := FetchAPOD() +func run(downloader ImageDownloader) error { + result, err := FetchAPOD("https://api.nasa.gov/planetary/apod") if err != nil { - panic(err) + return err } fmt.Println("Downloading image: " + result.Title) @@ -28,6 +34,5 @@ func main() { filename := result.Title + ".jpg" noSpaceFilename := strings.ReplaceAll(filename, " ", "_") - DownloadImage(result.Hdurl, "images", noSpaceFilename) - + return downloader.DownloadImage(result.Hdurl, "images", noSpaceFilename) } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..a9d07b9 --- /dev/null +++ b/main_test.go @@ -0,0 +1,36 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "os" + "testing" +) + +type MockImageDownloader struct{} + +func (d MockImageDownloader) DownloadImage(url, dir, filename string) error { + return nil +} + +func TestRun(t *testing.T) { + downloader := MockImageDownloader{} + + os.Unsetenv("NASA_KEY") + err := run(downloader) + if err == nil || err.Error() != "NASA_KEY environment variable not set" { + t.Errorf("expected error 'NASA_KEY environment variable not set', got %v", err) + } + + os.Setenv("NASA_KEY", "DEMO_KEY") + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"date":"2023-10-01","explanation":"Test explanation","media_type":"image","service_version":"v1","title":"Test Title","url":"https://example.com/test.jpg"}`)) + })) + defer server.Close() + + err = run(downloader) + if err != nil { + t.Errorf("expected no error, got %v", err) + } +}