Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/parts/request/test/electron-main/request.test.ts
4780 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 http from 'http';
7
import { AddressInfo } from 'net';
8
import assert from 'assert';
9
import { CancellationToken, CancellationTokenSource } from '../../../../common/cancellation.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../test/common/utils.js';
11
import { request } from '../../common/requestImpl.js';
12
import { streamToBuffer } from '../../../../common/buffer.js';
13
import { runWithFakedTimers } from '../../../../test/common/timeTravelScheduler.js';
14
15
16
suite('Request', () => {
17
18
let port: number;
19
let server: http.Server;
20
21
setup(async () => {
22
port = await new Promise<number>((resolvePort, rejectPort) => {
23
server = http.createServer((req, res) => {
24
if (req.url === '/noreply') {
25
return; // never respond
26
}
27
res.setHeader('Content-Type', 'application/json');
28
if (req.headers['echo-header']) {
29
res.setHeader('echo-header', req.headers['echo-header']);
30
}
31
const data: Buffer[] = [];
32
req.on('data', chunk => data.push(chunk));
33
req.on('end', () => {
34
res.end(JSON.stringify({
35
method: req.method,
36
url: req.url,
37
data: Buffer.concat(data).toString()
38
}));
39
});
40
}).listen(0, '127.0.0.1', () => {
41
const address = server.address();
42
resolvePort((address as AddressInfo).port);
43
}).on('error', err => {
44
rejectPort(err);
45
});
46
});
47
});
48
49
teardown(async () => {
50
await new Promise<void>((resolve, reject) => {
51
server.close(err => err ? reject(err) : resolve());
52
});
53
});
54
55
test('GET', async () => {
56
const context = await request({
57
url: `http://127.0.0.1:${port}`,
58
headers: {
59
'echo-header': 'echo-value'
60
}
61
}, CancellationToken.None);
62
assert.strictEqual(context.res.statusCode, 200);
63
assert.strictEqual(context.res.headers['content-type'], 'application/json');
64
assert.strictEqual(context.res.headers['echo-header'], 'echo-value');
65
const buffer = await streamToBuffer(context.stream);
66
const body = JSON.parse(buffer.toString());
67
assert.strictEqual(body.method, 'GET');
68
assert.strictEqual(body.url, '/');
69
});
70
71
test('POST', async () => {
72
const context = await request({
73
type: 'POST',
74
url: `http://127.0.0.1:${port}/postpath`,
75
data: 'Some data',
76
}, CancellationToken.None);
77
assert.strictEqual(context.res.statusCode, 200);
78
assert.strictEqual(context.res.headers['content-type'], 'application/json');
79
const buffer = await streamToBuffer(context.stream);
80
const body = JSON.parse(buffer.toString());
81
assert.strictEqual(body.method, 'POST');
82
assert.strictEqual(body.url, '/postpath');
83
assert.strictEqual(body.data, 'Some data');
84
});
85
86
test('timeout', async () => {
87
return runWithFakedTimers({}, async () => {
88
try {
89
await request({
90
type: 'GET',
91
url: `http://127.0.0.1:${port}/noreply`,
92
timeout: 123,
93
}, CancellationToken.None);
94
assert.fail('Should fail with timeout');
95
} catch (err) {
96
assert.strictEqual(err.message, 'Fetch timeout: 123ms');
97
}
98
});
99
});
100
101
test('cancel', async () => {
102
return runWithFakedTimers({}, async () => {
103
try {
104
const source = new CancellationTokenSource();
105
const res = request({
106
type: 'GET',
107
url: `http://127.0.0.1:${port}/noreply`,
108
}, source.token);
109
await new Promise(resolve => setTimeout(resolve, 100));
110
source.cancel();
111
await res;
112
assert.fail('Should fail with cancellation');
113
} catch (err) {
114
assert.strictEqual(err.message, 'Canceled');
115
}
116
});
117
});
118
119
ensureNoDisposablesAreLeakedInTestSuite();
120
});
121
122