|
| 1 | +package kit |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "io/fs" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "testing/fstest" |
| 10 | +) |
| 11 | + |
| 12 | +//go:embed testdata/* |
| 13 | +var testEmbeds embed.FS |
| 14 | + |
| 15 | +func TestAssetsURL(t *testing.T) { |
| 16 | + assets := NewAssets(testEmbeds) |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + path string |
| 20 | + want string |
| 21 | + }{ |
| 22 | + {"style.css", "/static/style.css"}, |
| 23 | + {"/style.css", "/static/style.css"}, |
| 24 | + {"js/app.js", "/static/js/app.js"}, |
| 25 | + } |
| 26 | + |
| 27 | + for _, tt := range tests { |
| 28 | + got := assets.URL(tt.path) |
| 29 | + if got != tt.want { |
| 30 | + t.Errorf("URL(%q) = %q, want %q", tt.path, got, tt.want) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestAssetsPrefix(t *testing.T) { |
| 36 | + assets := NewAssets(testEmbeds).WithPrefix("/assets") |
| 37 | + |
| 38 | + if assets.Prefix() != "/assets" { |
| 39 | + t.Errorf("Prefix() = %q, want %q", assets.Prefix(), "/assets") |
| 40 | + } |
| 41 | + |
| 42 | + url := assets.URL("style.css") |
| 43 | + if url != "/assets/style.css" { |
| 44 | + t.Errorf("URL() = %q, want %q", url, "/assets/style.css") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestAssetsOverlay(t *testing.T) { |
| 49 | + overlay := fstest.MapFS{ |
| 50 | + "custom.css": &fstest.MapFile{Data: []byte("/* custom */")}, |
| 51 | + } |
| 52 | + |
| 53 | + assets := NewAssets(testEmbeds).WithOverlay(overlay) |
| 54 | + |
| 55 | + content, err := assets.Read("custom.css") |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Read() error = %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + if string(content) != "/* custom */" { |
| 61 | + t.Errorf("Read() = %q, want %q", string(content), "/* custom */") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestAssetsOpenNotFound(t *testing.T) { |
| 66 | + assets := NewAssets(testEmbeds) |
| 67 | + |
| 68 | + _, err := assets.Open("nonexistent.css") |
| 69 | + if err == nil { |
| 70 | + t.Error("Open() expected error for nonexistent file") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestAssetsHandler(t *testing.T) { |
| 75 | + overlay := fstest.MapFS{ |
| 76 | + "test.css": &fstest.MapFile{Data: []byte("body { color: red; }")}, |
| 77 | + } |
| 78 | + |
| 79 | + assets := NewAssets(testEmbeds).WithOverlay(overlay) |
| 80 | + handler := assets.Handler() |
| 81 | + |
| 82 | + tests := []struct { |
| 83 | + path string |
| 84 | + wantStatus int |
| 85 | + wantType string |
| 86 | + }{ |
| 87 | + {"/static/test.css", http.StatusOK, "text/css; charset=utf-8"}, |
| 88 | + {"/static/notfound.css", http.StatusNotFound, ""}, |
| 89 | + {"/static/", http.StatusNotFound, ""}, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tt := range tests { |
| 93 | + req := httptest.NewRequest(http.MethodGet, tt.path, nil) |
| 94 | + w := httptest.NewRecorder() |
| 95 | + |
| 96 | + handler.ServeHTTP(w, req) |
| 97 | + |
| 98 | + if w.Code != tt.wantStatus { |
| 99 | + t.Errorf("Handler(%q) status = %d, want %d", tt.path, w.Code, tt.wantStatus) |
| 100 | + } |
| 101 | + |
| 102 | + if tt.wantType != "" && w.Header().Get("Content-Type") != tt.wantType { |
| 103 | + t.Errorf("Handler(%q) content-type = %q, want %q", |
| 104 | + tt.path, w.Header().Get("Content-Type"), tt.wantType) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestGetContentType(t *testing.T) { |
| 110 | + tests := []struct { |
| 111 | + name string |
| 112 | + want string |
| 113 | + }{ |
| 114 | + {"style.css", "text/css; charset=utf-8"}, |
| 115 | + {"app.js", "application/javascript; charset=utf-8"}, |
| 116 | + {"data.json", "application/json; charset=utf-8"}, |
| 117 | + {"page.html", "text/html; charset=utf-8"}, |
| 118 | + {"icon.svg", "image/svg+xml"}, |
| 119 | + {"photo.png", "image/png"}, |
| 120 | + {"photo.jpg", "image/jpeg"}, |
| 121 | + {"photo.jpeg", "image/jpeg"}, |
| 122 | + {"anim.gif", "image/gif"}, |
| 123 | + {"favicon.ico", "image/x-icon"}, |
| 124 | + {"font.woff", "font/woff"}, |
| 125 | + {"font.woff2", "font/woff2"}, |
| 126 | + {"font.ttf", "font/ttf"}, |
| 127 | + {"unknown.xyz", "application/octet-stream"}, |
| 128 | + } |
| 129 | + |
| 130 | + for _, tt := range tests { |
| 131 | + got := getContentType(tt.name) |
| 132 | + if got != tt.want { |
| 133 | + t.Errorf("getContentType(%q) = %q, want %q", tt.name, got, tt.want) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestAssetsHandlerDirectory(t *testing.T) { |
| 139 | + overlay := fstest.MapFS{ |
| 140 | + "subdir/file.txt": &fstest.MapFile{Data: []byte("test")}, |
| 141 | + } |
| 142 | + |
| 143 | + assets := NewAssets(testEmbeds).WithOverlay(overlay) |
| 144 | + handler := assets.Handler() |
| 145 | + |
| 146 | + req := httptest.NewRequest(http.MethodGet, "/subdir", nil) |
| 147 | + w := httptest.NewRecorder() |
| 148 | + |
| 149 | + handler.ServeHTTP(w, req) |
| 150 | + |
| 151 | + if w.Code != http.StatusNotFound { |
| 152 | + t.Errorf("Handler for directory status = %d, want %d", w.Code, http.StatusNotFound) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +type errorFS struct{} |
| 157 | + |
| 158 | +func (errorFS) Open(name string) (fs.File, error) { |
| 159 | + return nil, fs.ErrNotExist |
| 160 | +} |
| 161 | + |
| 162 | +func TestAssetsOverlayFallback(t *testing.T) { |
| 163 | + assets := NewAssets(testEmbeds).WithOverlay(errorFS{}) |
| 164 | + |
| 165 | + _, err := assets.Open("testdata/sample.txt") |
| 166 | + if err != nil { |
| 167 | + t.Errorf("Open() should fall back to embeds, got error: %v", err) |
| 168 | + } |
| 169 | +} |
0 commit comments