Skip to content

Commit 8f82a5b

Browse files
committed
Add tests
1 parent cdc58ca commit 8f82a5b

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

src/test/utils/httpRequest.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import assert from 'assert';
7+
import { httpRequest, RequestOptionsLike } from '../../utils/httpRequest';
8+
9+
suite('(unit) utils/httpRequest', () => {
10+
let originalFetch: typeof globalThis.fetch;
11+
let lastFetchUrl: string | URL | Request;
12+
let lastFetchInit: RequestInit | undefined;
13+
14+
function stubFetch(status = 200, body = '{}', headers?: Record<string, string>): void {
15+
globalThis.fetch = async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
16+
lastFetchUrl = input;
17+
lastFetchInit = init;
18+
return new Response(body, {
19+
status,
20+
statusText: status === 200 ? 'OK' : 'Error',
21+
headers: headers ?? {},
22+
});
23+
};
24+
}
25+
26+
setup(() => {
27+
originalFetch = globalThis.fetch;
28+
lastFetchUrl = undefined!;
29+
lastFetchInit = undefined;
30+
});
31+
32+
teardown(() => {
33+
globalThis.fetch = originalFetch;
34+
});
35+
36+
test('sets duplex to half when body is provided', async () => {
37+
stubFetch();
38+
const options: RequestOptionsLike = { method: 'POST', body: 'test' };
39+
await httpRequest('https://example.com', options);
40+
41+
assert.strictEqual(lastFetchInit?.duplex, 'half');
42+
});
43+
44+
test('sets duplex to half when body is empty string', async () => {
45+
stubFetch();
46+
const options: RequestOptionsLike = { method: 'POST', body: '' };
47+
await httpRequest('https://example.com', options);
48+
49+
assert.strictEqual(lastFetchInit?.duplex, 'half');
50+
});
51+
52+
test('does not set duplex when no body is provided', async () => {
53+
stubFetch();
54+
const options: RequestOptionsLike = { method: 'GET' };
55+
await httpRequest('https://example.com', options);
56+
57+
assert.strictEqual(lastFetchInit?.duplex, undefined);
58+
});
59+
60+
test('does not mutate original options headers via signRequest', async () => {
61+
stubFetch();
62+
const sharedOptions: RequestOptionsLike = {
63+
method: 'HEAD',
64+
headers: { 'Accept': 'application/json' },
65+
};
66+
67+
const signRequest = async (request: RequestOptionsLike): Promise<void> => {
68+
request.headers!['Authorization'] = 'Bearer token123';
69+
};
70+
71+
await httpRequest('https://example.com', sharedOptions, signRequest);
72+
73+
// The signed header should have been sent
74+
const sentHeaders = lastFetchInit?.headers as Record<string, string>;
75+
assert.strictEqual(sentHeaders['Authorization'], 'Bearer token123');
76+
77+
// But the original options should be unmodified
78+
assert.strictEqual(sharedOptions.headers!['Authorization'], undefined, 'Original options.headers should not be mutated');
79+
assert.strictEqual(Object.keys(sharedOptions.headers!).length, 1);
80+
});
81+
82+
test('passes signed headers through to fetch', async () => {
83+
stubFetch();
84+
const options: RequestOptionsLike = {
85+
method: 'GET',
86+
headers: { 'X-Custom': 'value' },
87+
};
88+
89+
const signRequest = async (request: RequestOptionsLike): Promise<void> => {
90+
request.headers!['Authorization'] = 'Bearer mytoken';
91+
};
92+
93+
await httpRequest('https://example.com', options, signRequest);
94+
95+
const sentHeaders = lastFetchInit?.headers as Record<string, string>;
96+
assert.strictEqual(sentHeaders['Authorization'], 'Bearer mytoken');
97+
assert.strictEqual(sentHeaders['X-Custom'], 'value');
98+
});
99+
100+
test('passes url directly to fetch', async () => {
101+
stubFetch();
102+
await httpRequest('https://example.com/v2/test', { method: 'GET' });
103+
104+
assert.strictEqual(lastFetchUrl, 'https://example.com/v2/test');
105+
});
106+
});

0 commit comments

Comments
 (0)