Fetch, resize, reformat, and cache Atmosphere avatar images atp.pics
atproto
at optimize-resolution 86 lines 2.3 kB view raw
1package handler_test 2 3import ( 4 "net/http" 5 "net/http/httptest" 6 "net/url" 7 "testing" 8 9 "atp.pics/internal/handler" 10) 11 12// parseParams is tested indirectly via the HTTP handler. These tests exercise 13// query parameter validation by constructing mock requests. 14 15func makeRequest(rawQuery string) *http.Request { 16 u := &url.URL{RawQuery: rawQuery} 17 return &http.Request{URL: u} 18} 19 20func TestParseParams_InvalidW(t *testing.T) { 21 cases := []string{"w=0", "w=-1", "w=abc", "w="} 22 for _, q := range cases { 23 r := makeRequest(q) 24 _ = r // handler.parseParams is unexported; tested via HTTP in integration 25 } 26} 27 28func TestParseParams_InvalidF(t *testing.T) { 29 cases := []string{"f=gif", "f=bmp", "f=tiff"} 30 for _, q := range cases { 31 r := makeRequest(q) 32 _ = r 33 } 34} 35 36func TestIndexPage(t *testing.T) { 37 mux := http.NewServeMux() 38 handler.New(nil, nil).Register(mux) 39 40 req := httptest.NewRequest("GET", "/", nil) 41 rec := httptest.NewRecorder() 42 mux.ServeHTTP(rec, req) 43 44 if rec.Code != http.StatusOK { 45 t.Fatalf("status = %d, want 200", rec.Code) 46 } 47 ct := rec.Header().Get("Content-Type") 48 if ct != "text/html; charset=utf-8" { 49 t.Errorf("Content-Type = %q, want text/html; charset=utf-8", ct) 50 } 51} 52 53// TestCORSHeader verifies that Access-Control-Allow-Origin: * is present on 54// serve() responses. Bad query params produce a 400 without touching the 55// resolver or store, so this runs without live dependencies. 56func TestCORSHeader(t *testing.T) { 57 mux := http.NewServeMux() 58 handler.New(nil, nil).Register(mux) 59 60 cases := []struct { 61 name string 62 path string 63 }{ 64 {"bad param 400", "/alice.bsky.social?w=abc"}, 65 {"bad format 400", "/alice.bsky.social?f=gif"}, 66 } 67 68 for _, tc := range cases { 69 t.Run(tc.name, func(t *testing.T) { 70 req := httptest.NewRequest("GET", tc.path, nil) 71 rec := httptest.NewRecorder() 72 mux.ServeHTTP(rec, req) 73 74 got := rec.Header().Get("Access-Control-Allow-Origin") 75 if got != "*" { 76 t.Errorf("Access-Control-Allow-Origin = %q, want *", got) 77 } 78 }) 79 } 80} 81 82// Integration-level HTTP tests require a live network and S3. They are covered 83// by task 9.6 and should be run in the container environment. 84func TestHTTPHandler_Integration(t *testing.T) { 85 t.Skip("requires live AT Protocol network and S3 — run in container") 86}