-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtransformer_test.go
509 lines (479 loc) · 15.4 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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"
"github.com/kylelemons/godebug/diff"
"github.com/golang/protobuf/proto"
)
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, 13},
{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 ⚡><head>")
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
expectedHTML string
expectedPreloads []*rpb.Metadata_Preload
}{
{
"<html ⚡><script>",
"<html ⚡><head><script></script></head><body></body></html>",
[]*rpb.Metadata_Preload{},
},
{
"<html ⚡><script src=foo crossorigin=anonymous>",
"<html ⚡><head><script crossorigin=anonymous src=foo></script></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "script", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "crossorigin", Val: "anonymous"}}}},
},
{
"<html ⚡><script src=mjs type=module async crossorigin=anonymous></script><script src=js async nomodule></script>",
"<html ⚡><head><script async crossorigin=anonymous src=mjs type=module></script><script async nomodule src=js></script></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "mjs", As: "script", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "crossorigin", Val: "anonymous"}}, Module: true}},
},
{
"<html ⚡><script src=foo>",
"<html ⚡><head><script src=foo></script></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "script"}},
},
{
"<html ⚡><link rel=foaf href=foo>",
"<html ⚡><head><link href=foo rel=foaf></head><body></body></html>",
[]*rpb.Metadata_Preload{},
},
{
"<html ⚡><link rel=stylesheet href=foo>",
"<html ⚡><head><link href=foo rel=stylesheet></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "style"}},
},
{ // case-insensitive
"<html ⚡><link rel=STYLEsheet href=foo>",
"<html ⚡><head><link href=foo rel=STYLEsheet></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "style"}},
},
{
"<html ⚡><link rel=stylesheet href=foo><script src=bar>",
"<html ⚡><head><link href=foo rel=stylesheet><script src=bar></script></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "style"}, {Url: "bar", As: "script"}},
},
{
manyScriptsHTML.String(),
manyScriptsHTML.String() + "</head><body></body></html>",
manyScriptsPreloads,
},
{
"<html ⚡><link rel=preload href=foo>",
"<html ⚡><head><link href=foo rel=preload></head><body></body></html>",
[]*rpb.Metadata_Preload{},
},
{
"<html ⚡><link rel=preload as=image href=foo>",
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "image"}},
},
{
"<html ⚡><link rel=preload as=image href=foo imagesrcset=bar>",
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar"}}}},
},
{
`<html ⚡><link rel=preload as=image imagesrcset="bar,">`,
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "bar", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar,"}}}},
},
{
`<html ⚡><link rel=preload as=image imagesrcset="bar, baz">`,
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "bar", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar, baz"}}}},
},
{
`<html ⚡><link rel=preload as=image imagesrcset=",, ,,bar">`,
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "bar", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: ",, ,,bar"}}}},
},
{
`<html ⚡><link rel=preload as=image imagesrcset="bar,baz quux">`,
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "bar,baz", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar,baz quux"}}}},
},
{
"<html ⚡><link rel=preload as=image href=foo imagesrcset=bar imagesizes=100vw>",
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar"}, {Key: "imagesizes", Val: "100vw"}}}},
},
{
"<html ⚡><link rel=preload as=image href=foo media=print>",
"<html ⚡><head></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "media", Val: "print"}}}},
},
}
for _, tc := range tcs {
t.Run(tc.html, func(t *testing.T) {
output, metadata, err := Process(&rpb.Request{Html: tc.html, Config: rpb.Request_NONE})
if err != nil {
t.Fatalf("unexpected failure: %v", err)
}
if diff := diff.Diff(tc.expectedHTML, output); diff != "" {
t.Errorf("html output differs (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tc.expectedPreloads, metadata.Preloads, cmp.Comparer(proto.Equal)); diff != "" {
t.Errorf("preloads differ (-want +got):\n%s", diff)
}
})
}
}
func TestMaxAge(t *testing.T) {
tcs := []struct {
html string
expectedMaxAgeSecs int32
}{
{
// No amp-scripts; no constraints on signing duration.
"<html ⚡>",
604800,
},
{
// amp-script but not inline; no constraints.
"<html ⚡><amp-script>",
604800,
},
{
// Inline amp-script; default to 1-day duration.
"<html ⚡><amp-script script=foo>",
86400,
},
{
// Inline amp-script with explicit 4-day duration.
"<html ⚡><amp-script script=foo max-age=345600>",
345600,
},
{
// Inline amp-script with explicit 1-year duration; capped at 7 days.
"<html ⚡><amp-script script=foo max-age=31536000>",
604800,
},
{
// Inline amp-script with invalid duration; use default.
"<html ⚡><amp-script script=foo max-age=aaaaaa>",
86400,
},
{
// Inline amp-script with negative duration; use 0.
"<html ⚡><amp-script script=foo max-age=-86400>",
0,
},
{
// Two inline amp-scripts, use min of both.
"<html ⚡><amp-script script=foo max-age=600000><amp-script script=foo max-age=500000>",
500000,
},
{
// Two inline amp-scripts, explicit > implicit.
"<html ⚡><amp-script script=foo max-age=600000><amp-script script=foo>",
86400,
},
{
// Two inline amp-scripts, explicit < implicit.
"<html ⚡><amp-script script=foo max-age=1><amp-script script=foo>",
1,
},
}
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 metadata.MaxAgeSecs != tc.expectedMaxAgeSecs {
t.Errorf("maxAgeSecs differs; got=%d, want=%d", metadata.MaxAgeSecs, tc.expectedMaxAgeSecs)
}
})
}
}
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)
}
})
}
}