forked from databricks/databricks-sql-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpConnection.test.ts
More file actions
168 lines (144 loc) · 4.71 KB
/
HttpConnection.test.ts
File metadata and controls
168 lines (144 loc) · 4.71 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import http from 'http';
import { expect } from 'chai';
import HttpConnection from '../../../../lib/connection/connections/HttpConnection';
import ThriftHttpConnection from '../../../../lib/connection/connections/ThriftHttpConnection';
import IConnectionOptions from '../../../../lib/connection/contracts/IConnectionOptions';
import ClientContextStub from '../../.stubs/ClientContextStub';
describe('HttpConnection.connect', () => {
it('should create Thrift connection', async () => {
const connection = new HttpConnection(
{
host: 'localhost',
port: 10001,
path: '/hive',
},
new ClientContextStub(),
);
const thriftConnection = await connection.getThriftConnection();
expect(thriftConnection).to.be.instanceOf(ThriftHttpConnection);
expect(thriftConnection.url).to.be.equal('http://localhost:10001/hive');
// We expect that connection will be cached
const anotherConnection = await connection.getThriftConnection();
expect(anotherConnection).to.eq(thriftConnection);
});
it('should set SSL certificates and disable rejectUnauthorized', async () => {
const connection = new HttpConnection(
{
host: 'localhost',
port: 10001,
path: '/hive',
https: true,
ca: 'ca',
cert: 'cert',
key: 'key',
},
new ClientContextStub(),
);
const thriftConnection = await connection.getThriftConnection();
expect(thriftConnection.config.agent.options.rejectUnauthorized).to.be.false;
expect(thriftConnection.config.agent.options.ca).to.be.eq('ca');
expect(thriftConnection.config.agent.options.cert).to.be.eq('cert');
expect(thriftConnection.config.agent.options.key).to.be.eq('key');
});
it('should initialize http agents', async () => {
const connection = new HttpConnection(
{
host: 'localhost',
port: 10001,
https: false,
path: '/hive',
},
new ClientContextStub(),
);
const thriftConnection = await connection.getThriftConnection();
expect(thriftConnection.config.agent).to.be.instanceOf(http.Agent);
});
it('should update headers (case 1: Thrift connection not initialized)', async () => {
const initialHeaders = {
a: 'test header A',
b: 'test header B',
};
const connection = new HttpConnection(
{
host: 'localhost',
port: 10001,
path: '/hive',
headers: initialHeaders,
},
new ClientContextStub(),
);
const extraHeaders = {
b: 'new header B',
c: 'test header C',
};
connection.setHeaders(extraHeaders);
expect(connection['headers']).to.deep.equal(extraHeaders);
const thriftConnection = await connection.getThriftConnection();
expect(thriftConnection.config.headers).to.deep.equal({
...initialHeaders,
...extraHeaders,
});
});
it('should update headers (case 2: Thrift connection initialized)', async () => {
const initialHeaders = {
a: 'test header A',
b: 'test header B',
};
const connection = new HttpConnection(
{
host: 'localhost',
port: 10001,
path: '/hive',
headers: initialHeaders,
},
new ClientContextStub(),
);
const thriftConnection = await connection.getThriftConnection();
expect(connection['headers']).to.deep.equal({});
expect(thriftConnection.config.headers).to.deep.equal(initialHeaders);
const extraHeaders = {
b: 'new header B',
c: 'test header C',
};
connection.setHeaders(extraHeaders);
expect(connection['headers']).to.deep.equal(extraHeaders);
expect(thriftConnection.config.headers).to.deep.equal({
...initialHeaders,
...extraHeaders,
});
});
it('should handle trailing slashes in host correctly', async () => {
interface TestCase {
input: {
host: string;
path?: string;
};
expected: string;
}
const testCases: TestCase[] = [
{
input: { host: 'xyz.com/', path: '/sql/v1' },
expected: 'https://xyz.com:443/sql/v1'
},
{
input: { host: 'xyz.com', path: '/sql/v1' },
expected: 'https://xyz.com:443/sql/v1'
},
{
input: { host: 'xyz.com/', path: undefined },
expected: 'https://xyz.com:443/'
}
];
for (const testCase of testCases) {
const options: IConnectionOptions = {
host: testCase.input.host,
port: 443,
path: testCase.input.path,
https: true,
};
const connection = new HttpConnection(options, new ClientContextStub());
const thriftConnection = await connection.getThriftConnection();
expect(thriftConnection.url).to.be.equal(testCase.expected);
}
});
});