From 4ff0c534c678caa5c3dc6ce7660ad330238c0b11 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Mon, 21 Apr 2025 23:04:34 -0400 Subject: [PATCH] hoo boy --- reddit/client_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 reddit/client_test.go diff --git a/reddit/client_test.go b/reddit/client_test.go new file mode 100644 index 0000000..9759a91 --- /dev/null +++ b/reddit/client_test.go @@ -0,0 +1,70 @@ +package reddit + +import ( + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestGetLatestImage(t *testing.T) { + // Mock Reddit API response + mockResponse := `{ + "kind": "Listing", + "data": { + "after": null, + "dist": 1, + "modhash": "", + "geo_filter": "", + "children": [ + { + "kind": "t3", + "data": { + "url_overridden_by_dest": "https://example.com/gallery", + "media_metadata": {}, + "preview": {} + } + }, + { + "kind": "t3", + "data": { + "url_overridden_by_dest": "https://example.com/image.jpg", + "media_metadata": {}, + "preview": {} + } + } + ] + } + }` + + // Create a test server + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.Contains(r.URL.Path, "/r/wallpaper/.json") { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(mockResponse)) + } else { + w.WriteHeader(http.StatusNotFound) + } + })) + defer server.Close() + + // Custom httpGet function for testing + httpGet := func(url string) (*http.Response, error) { + if strings.Contains(url, "/r/wallpaper/.json") { + return http.Get(server.URL + "/r/wallpaper/.json") + } + return nil, fmt.Errorf("unexpected URL: %s", url) + } + + // Test the function + imageURL, err := GetLatestImage(httpGet) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedURL := "https://example.com/image.jpg" + if imageURL != expectedURL { + t.Errorf("expected %s, got %s", expectedURL, imageURL) + } +}