Skip to content

Commit 5e5d68e

Browse files
bjarklerclydin
authored andcommitted
test: ensure e2e tests serve headers
1 parent bea90a6 commit 5e5d68e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { ng } from '../../utils/process';
10+
import { updateJsonFile } from '../../utils/project';
11+
12+
export default async function () {
13+
// This test ensures that ng e2e serves the HTTP headers that are configured
14+
// in the 'headers' field of the serve options. We do this by serving the
15+
// strictest possible CSP headers (default-src 'none') which blocks loading of
16+
// any resources (including scripts, styles and images) and should cause ng
17+
// e2e to fail with a CSP-related error, which is asserted below.
18+
19+
await updateJsonFile('angular.json', (json) => {
20+
const serve = json['projects']['test-project']['architect']['serve'];
21+
if (!serve['options']) serve['options'] = {};
22+
serve['options']['headers'] = {
23+
'Content-Security-Policy': "default-src 'none'",
24+
};
25+
});
26+
27+
let errorMessage = null;
28+
try {
29+
await ng('e2e');
30+
} catch (error) {
31+
errorMessage = error.message;
32+
}
33+
34+
if (!errorMessage) {
35+
throw new Error(
36+
'Application loaded successfully, indicating that the CSP headers were not served.',
37+
);
38+
}
39+
if (!errorMessage.match(/Refused to load/)) {
40+
throw new Error('Expected to see CSP loading failure in error logs.');
41+
}
42+
}

0 commit comments

Comments
 (0)