Initial GoWallpaper implementation
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDownloaderConvertsImageToJPEG(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
img := image.NewRGBA(image.Rect(0, 0, 2, 2))
|
||||
img.Set(0, 0, color.RGBA{R: 255, A: 255})
|
||||
_ = png.Encode(w, img)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
destination := filepath.Join(t.TempDir(), "wallpaper.jpg")
|
||||
if err := newDownloader().download(context.Background(), server.URL, destination); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := os.Open(destination)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
_, format, err := image.Decode(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if format != "jpeg" {
|
||||
t.Fatalf("expected jpeg, got %s", format)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloaderRejectsNonImage(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte("not an image"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := newDownloader().download(context.Background(), server.URL, filepath.Join(t.TempDir(), "wallpaper.jpg"))
|
||||
if err == nil {
|
||||
t.Fatal("expected non-image response to fail")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user