forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-engine_spec.ts
More file actions
447 lines (390 loc) · 15.7 KB
/
app-engine_spec.ts
File metadata and controls
447 lines (390 loc) · 15.7 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
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
// The compiler is needed as tests are in JIT.
/* eslint-disable import/no-unassigned-import */
import '@angular/compiler';
/* eslint-enable import/no-unassigned-import */
import { Component, REQUEST, inject } from '@angular/core';
import { destroyAngularServerApp, getOrCreateAngularServerApp } from '../src/app';
import { AngularAppEngine } from '../src/app-engine';
import { setAngularAppEngineManifest } from '../src/manifest';
import { RenderMode } from '../src/routes/route-config';
import { setAngularAppTestingManifest } from './testing-utils';
@Component({
selector: 'app-home',
template: 'Home works',
})
class TestHomeComponent {
private request = inject(REQUEST);
constructor() {
// Force header access to trigger validation
this.request?.headers.get('host');
this.request?.headers.get('x-forwarded-host');
}
}
function createEntryPoint(locale: string) {
return async () => {
@Component({
selector: `app-home-${locale}`,
template: `Home works ${locale.toUpperCase()}`,
})
class HomeComponent {}
@Component({
selector: `app-ssr-${locale}`,
template: `SSR works ${locale.toUpperCase()}`,
})
class SSRComponent {}
@Component({
selector: `app-ssg-${locale}`,
template: `SSG works ${locale.toUpperCase()}`,
})
class SSGComponent {}
setAngularAppTestingManifest(
[
{ path: 'ssg', component: SSGComponent },
{ path: 'ssr', component: SSRComponent },
{ path: '', component: HomeComponent },
],
[
{ path: 'ssg', renderMode: RenderMode.Prerender },
{ path: '**', renderMode: RenderMode.Server },
],
'/' + locale,
{
'ssg/index.html': {
size: 25,
hash: 'f799132d0a09e0fef93c68a12e443527700eb59e6f67fcb7854c3a60ff082fde',
text: async () => `<html>
<head>
<title>SSG page</title>
<base href="/${locale}" />
</head>
<body>
SSG works ${locale.toUpperCase()}
</body>
</html>
`,
},
},
locale,
);
return {
ɵgetOrCreateAngularServerApp: getOrCreateAngularServerApp,
ɵdestroyAngularServerApp: destroyAngularServerApp,
};
};
}
describe('AngularAppEngine', () => {
let appEngine: AngularAppEngine;
describe('Localized app', () => {
beforeAll(() => {
setAngularAppEngineManifest({
allowedHosts: ['example.com'],
// Note: Although we are testing only one locale, we need to configure two or more
// to ensure that we test a different code path.
entryPoints: {
it: createEntryPoint('it'),
en: createEntryPoint('en'),
},
supportedLocales: { 'it': 'it', 'en': 'en' },
basePath: '/',
});
appEngine = new AngularAppEngine();
});
describe('handle', () => {
it('should return null for requests to unknown pages', async () => {
const request = new Request('https://example.com/unknown/page');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
it('should return null for requests with unknown locales', async () => {
const request = new Request('https://example.com/es/ssr');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
it('should return a rendered page with correct locale', async () => {
const request = new Request('https://example.com/it/ssr');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSR works IT');
});
it('should correctly render the content when the URL ends with "index.html" with correct locale', async () => {
const request = new Request('https://example.com/it/ssr/index.html');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSR works IT');
expect(response?.headers?.get('Content-Language')).toBe('it');
});
it('should return a serve prerendered page with correct locale', async () => {
const request = new Request('https://example.com/it/ssg');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSG works IT');
expect(response?.headers?.get('Content-Language')).toBe('it');
});
it('should correctly serve the prerendered content when the URL ends with "index.html" with correct locale', async () => {
const request = new Request('https://example.com/it/ssg/index.html');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSG works IT');
});
it('should return null for requests to unknown pages in a locale', async () => {
const request = new Request('https://example.com/it/unknown/page');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
it('should redirect to the highest priority locale when the URL is "/"', async () => {
const request = new Request('https://example.com', {
headers: { 'Accept-Language': 'fr-CH, fr;q=0.9, it;q=0.8, en;q=0.7, *;q=0.5' },
});
const response = await appEngine.handle(request);
expect(response?.status).toBe(302);
expect(response?.headers.get('Location')).toBe('/it');
expect(response?.headers.get('Vary')).toBe('X-Forwarded-Prefix, Accept-Language');
});
it('should include forwarded prefix in locale redirect location when present', async () => {
const request = new Request('https://example.com', {
headers: {
'Accept-Language': 'it',
'X-Forwarded-Prefix': '/app',
},
});
const response = await appEngine.handle(request);
expect(response?.status).toBe(302);
expect(response?.headers.get('Location')).toBe('/app/it');
expect(response?.headers.get('Vary')).toBe('X-Forwarded-Prefix, Accept-Language');
});
it('should return null for requests to file-like resources in a locale', async () => {
const request = new Request('https://example.com/it/logo.png');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
});
});
describe('Localized app with single locale', () => {
beforeAll(() => {
setAngularAppEngineManifest({
allowedHosts: ['example.com'],
entryPoints: {
it: createEntryPoint('it'),
},
supportedLocales: { 'it': 'it' },
basePath: '/',
});
appEngine = new AngularAppEngine();
});
describe('handle', () => {
it('should return null for requests to unknown pages', async () => {
const request = new Request('https://example.com/unknown/page');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
it('should return a rendered page with correct locale', async () => {
const request = new Request('https://example.com/it/ssr');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSR works IT');
});
it('should correctly render the content when the URL ends with "index.html" with correct locale', async () => {
const request = new Request('https://example.com/it/ssr/index.html');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSR works IT');
expect(response?.headers?.get('Content-Language')).toBe('it');
});
it('should return a serve prerendered page with correct locale', async () => {
const request = new Request('https://example.com/it/ssg');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSG works IT');
expect(response?.headers?.get('Content-Language')).toBe('it');
});
it('should correctly serve the prerendered content when the URL ends with "index.html" with correct locale', async () => {
const request = new Request('https://example.com/it/ssg/index.html');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('SSG works IT');
});
it('should return null for requests to unknown pages in a locale', async () => {
const request = new Request('https://example.com/it/unknown/page');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
it('should return null for requests to file-like resources in a locale', async () => {
const request = new Request('https://example.com/it/logo.png');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
});
});
describe('Non-localized app', () => {
beforeAll(() => {
@Component({
selector: 'app-home',
template: `Home works`,
})
class HomeComponent {}
setAngularAppEngineManifest({
allowedHosts: ['example.com'],
entryPoints: {
'': async () => {
setAngularAppTestingManifest(
[{ path: 'home', component: HomeComponent }],
[{ path: '**', renderMode: RenderMode.Server }],
);
return {
ɵgetOrCreateAngularServerApp: getOrCreateAngularServerApp,
ɵdestroyAngularServerApp: destroyAngularServerApp,
};
},
},
basePath: '/',
supportedLocales: { 'en-US': '' },
});
appEngine = new AngularAppEngine();
});
it('should return null for requests to file-like resources', async () => {
const request = new Request('https://example.com/logo.png');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
it('should return null for requests to unknown pages', async () => {
const request = new Request('https://example.com/unknown/page');
const response = await appEngine.handle(request);
expect(response).toBeNull();
});
it('should return a rendered page for known paths', async () => {
const request = new Request('https://example.com/home');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('Home works');
});
it('should correctly render the content when the URL ends with "index.html"', async () => {
const request = new Request('https://example.com/home/index.html');
const response = await appEngine.handle(request);
expect(await response?.text()).toContain('Home works');
});
});
describe('Invalid host headers', () => {
let consoleErrorSpy: jasmine.Spy;
beforeAll(() => {
setAngularAppEngineManifest({
allowedHosts: ['example.com'],
entryPoints: {
'': async () => {
setAngularAppTestingManifest(
[{ path: 'home', component: TestHomeComponent }],
[{ path: '**', renderMode: RenderMode.Server }],
);
return {
ɵgetOrCreateAngularServerApp: getOrCreateAngularServerApp,
ɵdestroyAngularServerApp: destroyAngularServerApp,
};
},
},
basePath: '/',
supportedLocales: { 'en-US': '' },
});
appEngine = new AngularAppEngine();
});
beforeEach(() => {
consoleErrorSpy = spyOn(console, 'error');
});
it('should return 400 when disallowed host', async () => {
const request = new Request('https://evil.com');
const response = await appEngine.handle(request);
expect(response).not.toBeNull();
expect(response?.status).toBe(400);
expect(await response?.text()).toContain('URL with hostname "evil.com" is not allowed.');
expect(consoleErrorSpy).toHaveBeenCalledWith(
jasmine.stringMatching('URL with hostname "evil.com" is not allowed.'),
);
});
it('should return 400 when disallowed host header', async () => {
const request = new Request('https://example.com/home', {
headers: { 'host': 'evil.com' },
});
const response = await appEngine.handle(request);
expect(response).not.toBeNull();
expect(response?.status).toBe(400);
expect(await response?.text()).toContain(
'Header "host" with value "evil.com" is not allowed.',
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
jasmine.stringMatching('Header "host" with value "evil.com" is not allowed.'),
);
});
it('should return 400 when disallowed x-forwarded-host header', async () => {
const request = new Request('https://example.com/home', {
headers: { 'x-forwarded-host': 'evil.com' },
});
const response = await appEngine.handle(request);
expect(response).not.toBeNull();
expect(response?.status).toBe(400);
expect(await response?.text()).toContain(
'Header "x-forwarded-host" with value "evil.com" is not allowed.',
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
jasmine.stringMatching('Header "x-forwarded-host" with value "evil.com" is not allowed.'),
);
});
it('should return 400 when host with path separator', async () => {
const request = new Request('https://example.com/home', {
headers: { 'host': 'example.com/evil' },
});
const response = await appEngine.handle(request);
expect(response).not.toBeNull();
expect(response?.status).toBe(400);
expect(await response?.text()).toContain(
'Header "host" contains characters that are not allowed.',
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
jasmine.stringMatching('Header "host" contains characters that are not allowed.'),
);
});
});
describe('Disable host check', () => {
let consoleErrorSpy: jasmine.Spy;
beforeAll(() => {
setAngularAppEngineManifest({
allowedHosts: ['example.com'],
entryPoints: {
'': async () => {
setAngularAppTestingManifest(
[{ path: 'home', component: TestHomeComponent }],
[{ path: '**', renderMode: RenderMode.Server }],
);
return {
ɵgetOrCreateAngularServerApp: getOrCreateAngularServerApp,
ɵdestroyAngularServerApp: destroyAngularServerApp,
};
},
},
basePath: '/',
supportedLocales: { 'en-US': '' },
});
appEngine = new AngularAppEngine();
AngularAppEngine.ɵdisableAllowedHostsCheck = true;
});
afterAll(() => {
AngularAppEngine.ɵdisableAllowedHostsCheck = false;
});
beforeEach(() => {
consoleErrorSpy = spyOn(console, 'error');
});
it('should allow requests to disallowed hosts', async () => {
const request = new Request('https://evil.com/home');
const response = await appEngine.handle(request);
expect(response).toBeDefined();
expect(response?.status).toBe(200);
expect(await response?.text()).toContain('Home works');
expect(consoleErrorSpy).not.toHaveBeenCalled();
});
it('should allow requests with disallowed host header', async () => {
const request = new Request('https://example.com/home', {
headers: { 'host': 'evil.com' },
});
const response = await appEngine.handle(request);
expect(response).toBeDefined();
expect(response?.status).toBe(200);
expect(await response?.text()).toContain('Home works');
expect(consoleErrorSpy).not.toHaveBeenCalled();
});
});
});