-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathcustom-connector.js
More file actions
160 lines (140 loc) · 4.02 KB
/
custom-connector.js
File metadata and controls
160 lines (140 loc) · 4.02 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
const net = require('net');
const assert = require('chai').assert;
const { Connection } = require('../../src/tedious');
describe('custom connector', function() {
let server;
beforeEach(function(done) {
server = net.createServer();
server.listen(0, '127.0.0.1', done);
});
afterEach(() => {
server.close();
});
it('connection using a custom connector', function(done) {
let attemptedConnection = false;
let customConnectorCalled = false;
server.on('connection', async (connection) => {
attemptedConnection = true;
// no need to test auth/login, just end the connection sooner
connection.end();
});
const host = server.address().address;
const port = server.address().port;
const connection = new Connection({
options: {
connector: async () => {
customConnectorCalled = true;
return net.connect({
host,
port,
});
},
},
});
connection.on('end', (err) => {
// validates the connection was stablished using the custom connector
assert.isOk(attemptedConnection);
assert.isOk(customConnectorCalled);
connection.close();
done();
});
connection.on('error', (err) => {
// Connection lost errors are expected due to ending connection sooner
if (!/Connection lost/.test(err)) {
throw err;
}
});
connection.connect();
});
it('connection timeout using a custom connector', function(done) {
const host = server.address().address;
const port = server.address().port;
const connection = new Connection({
options: {
connectTimeout: 10,
connector: async () => {
return net.connect({
host,
port,
});
},
},
});
// times out since no server response is defined
connection.connect((err) => {
assert.strictEqual(
err.code,
'ETIMEOUT',
'should emit timeout error code'
);
assert.strictEqual(
err.message,
'Failed to connect using custom connector in 10ms',
'should emit expected custom connector timeout error msg'
);
done();
});
});
it('should emit socket error custom connector msg', function(done) {
const connection = new Connection({
options: {
connector: async () => {
throw new Error('ERR');
},
},
});
connection.connect((err) => {
assert.strictEqual(
err.code,
'ESOCKET',
'should emit expected error code'
);
assert.strictEqual(
err.message,
'Failed to connect using custom connector - ERR',
'should emit expected custom connector error msg'
);
done();
});
});
it('should only accept functions', function(done) {
assert.throws(() => {
new Connection({
options: {
connector: 'foo',
},
});
}, Error, 'The "config.options.connector" property must be a function.');
done();
});
it('should not allow setting both server and connector options', function(done) {
assert.throws(() => {
new Connection({
server: '0.0.0.0',
options: {
connector: async () => {},
},
});
}, Error, 'Server and connector are mutually exclusive, but 0.0.0.0 and a connector function were provided');
done();
});
it('should not allow setting both port and connector options', function(done) {
assert.throws(() => {
new Connection({
options: {
connector: async () => {},
port: 8080,
},
});
}, Error, 'Port and connector are mutually exclusive, but 8080 and a connector function were provided');
done();
});
it('should require server config option if custom connector is undefined', function(done) {
assert.throws(() => {
new Connection({
options: { port: 8080 },
});
}, TypeError, 'The "config.server" property is required and must be of type string.');
done();
});
});