-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsanitize.test.ts
43 lines (42 loc) · 1.29 KB
/
sanitize.test.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
import { sanitizeString, sanitizeUrl, getSafeSearchParam } from '../src';
describe('Test Sanitize String', () => {
let windowSpy: any;
beforeEach(() => {
windowSpy = jest.spyOn(window, 'window', 'get');
});
afterEach(() => {
windowSpy.mockRestore();
});
it('Test Normal String', () => {
expect(
sanitizeString(
"https://sharechat.com/?lang=<script>{alert('Hello World')}</script>"
)
).toEqual('httpssharechatcomlangscriptalertHello Worldscript');
});
it('Test URL String', () => {
expect(
sanitizeUrl(
"https://sharechat.com/?lang=<script>{alert('Hello World')}</script>"
)
).toEqual('https://sharechat.com/?lang=scriptalertHello World/script');
});
it('Test URL search parameter without attack', () => {
windowSpy.mockImplementation(() => ({
location: {
origin: 'https://sharechat.com',
search: '?answer=yes',
},
}));
expect(getSafeSearchParam('answer')).toEqual('yes');
});
it('Test URL search parameter with attack', () => {
windowSpy.mockImplementation(() => ({
location: {
origin: 'https://sharechat.com',
search: '?answer=<script>alert("Hello")</script>',
},
}));
expect(getSafeSearchParam('answer')).toEqual('scriptalertHelloscript');
});
});