forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateInterceptedFetch.spec.js
More file actions
69 lines (54 loc) · 2.27 KB
/
createInterceptedFetch.spec.js
File metadata and controls
69 lines (54 loc) · 2.27 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import createInterceptedFetch from './createInterceptedFetch';
describe('createInterceptedFetch', () => {
let mockFetch;
beforeEach(() => {
mockFetch = jest.fn(() => Promise.resolve({ ok: true, status: 200 }));
});
it('should return a function', () => {
const interceptedFetch = createInterceptedFetch(mockFetch);
expect(typeof interceptedFetch).toBe('function');
});
it('should call original fetch with the same URL and options if hostname does not match', async () => {
const interceptedFetch = createInterceptedFetch(mockFetch, {
hostname: 'example.com',
directlineToken: 'test-token'
});
const url = 'https://other.com/path';
const options = { headers: {} };
await interceptedFetch(url, options);
expect(mockFetch).toHaveBeenCalledWith(url, options);
});
it('should modify request headers when hostname matches', async () => {
const interceptedFetch = createInterceptedFetch(mockFetch, {
hostname: 'example.com',
directlineToken: 'test-token'
});
const url = 'https://example.com/api/data';
const options = { headers: {} };
await interceptedFetch(url, options);
expect(options.headers['directline_token']).toBe('test-token');
expect(mockFetch).toHaveBeenCalledWith(url, options);
});
it('should preserve existing headers when modifying request', async () => {
const interceptedFetch = createInterceptedFetch(mockFetch, {
hostname: 'example.com',
directlineToken: 'test-token'
});
const url = 'https://example.com/api/data';
const options = { headers: { 'Content-Type': 'application/json' } };
await interceptedFetch(url, options);
expect(options.headers['directline_token']).toBe('test-token');
expect(options.headers['Content-Type']).toBe('application/json');
expect(mockFetch).toHaveBeenCalledWith(url, options);
});
it('should return the response from original fetch', async () => {
const mockResponse = { ok: true, status: 200 };
mockFetch.mockResolvedValueOnce(mockResponse);
const interceptedFetch = createInterceptedFetch(mockFetch);
const response = await interceptedFetch('https://test.com', {
hostname: 'example.com',
directlineToken: 'test-token'
});
expect(response).toBe(mockResponse);
});
});