forked from ampproject/amppackager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_test.go
370 lines (343 loc) · 9.93 KB
/
transformer_test.go
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package transformer
import (
"errors"
"fmt"
"strings"
"testing"
rpb "github.com/ampproject/amppackager/transformer/request"
"github.com/ampproject/amppackager/transformer/transformers"
"github.com/google/go-cmp/cmp"
)
func TestProcess(t *testing.T) {
var fns []func(*transformers.Context) error
// Remember the original function and reinstate after test
orig := runTransformers
defer func() { runTransformers = orig }()
runTransformers = func(e *transformers.Context, fs []func(*transformers.Context) error) error {
fns = fs
return nil
}
// TODO(alin04): Test for func identity equality.
tests := []struct {
config rpb.Request_TransformersConfig
expectedLen int
}{
{rpb.Request_DEFAULT, 12},
{rpb.Request_NONE, 0},
{rpb.Request_VALIDATION, 1},
{rpb.Request_CUSTOM, 0},
}
for _, tc := range tests {
t.Run(tc.config.String(), func(t *testing.T) {
r := rpb.Request{Html: "<html ⚡><lemur>", Config: tc.config}
html, metadata, err := Process(&r)
if err != nil {
t.Fatalf("unexpected failure %v", err)
}
expectedHTML := "<html ⚡><head></head><body><lemur></lemur></body></html>"
if html != expectedHTML {
t.Errorf("got = %q, want = %q", html, expectedHTML)
}
if metadata == nil {
t.Error("metadata unexpectedly nil")
}
if len(fns) != tc.expectedLen {
t.Errorf("number of transformers, got=%d, want=%d", len(fns), tc.expectedLen)
}
})
}
}
func TestPreloads(t *testing.T) {
// Programmatically prepare the `> maxPreloads` test case.
var manyScriptsHTML strings.Builder
manyScriptsPreloads := []*rpb.Metadata_Preload{}
manyScriptsHTML.WriteString("<html ⚡>")
for i := 0; i <= maxPreloads; i++ {
fmt.Fprintf(&manyScriptsHTML, `<script src="foo%d"></script>`, i)
if i < maxPreloads {
manyScriptsPreloads = append(manyScriptsPreloads, &rpb.Metadata_Preload{Url: fmt.Sprintf("foo%d", i), As: "script"})
}
}
tcs := []struct {
html string
expectedPreloads []*rpb.Metadata_Preload
}{
{
"<html ⚡><script>",
[]*rpb.Metadata_Preload{},
},
{
"<html ⚡><script src=foo>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "script"}},
},
{
"<html ⚡><link rel=foaf href=foo>",
[]*rpb.Metadata_Preload{},
},
{
"<html ⚡><link rel=stylesheet href=foo>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "style"}},
},
{ // case-insensitive
"<html ⚡><link rel=STYLEsheet href=foo>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "style"}},
},
{
"<html ⚡><link rel=stylesheet href=foo><script src=bar>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "style"}, {Url: "bar", As: "script"}},
},
{
manyScriptsHTML.String(),
manyScriptsPreloads,
},
}
for _, tc := range tcs {
t.Run(tc.html, func(t *testing.T) {
_, metadata, err := Process(&rpb.Request{Html: tc.html, Config: rpb.Request_NONE})
if err != nil {
t.Fatalf("unexpected failure: %v", err)
}
if diff := cmp.Diff(tc.expectedPreloads, metadata.Preloads); diff != "" {
t.Errorf("preloads differ (-want +got):\n%s", diff)
}
})
}
}
func TestVersion(t *testing.T) {
// context is the context provided by Process() to runTransformers().
var context *transformers.Context
// Remember the original function and reinstate after test
orig := runTransformers
defer func() { runTransformers = orig }()
runTransformers = func(e *transformers.Context, fs []func(*transformers.Context) error) error {
context = e
return nil
}
_, _, err := Process(&rpb.Request{Html: "<html ⚡>", Config: rpb.Request_NONE, Version: SupportedVersions[0].Max})
if err != nil {
t.Fatalf("Process() unexpectedly failed: %v", err)
}
if context.Version != SupportedVersions[0].Max {
t.Errorf("Incorrect context.Version = %d", context.Version)
}
// Construct an unsatisfied version request.
badVersion := SupportedVersions[0].Max + 1
_, _, err = Process(&rpb.Request{Html: "", Config: rpb.Request_NONE, Version: badVersion})
if err == nil {
t.Fatal("Process() unexpectedly succeeded")
}
}
func TestCustom(t *testing.T) {
var fns []func(*transformers.Context) error
// Remember the original function and reinstate after test
orig := runTransformers
defer func() { runTransformers = orig }()
runTransformers = func(e *transformers.Context, fs []func(*transformers.Context) error) error {
fns = fs
return nil
}
// Case insensitive
tcs := []string{
"aMpBoIlerplate",
"AMPRuntimeCSS",
"linktag",
"NODECLEANUP",
"reorderHead",
"serverSideRendering",
"transformedIdentifier",
"aBsolUTEuRl",
"urlRewriTE",
}
for _, tc := range tcs {
t.Run(tc, func(t *testing.T) {
r := rpb.Request{Html: "<html ⚡><lemur>", Config: rpb.Request_CUSTOM, Transformers: []string{tc}}
if _, _, err := Process(&r); err != nil {
t.Fatalf("unexpected failure %v", err)
}
if len(fns) != 1 {
t.Error("expected successful transformer lookup")
}
})
}
}
func TestCustomFail(t *testing.T) {
r := rpb.Request{Html: "<html ⚡><lemur>", Config: rpb.Request_CUSTOM, Transformers: []string{"does_not_exist"}}
if html, _, err := Process(&r); err == nil {
t.Fatalf("Process(%v) = %s, nil; want error", r, html)
}
}
func TestError(t *testing.T) {
s := "something happened!"
// Remember the original function and reinstate after test
orig := runTransformers
defer func() { runTransformers = orig }()
runTransformers = func(e *transformers.Context, fs []func(*transformers.Context) error) error {
return errors.New(s)
}
r := rpb.Request{Html: "<html ⚡><lemur>", Config: rpb.Request_DEFAULT}
_, _, err := Process(&r)
if err == nil {
t.Fatalf("Process() unexpectedly succeeded")
}
if err.Error() != s {
t.Fatalf("mismatched error. got=%s, want=%s", err.Error(), s)
}
}
func TestInvalidUTF8(t *testing.T) {
tcs := []struct{ html, expectedError string }{
{"<html ⚡><le\003mur>", "character U+0003 at position 13 is not allowed in AMPHTML"},
{"<html ⚡><le\xc0mur>", "invalid UTF-8 at byte position 13"},
}
for _, tc := range tcs {
t.Run(tc.html, func(t *testing.T) {
r := rpb.Request{Html: tc.html, Config: rpb.Request_DEFAULT}
_, _, err := Process(&r)
if err == nil {
t.Fatal("unexpected success")
}
if err.Error() != tc.expectedError {
t.Fatalf("mismatched error. got=%s, want=%s", err.Error(), tc.expectedError)
}
})
}
}
func TestRequireAMPAttribute(t *testing.T) {
tcs := []struct {
desc string
html string
expectedError bool
expectedErrorInAMP bool
expectedErrorInAMP4Ads bool
expectedErrorInAMP4Email bool
}{
{
"⚡",
"<html ⚡><head></head><body></body></html>",
false, false, true, true,
},
{
"amp",
"<html amp><head></head><body></body></html>",
false, false, true, true,
},
{
"AMP",
"<HTML AMP><HEAD></HEAD><BODY></BODY></HTML>",
false, false, true, true,
},
{
"⚡4ads",
"<html ⚡4ads><head></head><body></body></html>",
false, true, false, true,
},
{
"amp4ads",
"<html amp4ads><head></head><body></body></html>",
false, true, false, true,
},
{
"AMP4ADS",
"<HTML AMP4ADS><HEAD></HEAD><BODY></BODY></HTML>",
false, true, false, true,
},
{
"⚡4email",
"<html ⚡4email><head></head><body></body></html>",
false, true, true, false,
},
{
"amp4email",
"<html amp4email><head></head><body></body></html>",
false, true, true, false,
},
{
"AMP4EMAIL",
"<HTML AMP4EMAIL><HEAD></HEAD><BODY></BODY></HTML>",
false, true, true, false,
},
{
"amp4ads amp4email",
"<html amp4ads amp4email><head></head><body></body></html>",
false, true, false, false,
},
{
"amp4",
"<html amp4><head></head><body></body></html>",
true, true, true, true,
},
{
"not AMP",
"<html><head></head><body></body></html>",
true, true, true, true,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
r := rpb.Request{Html: tc.html, Config: rpb.Request_NONE}
_, _, err := Process(&r)
if (err != nil) != tc.expectedError {
t.Errorf("Process() has error=%#v want=%t", err, tc.expectedError)
}
r = rpb.Request{Html: tc.html, Config: rpb.Request_NONE, AllowedFormats: []rpb.Request_HtmlFormat{rpb.Request_AMP}}
_, _, err = Process(&r)
if (err != nil) != tc.expectedErrorInAMP {
t.Errorf("Process(AMP) has error=%#v want=%t", err, tc.expectedErrorInAMP)
}
r = rpb.Request{Html: tc.html, Config: rpb.Request_NONE, AllowedFormats: []rpb.Request_HtmlFormat{rpb.Request_AMP4ADS}}
_, _, err = Process(&r)
if (err != nil) != tc.expectedErrorInAMP4Ads {
t.Errorf("Process(AMP4Ads) has error=%#v want=%t", err, tc.expectedErrorInAMP4Ads)
}
r = rpb.Request{Html: tc.html, Config: rpb.Request_NONE, AllowedFormats: []rpb.Request_HtmlFormat{rpb.Request_AMP4EMAIL}}
_, _, err = Process(&r)
if (err != nil) != tc.expectedErrorInAMP4Email {
t.Errorf("Process(AMP4Email) has error=%#v want=%t", err, tc.expectedErrorInAMP4Email)
}
})
}
}
func TestBaseURL(t *testing.T) {
docURL := "http://example.com/a/page.html"
tcs := []struct {
desc, base, expected string
}{
{
"no base href",
"<base target=_top>",
docURL,
},
{
"absolute",
"<base href=https://www.foo.com>",
"https://www.foo.com",
},
{
"relative",
"<base href=\"./child/to/a\">",
"http://example.com/a/child/to/a",
},
{
"relative to root",
"<base href=\"/\">",
"http://example.com/",
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
// Remember the original function and reinstate after test
orig := runTransformers
defer func() { runTransformers = orig }()
runTransformers = func(e *transformers.Context, fs []func(*transformers.Context) error) error {
if e.BaseURL.String() != tc.expected {
t.Errorf("setBaseURL(%s)=%s, want=%s", tc.base, e.BaseURL, tc.expected)
}
return nil
}
r := rpb.Request{Html: "<html amp><head>" + tc.base + "</head></html>", DocumentUrl: docURL, Config: rpb.Request_NONE}
_, _, err := Process(&r)
if err != nil {
t.Fatalf("unexpected failure %v", err)
}
})
}
}