|
1 | 1 | import { v8IsolateCodeSandbox } from '../../../src/lib/core/code/v8-isolate-code-sandbox' |
2 | 2 |
|
3 | 3 | const runScript = (script: string): Promise<unknown> => |
4 | | - v8IsolateCodeSandbox.runScript({ script, scriptContext: {} }) |
| 4 | + v8IsolateCodeSandbox.runScript({ script, scriptContext: {} }) |
5 | 5 |
|
6 | 6 | describe('v8IsolateCodeSandbox base64 polyfill', () => { |
7 | | - it('exposes btoa that matches Latin1 base64 encoding', async () => { |
8 | | - const result = await runScript("btoa('hello world')") |
9 | | - expect(result).toBe(Buffer.from('hello world', 'latin1').toString('base64')) |
10 | | - }) |
11 | | - |
12 | | - it('exposes atob that decodes base64 back to the original string', async () => { |
13 | | - const encoded = Buffer.from('hello world', 'latin1').toString('base64') |
14 | | - const result = await runScript(`atob('${encoded}')`) |
15 | | - expect(result).toBe('hello world') |
16 | | - }) |
17 | | - |
18 | | - it('round-trips a value through btoa and atob', async () => { |
19 | | - const result = await runScript("atob(btoa('ServiceNow ticket #42'))") |
20 | | - expect(result).toBe('ServiceNow ticket #42') |
21 | | - }) |
22 | | - |
23 | | - it('decodes a base64-encoded ServiceNow batch response body', async () => { |
24 | | - const body = Buffer.from( |
25 | | - JSON.stringify({ result: { sys_id: 'abc123def456' } }), |
26 | | - 'latin1', |
27 | | - ).toString('base64') |
28 | | - const result = await runScript( |
29 | | - `JSON.parse(atob('${body}')).result.sys_id`, |
30 | | - ) |
31 | | - expect(result).toBe('abc123def456') |
32 | | - }) |
33 | | - |
34 | | - it('throws when btoa receives characters outside the Latin1 range', async () => { |
35 | | - await expect(runScript("btoa('日本語')")).rejects.toThrow(/Latin1 range/) |
36 | | - }) |
| 7 | + it('exposes btoa that matches Latin1 base64 encoding', async () => { |
| 8 | + const result = await runScript("btoa('hello world')") |
| 9 | + expect(result).toBe(Buffer.from('hello world', 'latin1').toString('base64')) |
| 10 | + }) |
| 11 | + |
| 12 | + it('exposes atob that decodes base64 back to the original string', async () => { |
| 13 | + const encoded = Buffer.from('hello world', 'latin1').toString('base64') |
| 14 | + const result = await runScript(`atob('${encoded}')`) |
| 15 | + expect(result).toBe('hello world') |
| 16 | + }) |
| 17 | + |
| 18 | + it('round-trips a value through btoa and atob', async () => { |
| 19 | + const result = await runScript("atob(btoa('ServiceNow ticket #42'))") |
| 20 | + expect(result).toBe('ServiceNow ticket #42') |
| 21 | + }) |
| 22 | + |
| 23 | + it('decodes a base64-encoded ServiceNow batch response body', async () => { |
| 24 | + const body = Buffer.from( |
| 25 | + JSON.stringify({ result: { sys_id: 'abc123def456' } }), |
| 26 | + 'latin1', |
| 27 | + ).toString('base64') |
| 28 | + const result = await runScript( |
| 29 | + `JSON.parse(atob('${body}')).result.sys_id`, |
| 30 | + ) |
| 31 | + expect(result).toBe('abc123def456') |
| 32 | + }) |
| 33 | + |
| 34 | + it('throws when btoa receives characters outside the Latin1 range', async () => { |
| 35 | + await expect(runScript("btoa('日本語')")).rejects.toThrow(/Latin1 range/) |
| 36 | + }) |
| 37 | + |
| 38 | + it('exposes btoaUtf8 that encodes characters outside the Latin1 range', async () => { |
| 39 | + const result = await runScript("btoaUtf8('日本語')") |
| 40 | + expect(result).toBe(Buffer.from('日本語', 'utf8').toString('base64')) |
| 41 | + }) |
| 42 | + |
| 43 | + it('exposes atobUtf8 that decodes utf8 base64 back to the original string', async () => { |
| 44 | + const encoded = Buffer.from('日本語', 'utf8').toString('base64') |
| 45 | + const result = await runScript(`atobUtf8('${encoded}')`) |
| 46 | + expect(result).toBe('日本語') |
| 47 | + }) |
| 48 | + |
| 49 | + it('round-trips a unicode value through btoaUtf8 and atobUtf8', async () => { |
| 50 | + const result = await runScript( |
| 51 | + "atobUtf8(btoaUtf8('Coût € — 日本語 🚀'))", |
| 52 | + ) |
| 53 | + expect(result).toBe('Coût € — 日本語 🚀') |
| 54 | + }) |
| 55 | + |
| 56 | + it('decodes a utf8 base64-encoded ServiceNow batch response body', async () => { |
| 57 | + const body = Buffer.from( |
| 58 | + JSON.stringify({ result: { sys_id: 'Coût-中文-🚀' } }), |
| 59 | + 'utf8', |
| 60 | + ).toString('base64') |
| 61 | + const result = await runScript( |
| 62 | + `JSON.parse(atobUtf8('${body}')).result.sys_id`, |
| 63 | + ) |
| 64 | + expect(result).toBe('Coût-中文-🚀') |
| 65 | + }) |
37 | 66 | }) |
0 commit comments