-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescapeHTML.test.ts
109 lines (91 loc) · 2.77 KB
/
escapeHTML.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
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
import { expect } from '@std/expect';
import { describe, it } from '@std/testing/bdd';
import { escapeHTML } from './escapeHTML.ts';
describe('escapeHTML', () =>
{
it('should escape weird characters in strings but not mangle HTML entities', () =>
{
expect(escapeHTML('<div>"</div>')).toBe('<div>"</div>');
expect(escapeHTML("Hello & 'World'")).toBe('Hello & 'World'');
expect(escapeHTML('Test "quotes"')).toBe('Test "quotes"');
});
it('should handle empty strings', () =>
{
expect(escapeHTML('')).toBe('');
});
it('should return non-string values unchanged', () =>
{
expect(escapeHTML(42)).toBe(42);
expect(escapeHTML(null)).toBe(null);
expect(escapeHTML(undefined)).toBe(undefined);
expect(escapeHTML(true)).toBe(true);
expect(escapeHTML({ test: 'value' })).toEqual({ test: 'value' });
expect(escapeHTML(['test'])).toEqual(['test']);
});
it('should handle strings with no weird characters', () =>
{
expect(escapeHTML('Hello World')).toBe('Hello World');
expect(escapeHTML('12345')).toBe('12345');
});
it('should escape multiple occurrences of weird characters', () =>
{
expect(escapeHTML('<<>>&&&')).toBe('<<>>&&&');
});
it('should escape & to &', () =>
{
expect(escapeHTML('&')).toBe('&');
});
it('should escape < to <', () =>
{
expect(escapeHTML('<')).toBe('<');
});
it('should escape > to >', () =>
{
expect(escapeHTML('>')).toBe('>');
});
it('should escape " to "', () =>
{
expect(escapeHTML('"')).toBe('"');
});
it("should escape ' to '", () =>
{
expect(escapeHTML("'")).toBe(''');
});
it('should escape a string with multiple weird characters', () =>
{
const unsafeString = `<script>alert('XSS')</script>`;
const escapedString = '<script>alert('XSS')</script>';
expect(escapeHTML(unsafeString)).toBe(escapedString);
});
it('should return the same string if there are no weird characters', () =>
{
const safeString = 'Hello, World!';
expect(escapeHTML(safeString)).toBe(safeString);
});
it('should return an empty string as-is', () =>
{
expect(escapeHTML('')).toBe('');
});
it('should return undefined as-is', () =>
{
expect(escapeHTML(undefined)).toBe(undefined);
});
it('should return null as-is', () =>
{
expect(escapeHTML(null)).toBe(null);
});
it('should return a number as-is', () =>
{
expect(escapeHTML(123)).toBe(123);
});
it('should return an object as-is', () =>
{
const obj = { key: 'value' };
expect(escapeHTML(obj)).toBe(obj);
});
it('should return an array as-is', () =>
{
const arr = [1, 2, 3];
expect(escapeHTML(arr)).toBe(arr);
});
});