-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathstyle-nonce_spec.ts
76 lines (67 loc) · 2.16 KB
/
style-nonce_spec.ts
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
/**
* @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.io/license
*/
import { addStyleNonce } from './style-nonce';
describe('add-style-nonce', () => {
it('should add the nonce expression to all inline style tags', async () => {
const result = await addStyleNonce(`
<html>
<head>
<style>.a {color: red;}</style>
<style>.b {color: blue;}</style>
</head>
<body>
<app ngCspNonce="{% nonce %}"></app>
</body>
</html>
`);
expect(result).toContain('<style nonce="{% nonce %}">.a {color: red;}</style>');
expect(result).toContain('<style nonce="{% nonce %}">.b {color: blue;}</style>');
});
it('should add a lowercase nonce expression to style tags', async () => {
const result = await addStyleNonce(`
<html>
<head>
<style>.a {color: red;}</style>
</head>
<body>
<app ngcspnonce="{% nonce %}"></app>
</body>
</html>
`);
expect(result).toContain('<style nonce="{% nonce %}">.a {color: red;}</style>');
});
it('should preserve any pre-existing nonces', async () => {
const result = await addStyleNonce(`
<html>
<head>
<style>.a {color: red;}</style>
<style nonce="{% otherNonce %}">.b {color: blue;}</style>
</head>
<body>
<app ngCspNonce="{% nonce %}"></app>
</body>
</html>
`);
expect(result).toContain('<style nonce="{% nonce %}">.a {color: red;}</style>');
expect(result).toContain('<style nonce="{% otherNonce %}">.b {color: blue;}</style>');
});
it('should use the first nonce that is defined on the page', async () => {
const result = await addStyleNonce(`
<html>
<head>
<style>.a {color: red;}</style>
</head>
<body>
<app ngCspNonce="{% nonce %}"></app>
<other-app ngCspNonce="{% otherNonce %}"></other-app>
</body>
</html>
`);
expect(result).toContain('<style nonce="{% nonce %}">.a {color: red;}</style>');
});
});