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
|
import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { remotePatternToRegex } from '@astrojs/netlify';
import { loadFixture } from '../../../../astro/test/test-utils.js';
describe(
'Image CDN',
() => {
const root = new URL('./fixtures/middleware/', import.meta.url);
describe('when running outside of netlify', () => {
it('does not enable Image CDN', async () => {
const fixture = await loadFixture({ root });
await fixture.build();
const astronautPage = await fixture.readFile('astronaut/index.html');
assert.equal(astronautPage.includes(`src="/_astro/astronaut.`), true);
});
});
describe('when running inside of netlify', () => {
after(() => {
process.env.NETLIFY = undefined;
process.env.DISABLE_IMAGE_CDN = undefined;
});
it('enables Netlify Image CDN', async () => {
process.env.NETLIFY = 'true';
const fixture = await loadFixture({ root });
await fixture.build();
const astronautPage = await fixture.readFile('astronaut/index.html');
assert.equal(astronautPage.includes(`src="/.netlify/image`), true);
});
it('respects image CDN opt-out', async () => {
process.env.NETLIFY = 'true';
process.env.DISABLE_IMAGE_CDN = 'true';
const fixture = await loadFixture({ root });
await fixture.build();
const astronautPage = await fixture.readFile('astronaut/index.html');
assert.equal(astronautPage.includes(`src="/_astro/astronaut.`), true);
});
});
describe('remote image config', () => {
let regexes;
before(async () => {
const fixture = await loadFixture({ root });
await fixture.build();
const config = await fixture.readFile('../.netlify/v1/config.json');
if (config) {
regexes = JSON.parse(config).images.remote_images.map((pattern) => new RegExp(pattern));
}
});
it('generates remote image config patterns', async () => {
assert.equal(regexes?.length, 3);
});
it('generates correct config for domains', async () => {
const domain = regexes[0];
assert.equal(domain.test('https://example.net/image.jpg'), true);
assert.equal(
domain.test('https://www.example.net/image.jpg'),
false,
'subdomain should not match',
);
assert.equal(domain.test('http://example.net/image.jpg'), true, 'http should match');
assert.equal(
domain.test('https://example.net/subdomain/image.jpg'),
true,
'subpath should match',
);
const subdomain = regexes[1];
assert.equal(
subdomain.test('https://secret.example.edu/image.jpg'),
true,
'should match subdomains',
);
assert.equal(
subdomain.test('https://secretxexample.edu/image.jpg'),
false,
'should not use dots in domains as wildcards',
);
});
it('generates correct config for remotePatterns', async () => {
const patterns = regexes[2];
assert.equal(
patterns.test('https://example.org/images/1.jpg'),
true,
'should match domain',
);
assert.equal(
patterns.test('https://www.example.org/images/2.jpg'),
true,
'www subdomain should match',
);
assert.equal(
patterns.test('https://www.subdomain.example.org/images/2.jpg'),
false,
'second level subdomain should not match',
);
assert.equal(
patterns.test('https://example.org/not-images/2.jpg'),
false,
'wrong path should not match',
);
});
it('warns when remotepatterns generates an invalid regex', async (t) => {
const logger = {
warn: t.mock.fn(),
};
const regex = remotePatternToRegex(
{
hostname: '*.examp[le.org',
pathname: '/images/*',
},
logger,
);
assert.strictEqual(regex, undefined);
const calls = logger.warn.mock.calls;
assert.strictEqual(calls.length, 1);
assert.equal(
calls[0].arguments[0],
'Could not generate a valid regex from the remotePattern "{"hostname":"*.examp[le.org","pathname":"/images/*"}". Please check the syntax.',
);
});
});
},
{
timeout: 120000,
},
);
|