|
1 | 1 | import { assert } from "chai"; |
2 | | -import { LocalStorageCache } from "../lib/Cache"; |
| 2 | +import { LogLevel } from "configcat-common"; |
| 3 | +import { LocalStorageCache, fromUtf8Base64, getLocalStorage, toUtf8Base64 } from "../src/Cache"; |
| 4 | +import { FakeLogger } from "./helpers/fakes"; |
| 5 | +import { createClientWithLazyLoad } from "./helpers/utils"; |
3 | 6 |
|
| 7 | +describe("Base64 encode/decode test", () => { |
| 8 | + let allBmpChars = ""; |
| 9 | + for (let i = 0; i <= 0xFFFF; i++) { |
| 10 | + if (i < 0xD800 || 0xDFFF < i) { // skip lone surrogate chars |
| 11 | + allBmpChars += String.fromCharCode(i); |
| 12 | + } |
| 13 | + } |
4 | 14 |
|
5 | | -describe("LocalStorageCache cache tests", () => { |
6 | | - it("LocalStorageCache works with non latin 1 characters", () => { |
7 | | - const cache = new LocalStorageCache(); |
8 | | - const key = "testkey"; |
9 | | - const text = "äöüÄÖÜçéèñışğ⢙✓😀"; |
10 | | - cache.set(key, text); |
11 | | - const retrievedValue = cache.get(key); |
12 | | - assert.strictEqual(retrievedValue, text); |
| 15 | + for (const input of [ |
| 16 | + "", |
| 17 | + "\n", |
| 18 | + "äöüÄÖÜçéèñışğ⢙✓😀", |
| 19 | + allBmpChars |
| 20 | + ]) { |
| 21 | + it(`Base64 encode/decode works - input: ${input.slice(0, Math.min(input.length, 128))}`, () => { |
| 22 | + assert.strictEqual(fromUtf8Base64(toUtf8Base64(input)), input); |
13 | 23 | }); |
| 24 | + } |
| 25 | +}); |
| 26 | + |
| 27 | +describe("LocalStorageCache cache tests", () => { |
| 28 | + it("LocalStorageCache works with non latin 1 characters", () => { |
| 29 | + const localStorage = getLocalStorage(); |
| 30 | + assert.isNotNull(localStorage); |
| 31 | + |
| 32 | + const cache = new LocalStorageCache(localStorage!); |
| 33 | + const key = "testkey"; |
| 34 | + const text = "äöüÄÖÜçéèñışğ⢙✓😀"; |
| 35 | + cache.set(key, text); |
| 36 | + const retrievedValue = cache.get(key); |
| 37 | + assert.strictEqual(retrievedValue, text); |
| 38 | + assert.strictEqual(window.localStorage.getItem(key), "w6TDtsO8w4TDlsOcw6fDqcOow7HEscWfxJ/DosKi4oSi4pyT8J+YgA=="); |
| 39 | + }); |
| 40 | + |
| 41 | + it("Error is logged when LocalStorageCache.get throws", async () => { |
| 42 | + const errorMessage = "Something went wrong."; |
| 43 | + const faultyLocalStorage: Storage = { |
| 44 | + get length() { return 0; }, |
| 45 | + clear() { }, |
| 46 | + getItem() { throw Error(errorMessage); }, |
| 47 | + setItem() { }, |
| 48 | + removeItem() { }, |
| 49 | + key() { return null; } |
| 50 | + }; |
| 51 | + |
| 52 | + const fakeLogger = new FakeLogger(); |
| 53 | + |
| 54 | + const client = createClientWithLazyLoad("configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/AG6C1ngVb0CvM07un6JisQ", { logger: fakeLogger }, |
| 55 | + kernel => LocalStorageCache.setup(kernel, () => faultyLocalStorage)); |
| 56 | + |
| 57 | + try { await client.getValueAsync("stringDefaultCat", ""); } |
| 58 | + finally { client.dispose(); } |
| 59 | + |
| 60 | + assert.isDefined(fakeLogger.events.find(([level, eventId, , err]) => level === LogLevel.Error && eventId === 2200 && err instanceof Error && err.message === errorMessage)); |
| 61 | + }); |
| 62 | + |
| 63 | + it("Error is logged when LocalStorageCache.set throws", async () => { |
| 64 | + const errorMessage = "Something went wrong."; |
| 65 | + const faultyLocalStorage: Storage = { |
| 66 | + get length() { return 0; }, |
| 67 | + clear() { }, |
| 68 | + getItem() { return null; }, |
| 69 | + setItem() { throw Error(errorMessage); }, |
| 70 | + removeItem() { }, |
| 71 | + key() { return null; } |
| 72 | + }; |
| 73 | + |
| 74 | + const fakeLogger = new FakeLogger(); |
| 75 | + |
| 76 | + const client = createClientWithLazyLoad("configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/AG6C1ngVb0CvM07un6JisQ", { logger: fakeLogger }, |
| 77 | + kernel => LocalStorageCache.setup(kernel, () => faultyLocalStorage)); |
| 78 | + |
| 79 | + try { await client.getValueAsync("stringDefaultCat", ""); } |
| 80 | + finally { client.dispose(); } |
| 81 | + |
| 82 | + assert.isDefined(fakeLogger.events.find(([level, eventId, , err]) => level === LogLevel.Error && eventId === 2201 && err instanceof Error && err.message === errorMessage)); |
| 83 | + }); |
14 | 84 | }); |
0 commit comments