Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/github-authentication/src/test/node/fetch.test.ts
3326 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as assert from 'assert';
7
import * as http from 'http';
8
import * as net from 'net';
9
import { createFetch } from '../../node/fetch';
10
import { Log } from '../../common/logger';
11
import { AuthProviderType } from '../../github';
12
13
14
suite('fetching', () => {
15
const logger = new Log(AuthProviderType.github);
16
let server: http.Server;
17
let port: number;
18
19
setup(async () => {
20
await new Promise<void>((resolve) => {
21
server = http.createServer((req, res) => {
22
const reqUrl = new URL(req.url!, `http://${req.headers.host}`);
23
const expectAgent = reqUrl.searchParams.get('expectAgent');
24
const actualAgent = String(req.headers['user-agent']).toLowerCase();
25
if (expectAgent && !actualAgent.includes(expectAgent)) {
26
if (reqUrl.searchParams.get('error') === 'html') {
27
res.writeHead(200, {
28
'Content-Type': 'text/html',
29
'X-Client-User-Agent': actualAgent,
30
});
31
res.end('<html><body><h1>Bad Request</h1></body></html>');
32
return;
33
} else {
34
res.writeHead(400, {
35
'X-Client-User-Agent': actualAgent,
36
});
37
res.end('Bad Request');
38
return;
39
}
40
}
41
switch (reqUrl.pathname) {
42
case '/json': {
43
res.writeHead(200, {
44
'Content-Type': 'application/json',
45
'X-Client-User-Agent': actualAgent,
46
});
47
res.end(JSON.stringify({ message: 'Hello, world!' }));
48
break;
49
}
50
case '/text': {
51
res.writeHead(200, {
52
'Content-Type': 'text/plain',
53
'X-Client-User-Agent': actualAgent,
54
});
55
res.end('Hello, world!');
56
break;
57
}
58
default:
59
res.writeHead(404);
60
res.end('Not Found');
61
break;
62
}
63
}).listen(() => {
64
port = (server.address() as net.AddressInfo).port;
65
resolve();
66
});
67
});
68
});
69
70
teardown(async () => {
71
await new Promise<unknown>((resolve) => {
72
server.close(resolve);
73
});
74
});
75
76
test('should use Electron fetch', async () => {
77
const res = await createFetch()(`http://localhost:${port}/json`, {
78
logger,
79
retryFallbacks: true,
80
expectJSON: true,
81
});
82
const actualAgent = res.headers.get('x-client-user-agent') || 'None';
83
assert.ok(actualAgent.includes('electron'), actualAgent);
84
assert.strictEqual(res.status, 200);
85
assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });
86
});
87
88
test('should use Electron fetch 2', async () => {
89
const res = await createFetch()(`http://localhost:${port}/text`, {
90
logger,
91
retryFallbacks: true,
92
expectJSON: false,
93
});
94
const actualAgent = res.headers.get('x-client-user-agent') || 'None';
95
assert.ok(actualAgent.includes('electron'), actualAgent);
96
assert.strictEqual(res.status, 200);
97
assert.deepStrictEqual(await res.text(), 'Hello, world!');
98
});
99
100
test('should fall back to Node.js fetch', async () => {
101
const res = await createFetch()(`http://localhost:${port}/json?expectAgent=node`, {
102
logger,
103
retryFallbacks: true,
104
expectJSON: true,
105
});
106
const actualAgent = res.headers.get('x-client-user-agent') || 'None';
107
assert.strictEqual(actualAgent, 'node');
108
assert.strictEqual(res.status, 200);
109
assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });
110
});
111
112
test('should fall back to Node.js fetch 2', async () => {
113
const res = await createFetch()(`http://localhost:${port}/json?expectAgent=node&error=html`, {
114
logger,
115
retryFallbacks: true,
116
expectJSON: true,
117
});
118
const actualAgent = res.headers.get('x-client-user-agent') || 'None';
119
assert.strictEqual(actualAgent, 'node');
120
assert.strictEqual(res.status, 200);
121
assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });
122
});
123
124
test('should fall back to Node.js http/s', async () => {
125
const res = await createFetch()(`http://localhost:${port}/json?expectAgent=undefined`, {
126
logger,
127
retryFallbacks: true,
128
expectJSON: true,
129
});
130
const actualAgent = res.headers.get('x-client-user-agent') || 'None';
131
assert.strictEqual(actualAgent, 'undefined');
132
assert.strictEqual(res.status, 200);
133
assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });
134
});
135
136
test('should fail with first error', async () => {
137
const res = await createFetch()(`http://localhost:${port}/text`, {
138
logger,
139
retryFallbacks: true,
140
expectJSON: true, // Expect JSON but server returns text
141
});
142
const actualAgent = res.headers.get('x-client-user-agent') || 'None';
143
assert.ok(actualAgent.includes('electron'), actualAgent);
144
assert.strictEqual(res.status, 200);
145
assert.deepStrictEqual(await res.text(), 'Hello, world!');
146
});
147
});
148
149