-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathproxy.test.js
More file actions
86 lines (73 loc) · 2.44 KB
/
proxy.test.js
File metadata and controls
86 lines (73 loc) · 2.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { expect } = require('chai');
const sinon = require('sinon');
const httpProxy = require('http-proxy');
const https = require('https');
const config = require('./utils/config');
const { DBSQLClient } = require('../..');
class HttpProxyMock {
constructor(target, port) {
this.requests = [];
this.config = {
protocol: 'http',
host: 'localhost',
port,
};
this.target = `https://${config.host}`;
this.proxy = httpProxy.createServer({
target: this.target,
agent: new https.Agent({
rejectUnauthorized: false,
}),
});
this.proxy.on('proxyRes', (proxyRes) => {
const req = proxyRes.req;
this.requests.push({
method: req.method?.toUpperCase(),
url: `${req.protocol}//${req.host}${req.path}`,
requestHeaders: { ...req.getHeaders() },
responseHeaders: proxyRes.headers,
});
});
this.proxy.listen(port);
console.log(`Proxy listening at ${this.config.host}:${this.config.port} -> ${this.target}`);
}
close() {
this.proxy.close(() => {
console.log(`Proxy stopped at ${this.config.host}:${this.config.port}`);
});
}
}
describe('Proxy', () => {
it('should use http proxy', async () => {
const proxy = new HttpProxyMock(`https://${config.host}`, 9090);
try {
const client = new DBSQLClient();
// Our proxy mock is HTTP -> HTTPS, but DBSQLClient is hard-coded to use HTTPS.
// Here we override default behavior to make DBSQLClient work with HTTP proxy
const originalGetConnectionOptions = client.getConnectionOptions;
client.getConnectionOptions = (...args) => {
const result = originalGetConnectionOptions.apply(client, args);
result.https = false;
result.port = 80;
return result;
};
const clientConfig = client.getConfig();
sinon.stub(client, 'getConfig').returns(clientConfig);
const connection = await client.connect({
host: config.host,
path: config.path,
token: config.token,
proxy: proxy.config,
});
const session = await connection.openSession({
initialCatalog: config.database[0],
initialSchema: config.database[1],
});
expect(proxy.requests.length).to.be.gte(1);
expect(proxy.requests[0].method).to.be.eq('POST');
expect(proxy.requests[0].url).to.be.eq(`https://${config.host}${config.path}`);
} finally {
proxy.close();
}
});
});