-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallback_test.go
More file actions
200 lines (176 loc) · 5.29 KB
/
fallback_test.go
File metadata and controls
200 lines (176 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"strings"
"testing"
)
func TestFallbackLoad(t *testing.T) {
fb, err := NewFallback("testdata/fallback", DefaultInlineExtensions)
if err != nil {
t.Fatalf("NewFallback: %v", err)
}
if len(fb.all) != 3 {
t.Fatalf("loaded %d placeholders, want 3", len(fb.all))
}
if len(fb.byExt[".jpg"]) != 2 {
t.Fatalf("jpg count = %d, want 2", len(fb.byExt[".jpg"]))
}
if len(fb.byExt[".pdf"]) != 1 {
t.Fatalf("pdf count = %d, want 1", len(fb.byExt[".pdf"]))
}
}
func TestFallbackLoad_EmptyDir(t *testing.T) {
dir := t.TempDir()
fb, err := NewFallback(dir, DefaultInlineExtensions)
if err != nil {
t.Fatalf("NewFallback: %v", err)
}
if len(fb.all) != 0 {
t.Fatalf("loaded %d images, want 0", len(fb.all))
}
}
func TestFallbackSelect_Deterministic(t *testing.T) {
fb, _ := NewFallback("testdata/fallback", DefaultInlineExtensions)
img1 := fb.Select("some/key.jpg")
img2 := fb.Select("some/key.jpg")
if img1.Path != img2.Path {
t.Fatalf("same key returned different images: %q vs %q", img1.Path, img2.Path)
}
}
func TestFallbackSelect_DifferentKeys(t *testing.T) {
fb, _ := NewFallback("testdata/fallback", DefaultInlineExtensions)
img1 := fb.Select("key-a.jpg")
img2 := fb.Select("key-b.jpg")
if img1.Body == nil || img2.Body == nil {
t.Fatal("expected non-nil bodies")
}
}
func TestFallbackSelect_MatchesExtension(t *testing.T) {
fb, _ := NewFallback("testdata/fallback", DefaultInlineExtensions)
// PDF key should get the PDF placeholder
p := fb.Select("document/report.pdf")
if p == nil {
t.Fatal("expected placeholder")
}
if !strings.HasSuffix(p.Path, ".pdf") {
t.Fatalf("expected PDF placeholder, got %q", p.Path)
}
// JPG key should get a JPG placeholder
p = fb.Select("images/photo.jpg")
if p == nil {
t.Fatal("expected placeholder")
}
if !strings.HasSuffix(p.Path, ".jpg") {
t.Fatalf("expected JPG placeholder, got %q", p.Path)
}
}
func TestFallbackSelect_NilForUnmatchedExtension(t *testing.T) {
fb, _ := NewFallback("testdata/fallback", DefaultInlineExtensions)
p := fb.Select("data/export.csv")
if p != nil {
t.Fatal("expected nil for extension with no placeholders")
}
}
func TestFallbackSelect_NoImages(t *testing.T) {
dir := t.TempDir()
fb, _ := NewFallback(dir, DefaultInlineExtensions)
img := fb.Select("any.jpg")
if img != nil {
t.Fatal("expected nil when no fallback images")
}
}
func TestParseExtList(t *testing.T) {
cases := []struct {
in string
want []string
}{
{"", []string{}},
{"jpg", []string{".jpg"}},
{".jpg", []string{".jpg"}},
{"JPG", []string{".jpg"}},
{"jpeg", []string{".jpeg"}},
{" .jpg , png , WEBP ", []string{".jpg", ".png", ".webp"}},
{",,", []string{}},
}
for _, c := range cases {
got := ParseExtList(c.in)
if len(got) != len(c.want) {
t.Errorf("ParseExtList(%q) = %v, want %v", c.in, got, c.want)
continue
}
for i := range got {
if got[i] != c.want[i] {
t.Errorf("ParseExtList(%q)[%d] = %q, want %q", c.in, i, got[i], c.want[i])
}
}
}
}
func TestDefaultInlineExtensions_CoversCurrentFallbackSet(t *testing.T) {
want := []string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".pdf", ".mp4", ".mov", ".webm", ".avi"}
set := map[string]bool{}
for _, e := range DefaultInlineExtensions {
set[e] = true
}
for _, e := range want {
if !set[e] {
t.Errorf("DefaultInlineExtensions missing %q", e)
}
}
}
func TestFallbackDisposition_InlineForDefaults(t *testing.T) {
fb, _ := NewFallback("testdata/fallback", DefaultInlineExtensions)
got := fb.Disposition("photos/sunset.jpg")
want := `inline; filename="sunset.jpg"`
if got != want {
t.Fatalf("Disposition = %q, want %q", got, want)
}
}
func TestFallbackDisposition_AttachmentForUnlisted(t *testing.T) {
fb, _ := NewFallback("testdata/fallback", DefaultInlineExtensions)
got := fb.Disposition("docs/report.docx")
want := `attachment; filename="report.docx"`
if got != want {
t.Fatalf("Disposition = %q, want %q", got, want)
}
}
func TestFallbackDisposition_CustomList(t *testing.T) {
// Explicit empty list → everything is attachment.
fb, _ := NewFallback("testdata/fallback", []string{})
got := fb.Disposition("images/a.jpg")
want := `attachment; filename="a.jpg"`
if got != want {
t.Fatalf("Disposition = %q, want %q", got, want)
}
// Custom list adds docx as inline.
fb2, _ := NewFallback("testdata/fallback", []string{".docx"})
got = fb2.Disposition("reports/q1.docx")
want = `inline; filename="q1.docx"`
if got != want {
t.Fatalf("Disposition custom = %q, want %q", got, want)
}
}
func TestFallbackDisposition_JpegAliasedToJpg(t *testing.T) {
// Inline list contains .jpg; requesting .jpeg should still be inline.
fb, _ := NewFallback("testdata/fallback", []string{".jpg"})
got := fb.Disposition("pic.jpeg")
want := `inline; filename="pic.jpeg"`
if got != want {
t.Fatalf("Disposition = %q, want %q", got, want)
}
}
func TestFallbackDisposition_SanitizesNonASCII(t *testing.T) {
fb, _ := NewFallback("testdata/fallback", DefaultInlineExtensions)
cases := []struct {
key string
want string
}{
{"photos/café.jpg", `inline; filename="caf__.jpg"`},
{"weird/\"name\".jpg", `inline; filename="_name_.jpg"`},
{"ctl/\x01name.jpg", `inline; filename="_name.jpg"`},
}
for _, c := range cases {
got := fb.Disposition(c.key)
if got != c.want {
t.Errorf("Disposition(%q) = %q, want %q", c.key, got, c.want)
}
}
}