@theia/request 1.71.0-next.57 → 1.71.0-next.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/common-request-service.d.ts +12 -1
- package/lib/common-request-service.d.ts.map +1 -1
- package/lib/common-request-service.js.map +1 -1
- package/lib/node-request-service.d.ts +2 -9
- package/lib/node-request-service.d.ts.map +1 -1
- package/lib/node-request-service.js +59 -87
- package/lib/node-request-service.js.map +1 -1
- package/{src/package.spec.ts → lib/node-request-service.spec.d.ts} +3 -14
- package/lib/node-request-service.spec.d.ts.map +1 -0
- package/lib/node-request-service.spec.js +233 -0
- package/lib/node-request-service.spec.js.map +1 -0
- package/lib/proxy.d.ts +2 -4
- package/lib/proxy.d.ts.map +1 -1
- package/lib/proxy.js +30 -13
- package/lib/proxy.js.map +1 -1
- package/lib/{package.spec.d.ts → proxy.spec.d.ts} +3 -2
- package/lib/proxy.spec.d.ts.map +1 -0
- package/lib/proxy.spec.js +85 -0
- package/lib/proxy.spec.js.map +1 -0
- package/package.json +4 -5
- package/src/common-request-service.ts +10 -1
- package/src/node-request-service.spec.ts +243 -0
- package/src/node-request-service.ts +65 -102
- package/src/proxy.spec.ts +94 -0
- package/src/proxy.ts +32 -17
- package/lib/package.spec.d.ts.map +0 -1
- package/lib/package.spec.js +0 -26
- package/lib/package.spec.js.map +0 -1
package/lib/proxy.js
CHANGED
|
@@ -16,9 +16,7 @@
|
|
|
16
16
|
********************************************************************************/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.getProxyAgent = getProxyAgent;
|
|
19
|
-
const
|
|
20
|
-
const httpAgent = require("http-proxy-agent");
|
|
21
|
-
const httpsAgent = require("https-proxy-agent");
|
|
19
|
+
const undici_1 = require("undici");
|
|
22
20
|
function getSystemProxyURI(requestURL, env) {
|
|
23
21
|
if (requestURL.protocol === 'http:') {
|
|
24
22
|
return env.HTTP_PROXY || env.http_proxy;
|
|
@@ -29,22 +27,41 @@ function getSystemProxyURI(requestURL, env) {
|
|
|
29
27
|
return undefined;
|
|
30
28
|
}
|
|
31
29
|
function getProxyAgent(rawRequestURL, env, options = {}) {
|
|
32
|
-
|
|
30
|
+
let requestURL;
|
|
31
|
+
try {
|
|
32
|
+
requestURL = new URL(rawRequestURL);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
33
37
|
const proxyURL = options.proxyUrl || getSystemProxyURI(requestURL, env);
|
|
34
38
|
if (!proxyURL) {
|
|
39
|
+
if (options.strictSSL === false) {
|
|
40
|
+
return new undici_1.Agent({
|
|
41
|
+
connect: { rejectUnauthorized: false }
|
|
42
|
+
});
|
|
43
|
+
}
|
|
35
44
|
return undefined;
|
|
36
45
|
}
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
let proxyEndpoint;
|
|
47
|
+
try {
|
|
48
|
+
proxyEndpoint = new URL(proxyURL);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
39
51
|
return undefined;
|
|
40
52
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
if (!/^https?:$/.test(proxyEndpoint.protocol)) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const proxyAgentOptions = {
|
|
57
|
+
uri: proxyURL,
|
|
58
|
+
token: proxyEndpoint.username
|
|
59
|
+
? `Basic ${Buffer.from(`${decodeURIComponent(proxyEndpoint.username)}:${decodeURIComponent(proxyEndpoint.password)}`).toString('base64')}`
|
|
60
|
+
: undefined,
|
|
46
61
|
};
|
|
47
|
-
|
|
48
|
-
|
|
62
|
+
if (options.strictSSL === false) {
|
|
63
|
+
proxyAgentOptions.requestTls = { rejectUnauthorized: false };
|
|
64
|
+
}
|
|
65
|
+
return new undici_1.ProxyAgent(proxyAgentOptions);
|
|
49
66
|
}
|
|
50
67
|
//# sourceMappingURL=proxy.js.map
|
package/lib/proxy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;kFAckF;;
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;kFAckF;;AAmBlF,sCA0CC;AA3DD,mCAAuD;AAEvD,SAAS,iBAAiB,CAAC,UAAe,EAAE,GAAuB;IAC/D,IAAI,UAAU,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QAClC,OAAO,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,UAAU,CAAC;IAC5C,CAAC;SAAM,IAAI,UAAU,CAAC,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAC1C,OAAO,GAAG,CAAC,WAAW,IAAI,GAAG,CAAC,WAAW,IAAI,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,UAAU,CAAC;IAClF,CAAC;IAED,OAAO,SAAS,CAAC;AACrB,CAAC;AAOD,SAAgB,aAAa,CAAC,aAAqB,EAAE,GAAuB,EAAE,UAAyB,EAAE;IACrG,IAAI,UAAe,CAAC;IACpB,IAAI,CAAC;QACD,UAAU,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC;IACxC,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,iBAAiB,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;IAExE,IAAI,CAAC,QAAQ,EAAE,CAAC;QACZ,IAAI,OAAO,CAAC,SAAS,KAAK,KAAK,EAAE,CAAC;YAC9B,OAAO,IAAI,cAAK,CAAC;gBACb,OAAO,EAAE,EAAE,kBAAkB,EAAE,KAAK,EAAE;aACzC,CAAC,CAAC;QACP,CAAC;QACD,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,IAAI,aAAkB,CAAC;IACvB,IAAI,CAAC;QACD,aAAa,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5C,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,MAAM,iBAAiB,GAAuB;QAC1C,GAAG,EAAE,QAAQ;QACb,KAAK,EAAE,aAAa,CAAC,QAAQ;YACzB,CAAC,CAAC,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG,kBAAkB,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,kBAAkB,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;YAC1I,CAAC,CAAC,SAAS;KAClB,CAAC;IAEF,IAAI,OAAO,CAAC,SAAS,KAAK,KAAK,EAAE,CAAC;QAC9B,iBAAiB,CAAC,UAAU,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,CAAC;IACjE,CAAC;IAED,OAAO,IAAI,mBAAU,CAAC,iBAAiB,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/********************************************************************************
|
|
2
|
-
* Copyright (C)
|
|
2
|
+
* Copyright (C) 2026 EclipseSource GmbH.
|
|
3
3
|
*
|
|
4
4
|
* This program and the accompanying materials are made available under the
|
|
5
5
|
* terms of the Eclipse Public License v. 2.0 which is available at
|
|
@@ -13,4 +13,5 @@
|
|
|
13
13
|
*
|
|
14
14
|
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
15
|
********************************************************************************/
|
|
16
|
-
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=proxy.spec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.spec.d.ts","sourceRoot":"","sources":["../src/proxy.spec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;kFAckF"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/********************************************************************************
|
|
3
|
+
* Copyright (C) 2026 EclipseSource GmbH.
|
|
4
|
+
*
|
|
5
|
+
* This program and the accompanying materials are made available under the
|
|
6
|
+
* terms of the Eclipse Public License v. 2.0 which is available at
|
|
7
|
+
* http://www.eclipse.org/legal/epl-2.0.
|
|
8
|
+
*
|
|
9
|
+
* This Source Code may also be made available under the following Secondary
|
|
10
|
+
* Licenses when the conditions for such availability set forth in the Eclipse
|
|
11
|
+
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
12
|
+
* with the GNU Classpath Exception which is available at
|
|
13
|
+
* https://www.gnu.org/software/classpath/license.html.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
16
|
+
********************************************************************************/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
const chai_1 = require("chai");
|
|
19
|
+
const proxy_1 = require("./proxy");
|
|
20
|
+
describe('proxy', () => {
|
|
21
|
+
describe('getProxyAgent', () => {
|
|
22
|
+
it('should return undefined when no proxy is configured', () => {
|
|
23
|
+
const agent = (0, proxy_1.getProxyAgent)('https://example.com', {});
|
|
24
|
+
(0, chai_1.expect)(agent).to.be.undefined;
|
|
25
|
+
});
|
|
26
|
+
it('should return a dispatcher when proxy URL is provided via options', () => {
|
|
27
|
+
const agent = (0, proxy_1.getProxyAgent)('https://example.com', {}, {
|
|
28
|
+
proxyUrl: 'http://proxy.example.com:8080'
|
|
29
|
+
});
|
|
30
|
+
(0, chai_1.expect)(agent).to.not.be.undefined;
|
|
31
|
+
});
|
|
32
|
+
it('should return a dispatcher when HTTP_PROXY env variable is set for http URLs', () => {
|
|
33
|
+
const env = { HTTP_PROXY: 'http://proxy.example.com:8080' };
|
|
34
|
+
const agent = (0, proxy_1.getProxyAgent)('http://example.com', env);
|
|
35
|
+
(0, chai_1.expect)(agent).to.not.be.undefined;
|
|
36
|
+
});
|
|
37
|
+
it('should return a dispatcher when http_proxy env variable is set for http URLs', () => {
|
|
38
|
+
const env = { http_proxy: 'http://proxy.example.com:8080' };
|
|
39
|
+
const agent = (0, proxy_1.getProxyAgent)('http://example.com', env);
|
|
40
|
+
(0, chai_1.expect)(agent).to.not.be.undefined;
|
|
41
|
+
});
|
|
42
|
+
it('should return a dispatcher when HTTPS_PROXY env variable is set for https URLs', () => {
|
|
43
|
+
const env = { HTTPS_PROXY: 'http://proxy.example.com:8080' };
|
|
44
|
+
const agent = (0, proxy_1.getProxyAgent)('https://example.com', env);
|
|
45
|
+
(0, chai_1.expect)(agent).to.not.be.undefined;
|
|
46
|
+
});
|
|
47
|
+
it('should fall back to HTTP_PROXY for https URLs when HTTPS_PROXY is not set', () => {
|
|
48
|
+
const env = { HTTP_PROXY: 'http://proxy.example.com:8080' };
|
|
49
|
+
const agent = (0, proxy_1.getProxyAgent)('https://example.com', env);
|
|
50
|
+
(0, chai_1.expect)(agent).to.not.be.undefined;
|
|
51
|
+
});
|
|
52
|
+
it('should return undefined for invalid request URLs', () => {
|
|
53
|
+
const agent = (0, proxy_1.getProxyAgent)('not-a-url', {}, {
|
|
54
|
+
proxyUrl: 'http://proxy.example.com:8080'
|
|
55
|
+
});
|
|
56
|
+
(0, chai_1.expect)(agent).to.be.undefined;
|
|
57
|
+
});
|
|
58
|
+
it('should return undefined for non-http proxy protocols', () => {
|
|
59
|
+
const agent = (0, proxy_1.getProxyAgent)('https://example.com', {}, {
|
|
60
|
+
proxyUrl: 'socks5://proxy.example.com:1080'
|
|
61
|
+
});
|
|
62
|
+
(0, chai_1.expect)(agent).to.be.undefined;
|
|
63
|
+
});
|
|
64
|
+
it('should return undefined for invalid proxy URLs', () => {
|
|
65
|
+
const agent = (0, proxy_1.getProxyAgent)('https://example.com', {}, {
|
|
66
|
+
proxyUrl: 'not-a-valid-url'
|
|
67
|
+
});
|
|
68
|
+
(0, chai_1.expect)(agent).to.be.undefined;
|
|
69
|
+
});
|
|
70
|
+
it('should return an Agent (not ProxyAgent) when strictSSL is false and no proxy is set', () => {
|
|
71
|
+
const agent = (0, proxy_1.getProxyAgent)('https://example.com', {}, {
|
|
72
|
+
strictSSL: false
|
|
73
|
+
});
|
|
74
|
+
(0, chai_1.expect)(agent).to.not.be.undefined;
|
|
75
|
+
});
|
|
76
|
+
it('should prefer proxyUrl option over environment variables', () => {
|
|
77
|
+
const env = { HTTP_PROXY: 'http://env-proxy.example.com:8080' };
|
|
78
|
+
const agent = (0, proxy_1.getProxyAgent)('http://example.com', env, {
|
|
79
|
+
proxyUrl: 'http://option-proxy.example.com:9090'
|
|
80
|
+
});
|
|
81
|
+
(0, chai_1.expect)(agent).to.not.be.undefined;
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
//# sourceMappingURL=proxy.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.spec.js","sourceRoot":"","sources":["../src/proxy.spec.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;kFAckF;;AAElF,+BAA8B;AAC9B,mCAAwC;AAExC,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACnB,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;QAC3B,EAAE,CAAC,qDAAqD,EAAE,GAAG,EAAE;YAC3D,MAAM,KAAK,GAAG,IAAA,qBAAa,EAAC,qBAAqB,EAAE,EAAE,CAAC,CAAC;YACvD,IAAA,aAAM,EAAC,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,SAAS,CAAC;QAClC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,mEAAmE,EAAE,GAAG,EAAE;YACzE,MAAM,KAAK,GAAG,IAAA,qBAAa,EAAC,qBAAqB,EAAE,EAAE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AE;YAChE,MAAM,GAAG,GAAG,EAAE,UAAU,EAAE,mCAAmC,EAAE,CAAC;YAChE,MAAM,KAAK,GAAG,IAAA,qBAAa,EAAC,oBAAoB,EAAE,GAAG,EAAE;gBACnD,QAAQ,EAAE,sCAAsC;aACnD,CAAC,CAAC;YACH,IAAA,aAAM,EAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC;QACtC,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theia/request",
|
|
3
|
-
"version": "1.71.0-next.
|
|
3
|
+
"version": "1.71.0-next.64+5f7caff6d",
|
|
4
4
|
"description": "Theia Proxy-Aware Request Service",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -29,9 +29,8 @@
|
|
|
29
29
|
"watch": "theiaext watch"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"tslib": "^2.6.2"
|
|
32
|
+
"tslib": "^2.6.2",
|
|
33
|
+
"undici": "^7.16.0"
|
|
35
34
|
},
|
|
36
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "5f7caff6d4a136452787442612656962b5c5099e"
|
|
37
36
|
}
|
|
@@ -28,6 +28,15 @@ export interface RequestOptions {
|
|
|
28
28
|
headers?: Headers;
|
|
29
29
|
timeout?: number;
|
|
30
30
|
data?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Controls automatic redirect following.
|
|
33
|
+
*
|
|
34
|
+
* Set to `0` to disable automatic redirect following (`redirect: 'manual'`).
|
|
35
|
+
* Any other value or `undefined` follows redirects automatically.
|
|
36
|
+
*
|
|
37
|
+
* Note: Custom redirect count limits are not supported; the underlying `fetch` API
|
|
38
|
+
* either follows all redirects or none.
|
|
39
|
+
*/
|
|
31
40
|
followRedirects?: number;
|
|
32
41
|
proxyAuthorization?: string;
|
|
33
42
|
}
|
|
@@ -123,5 +132,5 @@ export const REQUEST_SERVICE_PATH = '/services/request-service';
|
|
|
123
132
|
|
|
124
133
|
export interface CancellationToken {
|
|
125
134
|
readonly isCancellationRequested: boolean;
|
|
126
|
-
readonly onCancellationRequested: (listener: () => void) => void;
|
|
135
|
+
readonly onCancellationRequested: (listener: () => void) => void | { dispose(): void };
|
|
127
136
|
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (C) 2026 EclipseSource GmbH.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
* http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
*
|
|
8
|
+
* This Source Code may also be made available under the following Secondary
|
|
9
|
+
* Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
* with the GNU Classpath Exception which is available at
|
|
12
|
+
* https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
*
|
|
14
|
+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
********************************************************************************/
|
|
16
|
+
|
|
17
|
+
import { expect } from 'chai';
|
|
18
|
+
import * as http from 'http';
|
|
19
|
+
import { NodeRequestService } from './node-request-service';
|
|
20
|
+
import { CancellationToken, RequestContext } from './common-request-service';
|
|
21
|
+
|
|
22
|
+
describe('NodeRequestService', () => {
|
|
23
|
+
let service: NodeRequestService;
|
|
24
|
+
let server: http.Server;
|
|
25
|
+
let serverPort: number;
|
|
26
|
+
|
|
27
|
+
before(done => {
|
|
28
|
+
service = new NodeRequestService();
|
|
29
|
+
server = http.createServer((req, res) => {
|
|
30
|
+
if (req.url === '/json') {
|
|
31
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
32
|
+
res.end(JSON.stringify({ message: 'hello' }));
|
|
33
|
+
} else if (req.url === '/text') {
|
|
34
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
35
|
+
res.end('hello world');
|
|
36
|
+
} else if (req.url === '/echo-headers') {
|
|
37
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
38
|
+
res.end(JSON.stringify(req.headers));
|
|
39
|
+
} else if (req.url === '/echo-method') {
|
|
40
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
41
|
+
res.end(req.method);
|
|
42
|
+
} else if (req.url === '/echo-body') {
|
|
43
|
+
const chunks: Buffer[] = [];
|
|
44
|
+
req.on('data', chunk => chunks.push(chunk));
|
|
45
|
+
req.on('end', () => {
|
|
46
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
47
|
+
res.end(Buffer.concat(chunks).toString());
|
|
48
|
+
});
|
|
49
|
+
} else if (req.url === '/status/404') {
|
|
50
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
51
|
+
res.end('not found');
|
|
52
|
+
} else if (req.url === '/status/500') {
|
|
53
|
+
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
54
|
+
res.end('server error');
|
|
55
|
+
} else if (req.url === '/slow') {
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
58
|
+
res.end('slow response');
|
|
59
|
+
}, 5000);
|
|
60
|
+
} else if (req.url === '/empty') {
|
|
61
|
+
res.writeHead(204);
|
|
62
|
+
res.end();
|
|
63
|
+
} else if (req.url === '/redirect') {
|
|
64
|
+
res.writeHead(302, { 'Location': '/text' });
|
|
65
|
+
res.end();
|
|
66
|
+
} else if (req.url === '/redirect-chain') {
|
|
67
|
+
res.writeHead(302, { 'Location': '/redirect' });
|
|
68
|
+
res.end();
|
|
69
|
+
} else {
|
|
70
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
71
|
+
res.end('ok');
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
server.listen(0, () => {
|
|
75
|
+
const addr = server.address();
|
|
76
|
+
if (addr && typeof addr === 'object') {
|
|
77
|
+
serverPort = addr.port;
|
|
78
|
+
}
|
|
79
|
+
done();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
after(done => {
|
|
84
|
+
server.close(done);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
function url(path: string): string {
|
|
88
|
+
return `http://localhost:${serverPort}${path}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
it('should perform a basic GET request', async () => {
|
|
92
|
+
const result = await service.request({ url: url('/text') });
|
|
93
|
+
expect(result.res.statusCode).to.equal(200);
|
|
94
|
+
expect(RequestContext.asText(result)).to.equal('hello world');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should parse JSON responses', async () => {
|
|
98
|
+
const result = await service.request({ url: url('/json') });
|
|
99
|
+
expect(result.res.statusCode).to.equal(200);
|
|
100
|
+
const json = RequestContext.asJson<{ message: string }>(result);
|
|
101
|
+
expect(json.message).to.equal('hello');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should handle non-200 status codes', async () => {
|
|
105
|
+
const result = await service.request({ url: url('/status/404') });
|
|
106
|
+
expect(result.res.statusCode).to.equal(404);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should handle 500 status codes', async () => {
|
|
110
|
+
const result = await service.request({ url: url('/status/500') });
|
|
111
|
+
expect(result.res.statusCode).to.equal(500);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should handle 204 No Content', async () => {
|
|
115
|
+
const result = await service.request({ url: url('/empty') });
|
|
116
|
+
expect(result.res.statusCode).to.equal(204);
|
|
117
|
+
expect(RequestContext.asText(result)).to.equal('');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should send custom headers', async () => {
|
|
121
|
+
const result = await service.request({
|
|
122
|
+
url: url('/echo-headers'),
|
|
123
|
+
headers: { 'X-Custom': 'test-value' }
|
|
124
|
+
});
|
|
125
|
+
const headers = RequestContext.asJson<Record<string, string>>(result);
|
|
126
|
+
expect(headers['x-custom']).to.equal('test-value');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should use specified HTTP method', async () => {
|
|
130
|
+
const result = await service.request({
|
|
131
|
+
url: url('/echo-method'),
|
|
132
|
+
type: 'POST'
|
|
133
|
+
});
|
|
134
|
+
expect(RequestContext.asText(result)).to.equal('POST');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('should send request body', async () => {
|
|
138
|
+
const result = await service.request({
|
|
139
|
+
url: url('/echo-body'),
|
|
140
|
+
type: 'POST',
|
|
141
|
+
data: 'test body content'
|
|
142
|
+
});
|
|
143
|
+
expect(RequestContext.asText(result)).to.equal('test body content');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should timeout when request takes too long', async () => {
|
|
147
|
+
try {
|
|
148
|
+
await service.request({
|
|
149
|
+
url: url('/slow'),
|
|
150
|
+
timeout: 100
|
|
151
|
+
});
|
|
152
|
+
expect.fail('Should have thrown a timeout error');
|
|
153
|
+
} catch (e) {
|
|
154
|
+
expect(e).to.be.an.instanceOf(Error);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should abort on cancellation', async () => {
|
|
159
|
+
const listeners: Array<() => void> = [];
|
|
160
|
+
const token: CancellationToken = {
|
|
161
|
+
isCancellationRequested: false,
|
|
162
|
+
onCancellationRequested: (listener: () => void) => {
|
|
163
|
+
listeners.push(listener);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const promise = service.request({
|
|
168
|
+
url: url('/slow'),
|
|
169
|
+
}, token);
|
|
170
|
+
|
|
171
|
+
// Trigger cancellation shortly after the request starts
|
|
172
|
+
setTimeout(() => {
|
|
173
|
+
listeners.forEach(l => l());
|
|
174
|
+
}, 50);
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
await promise;
|
|
178
|
+
expect.fail('Should have thrown an abort error');
|
|
179
|
+
} catch (e) {
|
|
180
|
+
expect(e).to.be.an.instanceOf(Error);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should include response headers', async () => {
|
|
185
|
+
const result = await service.request({ url: url('/json') });
|
|
186
|
+
expect(result.res.headers['content-type']).to.equal('application/json');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('should set the url on the result', async () => {
|
|
190
|
+
const requestUrl = url('/text');
|
|
191
|
+
const result = await service.request({ url: requestUrl });
|
|
192
|
+
expect(result.url).to.equal(requestUrl);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe('configure', () => {
|
|
196
|
+
it('should configure proxy settings', async () => {
|
|
197
|
+
const configuredService = new NodeRequestService();
|
|
198
|
+
await configuredService.configure({
|
|
199
|
+
proxyUrl: 'http://proxy.example.com:8080',
|
|
200
|
+
strictSSL: false,
|
|
201
|
+
proxyAuthorization: 'Basic dGVzdDp0ZXN0'
|
|
202
|
+
});
|
|
203
|
+
// Just verify configuration doesn't throw
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should inject Proxy-Authorization header when authorization is configured', async () => {
|
|
207
|
+
const configuredService = new NodeRequestService();
|
|
208
|
+
await configuredService.configure({
|
|
209
|
+
proxyAuthorization: 'Basic dGVzdDp0ZXN0'
|
|
210
|
+
});
|
|
211
|
+
const result = await configuredService.request({ url: url('/echo-headers') });
|
|
212
|
+
const headers = RequestContext.asJson<Record<string, string>>(result);
|
|
213
|
+
expect(headers['proxy-authorization']).to.equal('Basic dGVzdDp0ZXN0');
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe('redirect handling', () => {
|
|
218
|
+
it('should follow redirects by default', async () => {
|
|
219
|
+
const result = await service.request({ url: url('/redirect') });
|
|
220
|
+
expect(result.res.statusCode).to.equal(200);
|
|
221
|
+
expect(RequestContext.asText(result)).to.equal('hello world');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should not follow redirects when followRedirects is 0', async () => {
|
|
225
|
+
const result = await service.request({ url: url('/redirect'), followRedirects: 0 });
|
|
226
|
+
expect(result.res.statusCode).to.equal(302);
|
|
227
|
+
expect(result.res.headers['location']).to.equal('/text');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('should follow redirect chains by default', async () => {
|
|
231
|
+
const result = await service.request({ url: url('/redirect-chain') });
|
|
232
|
+
expect(result.res.statusCode).to.equal(200);
|
|
233
|
+
expect(RequestContext.asText(result)).to.equal('hello world');
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe('resolveProxy', () => {
|
|
238
|
+
it('should return undefined by default', async () => {
|
|
239
|
+
const proxy = await service.resolveProxy('https://example.com');
|
|
240
|
+
expect(proxy).to.be.undefined;
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
});
|