-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbasic.test.ts
More file actions
42 lines (36 loc) · 1.61 KB
/
basic.test.ts
File metadata and controls
42 lines (36 loc) · 1.61 KB
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
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {expect} from 'chai';
import {BasicAuthStrategy} from '@salesforce/b2c-tooling-sdk/auth';
describe('auth/basic', () => {
describe('BasicAuthStrategy', () => {
it('getAuthorizationHeader returns a Basic header with base64 encoded user:pass', async () => {
const auth = new BasicAuthStrategy('user', 'pass');
const header = await auth.getAuthorizationHeader();
expect(header).to.equal(`Basic ${Buffer.from('user:pass').toString('base64')}`);
});
it('fetch injects Authorization header and preserves existing headers', async () => {
const originalFetch = globalThis.fetch;
try {
let seenHeader: string | null = null;
let seenCustom: string | null = null;
globalThis.fetch = (async (_url: string | URL, init?: RequestInit) => {
const headers = new Headers(init?.headers);
seenHeader = headers.get('Authorization');
seenCustom = headers.get('x-custom');
return new Response('ok', {status: 200});
}) as typeof fetch;
const auth = new BasicAuthStrategy('user', 'pass');
const res = await auth.fetch('https://example.com', {headers: {'x-custom': '1'}});
expect(res.status).to.equal(200);
expect(seenCustom).to.equal('1');
expect(seenHeader).to.equal(`Basic ${Buffer.from('user:pass').toString('base64')}`);
} finally {
globalThis.fetch = originalFetch;
}
});
});
});