-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerender_resource_test.mts
112 lines (92 loc) · 4.43 KB
/
prerender_resource_test.mts
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
import { safe, SafeHtml } from '../safe_html/safe_html.mjs';
import { PrerenderResource } from './prerender_resource.mjs';
describe('PrerenderResource', () => {
describe('of()', () => {
it('returns a `PrerenderResource` from `SafeHtml` data', () => {
const res = PrerenderResource.fromHtml(
'/foo/bar.html', safe`<div></div>`);
expect(res.path).toBe('/foo/bar.html');
expect(new TextDecoder().decode(res.contents)).toBe('<div></div>');
});
it('throws when given non-`SafeHtml` input', () => {
expect(() => PrerenderResource.fromHtml(
'/foo/bar.html',
'unsafe HTML content' as unknown as SafeHtml,
)).toThrowError(/Only `SafeHtml` objects can be used in `\*.html` or `\*.htm` files\./);
expect(() => PrerenderResource.fromHtml(
'/foo/bar.html',
{ getHtmlAsString: () => 'unsafe HTML content' } as SafeHtml,
)).toThrowError(/Only `SafeHtml` objects can be used in `\*.html` or `\*.htm` files\./);
});
it('throws when given an invalid URL path', () => {
expect(() => PrerenderResource.fromHtml(
'does/not/start/with/a/slash.ext',
safe`Hello, World!`,
)).toThrowError(/must start with a "\/"/);
});
});
describe('fromText()', () => {
it('returns a `PrerenderResource` from string data', () => {
const res = PrerenderResource.fromText(
'/foo/bar.txt', 'Hello World!');
expect(res.path).toBe('/foo/bar.txt');
expect(new TextDecoder().decode(res.contents)).toBe('Hello World!');
});
it('returns a `PrerenderResource` with input string re-encoded as UTF-8', () => {
// The `¢` character is `\xc2\x00\xa2\x00` in UTF-16, which is used
// for in-memory strings in JS. We expect this to be converted to
// `\xc2\xa2` (the UTF-8 equivalent), when stored in the `Buffer`.
const res = PrerenderResource.fromText('/foo/bar.txt', '¢');
const contents = new Uint8Array(res.contents);
expect(contents.length).toBe(2);
expect(contents[0]).toBe(0xc2);
expect(contents[1]).toBe(0xa2);
});
it('throws when given an invalid URL path', () => {
expect(() => PrerenderResource.fromText(
'does/not/start/with/a/slash.ext',
'Hello, World!',
)).toThrowError(/must start with a "\/"/);
});
it('throws when given an HTML path', () => {
expect(() => PrerenderResource.fromText(
'/index.html', 'Hello, World!',
)).toThrowError(/this would be unsafe/);
expect(() => PrerenderResource.fromText(
'/index.htm', 'Hello, World!',
)).toThrowError(/this would be unsafe/);
});
});
describe('fromBinary()', () => {
it('returns a `PrerenderResource` from a `TypedArray`', () => {
const res = PrerenderResource.fromBinary(
'/foo/bar.bin', new Uint8Array([ 0, 1, 2, 3 ]));
expect(res.path).toBe('/foo/bar.bin');
const contents = new Uint8Array(res.contents);
expect(contents.length).toBe(4);
expect(contents[0]).toBe(0);
expect(contents[1]).toBe(1);
expect(contents[2]).toBe(2);
expect(contents[3]).toBe(3);
});
it('returns a `PrerenderResource` from an `ArrayBuffer`', () => {
const res = PrerenderResource.fromBinary(
'/foo/bar.bin', new Uint8Array([ 0, 1, 2, 3 ]).buffer);
expect(res.path).toBe('/foo/bar.bin');
const contents = new Uint8Array(res.contents);
expect(contents.length).toBe(4);
expect(contents[0]).toBe(0);
expect(contents[1]).toBe(1);
expect(contents[2]).toBe(2);
expect(contents[3]).toBe(3);
});
it('throws an error when used with an HTML path', () => {
expect(() => PrerenderResource.fromBinary(
'/index.html', new Uint8Array([ 0, 1, 2, 3 ]),
)).toThrowError(/this would be unsafe/);
expect(() => PrerenderResource.fromBinary(
'/index.htm', new Uint8Array([ 0, 1, 2, 3 ]),
)).toThrowError(/this would be unsafe/);
});
});
});