-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
231 lines (211 loc) · 7.08 KB
/
Copy pathutils_test.go
File metadata and controls
231 lines (211 loc) · 7.08 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/bbondy/go-brianbondy/data"
)
func TestSlugifyTitle(t *testing.T) {
tests := []struct {
title string
want string
}{
{"Hello World", "hello-world"},
{"This is a test!", "this-is-a-test"},
{"Another_Test 123", "another-test-123"},
}
for _, tt := range tests {
got := slugifyTitle(tt.title)
if got != tt.want {
t.Errorf("slugifyTitle(%q) = %v, want %v", tt.title, got, tt.want)
}
}
}
func TestGetTitle(t *testing.T) {
titleSlug := "test-slug"
want := "test-slug - Brian R. Bondy"
got := GetTitle(titleSlug)
if got != want {
t.Errorf("GetTitle(%q) = %v, want %v", titleSlug, got, want)
}
}
func TestDirectToHttps(t *testing.T) {
tests := []struct {
name string
url string
proto string
host string
forwardedProto string
wantRedirect bool
wantLocation string
}{
{"http to canonical https", "http://example.com/test?x=1", "HTTP/1.1", "example.com", "", true, "https://brianbondy.com/test?x=1"},
{"www redirects to apex", "https://www.brianbondy.com", "HTTP/2.0", "www.brianbondy.com", "", true, "https://brianbondy.com/"},
{"canonical https", "https://brianbondy.com", "HTTP/2.0", "brianbondy.com", "", false, ""},
{"localhost no redirect", "http://localhost:8080", "HTTP/1.1", "localhost:8080", "", false, ""},
{"forwarded canonical https", "http://brianbondy.com", "HTTP/1.1", "brianbondy.com", "https", false, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", tt.url, nil)
req.Proto = tt.proto
req.Host = tt.host
req.Header.Set("X-Forwarded-Proto", tt.forwardedProto)
w := httptest.NewRecorder()
directToHttps(w, req, func(w http.ResponseWriter, r *http.Request) {})
if tt.wantRedirect {
if w.Code != http.StatusPermanentRedirect {
t.Errorf("expected status %v, got %v", http.StatusPermanentRedirect, w.Code)
}
if got := w.Header().Get("Location"); got != tt.wantLocation {
t.Errorf("expected location %q, got %q", tt.wantLocation, got)
}
} else {
if w.Code != http.StatusOK {
t.Errorf("expected status %v, got %v", http.StatusOK, w.Code)
}
}
})
}
}
func TestCanonicalURL(t *testing.T) {
if got := canonicalURL(&data.SimpleMarkdownPage{MarkdownSlug: "about.markdown"}); got != "https://brianbondy.com/about" {
t.Errorf("canonicalURL() = %q, want about URL", got)
}
if got := canonicalURL(&data.BlogPostPage{ShareUrl: "/blog/1/test-post"}); got != "https://brianbondy.com/blog/1/test-post" {
t.Errorf("canonicalURL() = %q, want blog URL", got)
}
}
func TestExtractFirstParagraph(t *testing.T) {
tests := []struct {
name string
content string
want string
}{
{
"p tags",
"<p>This is a test paragraph.</p><p>Another paragraph.</p>",
"This is a test paragraph.",
},
{
"no p tags",
"This is a test paragraph. Another paragraph.",
"This is a test paragraph. Another paragraph.",
},
{
"long p tag",
"<p>" + strings.Repeat("A", 400) + "</p>",
strings.Repeat("A", 300) + "...",
},
{
"long content no p tags",
strings.Repeat("A", 400),
strings.Repeat("A", 300) + "...",
},
{
"empty p tag",
"<p></p>Some content",
"Some content",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractFirstParagraph(tt.content)
if got != tt.want {
t.Errorf("extractFirstParagraph() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetImageMimeType(t *testing.T) {
tests := []struct {
imagePath string
want string
}{
{"image.png", "image/png"},
{"image.webp", "image/webp"},
{"image.jpg", "image/jpeg"},
{"image.jpeg", "image/jpeg"},
{"image.gif", "image/gif"},
{"image.bmp", ""},
}
for _, tt := range tests {
got := getImageMimeType(tt.imagePath)
if got != tt.want {
t.Errorf("getImageMimeType(%q) = %v, want %v", tt.imagePath, got, tt.want)
}
}
}
func TestOptimizeImageTag(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
"basic image optimization",
`<img src="/static/img/test.jpg" alt="test">`,
`<img src="/static/img/test.jpg" alt="test" loading="lazy" data-lightbox-src="/static/img/test.jpg" decoding="async">`,
},
{
"image with existing loading attribute",
`<img src="/static/img/test.png" alt="test" loading="lazy">`,
`<img src="/static/img/test.png" alt="test" loading="lazy" data-lightbox-src="/static/img/test.png" decoding="async">`,
},
{
"external image - no optimization",
`<img src="https://example.com/image.jpg" alt="test">`,
`<img src="https://example.com/image.jpg" alt="test">`,
},
{
"data URL - no optimization",
`<img src="data:image/png;base64,test" alt="test">`,
`<img src="data:image/png;base64,test" alt="test">`,
},
{
"non-static image - no optimization",
`<img src="/other/image.jpg" alt="test">`,
`<img src="/other/image.jpg" alt="test">`,
},
{
"image with responsive variants",
`<img src="/static/img/blogpost_190/tahoe-start.webp" alt="test">`,
`<img src="/static/img/blogpost_190/tahoe-start.webp" alt="test" loading="lazy" data-lightbox-src="/static/img/blogpost_190/tahoe-start.webp" srcset="/static/img/blogpost_190/tahoe-start-640.webp 640w, /static/img/blogpost_190/tahoe-start-960.webp 960w, /static/img/blogpost_190/tahoe-start-1200.webp 1200w" sizes="(max-width: 800px) calc(100vw - 44px), 756px" decoding="async">`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := optimizeImageTag(tt.input)
if got != tt.expected {
t.Errorf("optimizeImageTag() = %v, want %v", got, tt.expected)
}
})
}
}
func TestOptimizeImagesInContent(t *testing.T) {
input := `<p>Some text</p><img src="/static/img/test.jpg" alt="test"><p>More text</p><img src="/static/img/test2.png" alt="test2">`
expected := `<p>Some text</p><img src="/static/img/test.jpg" alt="test" loading="lazy" data-lightbox-src="/static/img/test.jpg" decoding="async"><p>More text</p><img src="/static/img/test2.png" alt="test2" loading="lazy" data-lightbox-src="/static/img/test2.png" decoding="async">`
got := optimizeImagesInContent(input)
if got != expected {
t.Errorf("optimizeImagesInContent() = %v, want %v", got, expected)
}
}
func TestResponsiveImageSrcset(t *testing.T) {
got := responsiveImageSrcset("/static/img/blogpost_190/tahoe-start.webp")
want := "/static/img/blogpost_190/tahoe-start-640.webp 640w, /static/img/blogpost_190/tahoe-start-960.webp 960w, /static/img/blogpost_190/tahoe-start-1200.webp 1200w"
if got != want {
t.Errorf("responsiveImageSrcset() = %q, want %q", got, want)
}
}
func TestResponsiveImageSizesFor(t *testing.T) {
if got := responsiveImageSizesFor(); got != responsiveImageSizes {
t.Errorf("responsiveImageSizesFor() = %q, want %q", got, responsiveImageSizes)
}
if got := responsiveImageSizesFor(""); got != responsiveImageSizes {
t.Errorf("responsiveImageSizesFor(\"\") = %q, want %q", got, responsiveImageSizes)
}
if got := responsiveImageSizesFor("160px"); got != "160px" {
t.Errorf("responsiveImageSizesFor(\"160px\") = %q, want %q", got, "160px")
}
}