Skip to content

Commit 8e6098a

Browse files
Add some tests for proxy support (#206)
Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent 80c660e commit 8e6098a

4 files changed

Lines changed: 153 additions & 1 deletion

File tree

package-lock.json

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"eslint-plugin-jsx-a11y": "^6.6.1",
6464
"eslint-plugin-react": "^7.30.1",
6565
"eslint-plugin-react-hooks": "^4.6.0",
66+
"http-proxy": "^1.18.1",
6667
"mocha": "^10.2.0",
6768
"nyc": "^15.1.0",
6869
"prettier": "^2.8.4",

tests/e2e/proxy.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const { expect } = require('chai');
2+
const sinon = require('sinon');
3+
const httpProxy = require('http-proxy');
4+
const https = require('https');
5+
const config = require('./utils/config');
6+
const { DBSQLClient } = require('../..');
7+
8+
class HttpProxyMock {
9+
constructor(target, port) {
10+
this.requests = [];
11+
12+
this.config = {
13+
protocol: 'http',
14+
host: 'localhost',
15+
port,
16+
};
17+
18+
this.target = `https://${config.host}`;
19+
20+
this.proxy = httpProxy.createServer({
21+
target: this.target,
22+
agent: new https.Agent({
23+
rejectUnauthorized: false,
24+
}),
25+
});
26+
27+
this.proxy.on('proxyRes', (proxyRes) => {
28+
const req = proxyRes.req;
29+
this.requests.push({
30+
method: req.method?.toUpperCase(),
31+
url: `${req.protocol}//${req.host}${req.path}`,
32+
requestHeaders: { ...req.getHeaders() },
33+
responseHeaders: proxyRes.headers,
34+
});
35+
});
36+
37+
this.proxy.listen(port);
38+
console.log(`Proxy listening at ${this.config.host}:${this.config.port} -> ${this.target}`);
39+
}
40+
41+
close() {
42+
this.proxy.close(() => {
43+
console.log(`Proxy stopped at ${this.config.host}:${this.config.port}`);
44+
});
45+
}
46+
}
47+
48+
describe('Proxy', () => {
49+
it('should use http proxy', async () => {
50+
const proxy = new HttpProxyMock(`https://${config.host}`, 9090);
51+
try {
52+
const client = new DBSQLClient();
53+
const clientConfig = client.getConfig();
54+
sinon.stub(client, 'getConfig').returns(clientConfig);
55+
56+
const connection = await client.connect({
57+
host: config.host,
58+
path: config.path,
59+
token: config.token,
60+
proxy: proxy.config,
61+
});
62+
63+
const session = await connection.openSession({
64+
initialCatalog: config.database[0],
65+
initialSchema: config.database[1],
66+
});
67+
68+
expect(proxy.requests.length).to.be.gte(1);
69+
expect(proxy.requests[0].method).to.be.eq('POST');
70+
expect(proxy.requests[0].url).to.be.eq(`https://${config.host}${config.path}`);
71+
} finally {
72+
proxy.close();
73+
}
74+
});
75+
});

tests/e2e/timeouts.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function openSession(socketTimeout, customConfig) {
2525
});
2626
}
2727

28-
describe('Data fetching', () => {
28+
describe('Timeouts', () => {
2929
const socketTimeout = 1; // minimum value to make sure any request will time out
3030

3131
it('should use default socket timeout', async () => {

0 commit comments

Comments
 (0)