|
1 | 1 | const expect = require('chai').expect, |
2 | | - |
3 | 2 | encode = require('../../').encode, |
4 | 3 | percentEncodeCharCode = require('../../encoder').percentEncodeCharCode; |
5 | 4 |
|
6 | 5 | describe('.encode', function () { |
7 | | - it('should percent-encode C0 control codes', function () { |
8 | | - var i, |
9 | | - char; |
| 6 | + describe('with TextEncoder', function () { |
| 7 | + it('should percent-encode C0 control codes', function () { |
| 8 | + var i, |
| 9 | + char; |
10 | 10 |
|
11 | | - for (i = 0; i < 32; i++) { |
12 | | - char = String.fromCharCode(i); |
13 | | - expect(encode(char)).to.equal(percentEncodeCharCode(i)); |
14 | | - } |
| 11 | + for (i = 0; i < 32; i++) { |
| 12 | + char = String.fromCharCode(i); |
| 13 | + expect(encode(char)).to.equal(percentEncodeCharCode(i)); |
| 14 | + } |
15 | 15 |
|
16 | | - char = String.fromCharCode(127); |
17 | | - expect(encode(char)).to.equal(percentEncodeCharCode(127)); |
18 | | - }); |
| 16 | + char = String.fromCharCode(127); |
| 17 | + expect(encode(char)).to.equal(percentEncodeCharCode(127)); |
| 18 | + }); |
19 | 19 |
|
20 | | - it('should percent-encode SPACE, ("), (#), (\'), (<), and (>)', function () { |
21 | | - var i, |
22 | | - char, |
23 | | - encoded, |
24 | | - chars = [], |
25 | | - expected = [' ', '"', '#', '\'', '<', '>']; |
| 20 | + it('should percent-encode SPACE, ("), (#), (\'), (<), and (>)', function () { |
| 21 | + var i, |
| 22 | + char, |
| 23 | + encoded, |
| 24 | + chars = [], |
| 25 | + expected = [' ', '"', '#', '\'', '<', '>']; |
26 | 26 |
|
27 | | - for (i = 32; i < 127; i++) { |
28 | | - char = String.fromCharCode(i); |
29 | | - encoded = encode(char); |
| 27 | + for (i = 32; i < 127; i++) { |
| 28 | + char = String.fromCharCode(i); |
| 29 | + encoded = encode(char); |
30 | 30 |
|
31 | | - if (char !== encoded) { |
32 | | - chars.push(char); |
33 | | - expect(encoded).to.equal(percentEncodeCharCode(i)); |
| 31 | + if (char !== encoded) { |
| 32 | + chars.push(char); |
| 33 | + expect(encoded).to.equal(percentEncodeCharCode(i)); |
| 34 | + } |
34 | 35 | } |
35 | | - } |
36 | 36 |
|
37 | | - expect(chars).to.eql(expected); |
38 | | - }); |
| 37 | + expect(chars).to.eql(expected); |
| 38 | + }); |
39 | 39 |
|
40 | | - it('should percent-encode unicode characters', function () { |
41 | | - expect(encode('𝌆й你ス')).to.eql('%F0%9D%8C%86%D0%B9%E4%BD%A0%E3%82%B9'); |
42 | | - }); |
| 40 | + it('should percent-encode unicode characters', function () { |
| 41 | + expect(encode('𝌆й你ス')).to.eql('%F0%9D%8C%86%D0%B9%E4%BD%A0%E3%82%B9'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should percent-encode 4-byte unicode characters', function () { |
| 45 | + expect(encode('𝌆')).to.eql('%F0%9D%8C%86'); |
| 46 | + }); |
43 | 47 |
|
44 | | - it('should not double encode characters', function () { |
45 | | - expect(encode('key:%F0%9F%8D%AA')).to.equal('key:%F0%9F%8D%AA'); |
| 48 | + it('should handle unpaired surrogates', function () { |
| 49 | + expect(encode('\uD800')).to.eql('%EF%BF%BD'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should not double encode characters', function () { |
| 53 | + expect(encode('key:%F0%9F%8D%AA')).to.equal('key:%F0%9F%8D%AA'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return empty string on invalid input types', function () { |
| 57 | + expect(encode()).to.equal(''); |
| 58 | + expect(encode(null)).to.equal(''); |
| 59 | + expect(encode(undefined)).to.equal(''); |
| 60 | + expect(encode(NaN)).to.equal(''); |
| 61 | + expect(encode(true)).to.equal(''); |
| 62 | + expect(encode(1234)).to.equal(''); |
| 63 | + expect(encode(Function)).to.equal(''); |
| 64 | + expect(encode(['key', 'value'])).to.equal(''); |
| 65 | + }); |
46 | 66 | }); |
47 | 67 |
|
48 | | - it('should return empty string on invalid input types', function () { |
49 | | - expect(encode()).to.equal(''); |
50 | | - expect(encode(null)).to.equal(''); |
51 | | - expect(encode(undefined)).to.equal(''); |
52 | | - expect(encode(NaN)).to.equal(''); |
53 | | - expect(encode(true)).to.equal(''); |
54 | | - expect(encode(1234)).to.equal(''); |
55 | | - expect(encode(Function)).to.equal(''); |
56 | | - expect(encode(['key', 'value'])).to.equal(''); |
| 68 | + describe('without TextEncoder', function () { |
| 69 | + let oldTextEncoder; |
| 70 | + |
| 71 | + before(function () { |
| 72 | + oldTextEncoder = global.TextEncoder; |
| 73 | + global.TextEncoder = undefined; |
| 74 | + }); |
| 75 | + |
| 76 | + after(function () { |
| 77 | + global.TextEncoder = oldTextEncoder; |
| 78 | + }); |
| 79 | + |
| 80 | + it('should percent-encode C0 control codes', function () { |
| 81 | + var i, |
| 82 | + char; |
| 83 | + |
| 84 | + for (i = 0; i < 32; i++) { |
| 85 | + char = String.fromCharCode(i); |
| 86 | + expect(encode(char)).to.equal(percentEncodeCharCode(i)); |
| 87 | + } |
| 88 | + |
| 89 | + char = String.fromCharCode(127); |
| 90 | + expect(encode(char)).to.equal(percentEncodeCharCode(127)); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should percent-encode SPACE, ("), (#), (\'), (<), and (>)', function () { |
| 94 | + var i, |
| 95 | + char, |
| 96 | + encoded, |
| 97 | + chars = [], |
| 98 | + expected = [' ', '"', '#', '\'', '<', '>']; |
| 99 | + |
| 100 | + for (i = 32; i < 127; i++) { |
| 101 | + char = String.fromCharCode(i); |
| 102 | + encoded = encode(char); |
| 103 | + |
| 104 | + if (char !== encoded) { |
| 105 | + chars.push(char); |
| 106 | + expect(encoded).to.equal(percentEncodeCharCode(i)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + expect(chars).to.eql(expected); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should percent-encode unicode characters', function () { |
| 114 | + expect(encode('𝌆й你ス')).to.eql('%F0%9D%8C%86%D0%B9%E4%BD%A0%E3%82%B9'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should percent-encode 4-byte unicode characters', function () { |
| 118 | + expect(encode('𝌆')).to.eql('%F0%9D%8C%86'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle unpaired surrogates', function () { |
| 122 | + expect(encode('\uD800')).to.eql('%EF%BF%BD'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should not double encode characters', function () { |
| 126 | + expect(encode('key:%F0%9F%8D%AA')).to.equal('key:%F0%9F%8D%AA'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should return empty string on invalid input types', function () { |
| 130 | + expect(encode()).to.equal(''); |
| 131 | + expect(encode(null)).to.equal(''); |
| 132 | + expect(encode(undefined)).to.equal(''); |
| 133 | + expect(encode(NaN)).to.equal(''); |
| 134 | + expect(encode(true)).to.equal(''); |
| 135 | + expect(encode(1234)).to.equal(''); |
| 136 | + expect(encode(Function)).to.equal(''); |
| 137 | + expect(encode(['key', 'value'])).to.equal(''); |
| 138 | + }); |
57 | 139 | }); |
58 | 140 | }); |
0 commit comments