Skip to content

Commit 9228f1e

Browse files
committed
test in browser as well as node
1 parent 14636e8 commit 9228f1e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { base64ToUnicode, unicodeToBase64 } from '@sentry/utils';
3+
import { expect } from 'chai';
4+
5+
// See https://tools.ietf.org/html/rfc4648#section-4 for base64 spec
6+
// eslint-disable-next-line no-useless-escape
7+
const BASE64_REGEX = /([a-zA-Z0-9+/]{4})*(|([a-zA-Z0-9+/]{3}=)|([a-zA-Z0-9+/]{2}==))/;
8+
9+
// NOTE: These tests are copied (and adapted for chai syntax) from `string.test.ts` in `@sentry/utils`. The
10+
// base64-conversion functions have a different implementation in browser and node, so they're copied here to prove they
11+
// work in a real live browser. If you make changes here, make sure to also port them over to that copy.
12+
describe('base64ToUnicode/unicodeToBase64', () => {
13+
const unicodeString = 'Dogs are great!';
14+
const base64String = 'RG9ncyBhcmUgZ3JlYXQh';
15+
16+
it('converts to valid base64', () => {
17+
expect(BASE64_REGEX.test(unicodeToBase64(unicodeString))).to.be.true;
18+
});
19+
20+
it('works as expected', () => {
21+
expect(unicodeToBase64(unicodeString)).to.equal(base64String);
22+
expect(base64ToUnicode(base64String)).to.equal(unicodeString);
23+
});
24+
25+
it('conversion functions are inverses', () => {
26+
expect(base64ToUnicode(unicodeToBase64(unicodeString))).to.equal(unicodeString);
27+
expect(unicodeToBase64(base64ToUnicode(base64String))).to.equal(base64String);
28+
});
29+
30+
it('can handle and preserve multi-byte characters in original string', () => {
31+
['🐶', 'Καλό κορίτσι, Μάιζεϊ!', 'Of margir hundar! Ég geri ráð fyrir að ég þurfi stærra rúm.'].forEach(orig => {
32+
expect(() => {
33+
unicodeToBase64(orig);
34+
}).not.to.throw;
35+
expect(base64ToUnicode(unicodeToBase64(orig))).to.equal(orig);
36+
});
37+
});
38+
39+
it('throws an error when given invalid input', () => {
40+
expect(() => {
41+
unicodeToBase64(null as any);
42+
}).to.throw('Unable to convert to base64');
43+
expect(() => {
44+
unicodeToBase64(undefined as any);
45+
}).to.throw('Unable to convert to base64');
46+
expect(() => {
47+
unicodeToBase64({} as any);
48+
}).to.throw('Unable to convert to base64');
49+
50+
expect(() => {
51+
base64ToUnicode(null as any);
52+
}).to.throw('Unable to convert from base64');
53+
expect(() => {
54+
base64ToUnicode(undefined as any);
55+
}).to.throw('Unable to convert from base64');
56+
expect(() => {
57+
base64ToUnicode({} as any);
58+
}).to.throw('Unable to convert from base64');
59+
60+
// Note that by design, in node base64 encoding and decoding will accept any string, whether or not it's valid
61+
// base64, by ignoring all invalid characters, including whitespace. Therefore, no wacky strings have been included
62+
// here because they don't actually error.
63+
});
64+
});

packages/utils/test/string.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ describe('isMatchingPattern()', () => {
5050
});
5151
});
5252

53+
// NOTE: These tests are copied (and adapted for chai syntax) to `string.test.ts` in `@sentry/browser`. The
54+
// base64-conversion functions have a different implementation in browser and node, so they're copied there to prove
55+
// they work in a real live browser. If you make changes here, make sure to also port them over to that copy.
5356
describe('base64ToUnicode/unicodeToBase64', () => {
5457
const unicodeString = 'Dogs are great!';
5558
const base64String = 'RG9ncyBhcmUgZ3JlYXQh';

0 commit comments

Comments
 (0)