forked from amqp-node/amqplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.js
186 lines (165 loc) · 6.18 KB
/
connect.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
var connect = require('../lib/connect').connect;
var credentialsFromUrl = require('../lib/connect').credentialsFromUrl;
var defs = require('../lib/defs');
var assert = require('assert');
var util = require('./util');
var net = require('net');
var parseUrl = require('url').parse;
var fail = util.fail, succeed = util.succeed, latch = util.latch,
kCallback = util.kCallback,
succeedIfAttributeEquals = util.succeedIfAttributeEquals;
var format = require('util').format;
var baseURL = process.env.URL || 'amqp://localhost';
suite("Credentials", function() {
function checkCreds(creds, user, pass, done) {
if (creds.mechanism != 'PLAIN') {
return done('expected mechanism PLAIN');
}
if (creds.username != user || creds.password != pass) {
return done(format("expected '%s', '%s'; got '%s', '%s'",
user, pass, creds.username, creds.password));
}
done();
}
test("no creds", function(done) {
var parts = new URL('amqp://localhost');
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'guest', 'guest', done);
});
test("usual user:pass", function(done) {
var parts = new URL('amqp://user:pass@localhost')
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'user', 'pass', done);
});
test("missing user", function(done) {
var parts = new URL('amqps://:password@localhost');
var creds = credentialsFromUrl(parts);
checkCreds(creds, '', 'password', done);
});
test("missing password", function(done) {
var parts = new URL('amqps://username:@localhost');
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'username', '', done);
});
test("escaped colons", function(done) {
var parts = new URL('amqp://user%3Aname:pass%3Aword@localhost')
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'user:name', 'pass:word', done);
});
});
suite("Connect API", function() {
test("Connection refused", function(done) {
connect('amqp://localhost:23450', {},
kCallback(fail(done), succeed(done)));
});
// %% this ought to fail the promise, rather than throwing an error
test("bad URL", function() {
assert.throws(function() {
connect('blurble');
});
});
[
["As instance of URL", url => url ],
["As string", url => url.href ],
].forEach(([suiteDescription, processURL]) => {
suite(suiteDescription, () => {
test("wrongly typed open option", function(done) {
var url = new URL(baseURL);
url.searchParams.set('frameMax', 'NOT A NUMBER');
connect(processURL(url), {}, kCallback(fail(done), succeed(done)));
});
test("serverProperties", function(done) {
var url = new URL(baseURL);
connect(processURL(url), {}, function(err, connection) {
if (err) { return done(err); }
assert.equal(connection.serverProperties.product, 'RabbitMQ');
done();
});
});
test("using custom heartbeat option", function(done) {
var url = new URL(baseURL);
url.searchParams.set('heartbeat', 20);
connect(processURL(url), {}, kCallback(succeedIfAttributeEquals('heartbeat', 20, done), fail(done)));
});
test("wrongly typed heartbeat option", function(done) {
var url = new URL(baseURL);
url.searchParams.set('heartbeat', 'NOT A NUMBER');
connect(processURL(url), {}, kCallback(fail(done), succeed(done)));
});
test("using plain credentials", function(done) {
var url = new URL(baseURL);
var u = url.username || 'guest';
var p = url.password || 'guest';
connect(processURL(url), {credentials: require('../lib/credentials').plain(u, p)},
kCallback(succeed(done), fail(done)));
});
test("using amqplain credentials", function(done) {
var url = new URL(baseURL);
var u = url.username || 'guest';
var p = url.password || 'guest';
connect(processURL(url), {credentials: require('../lib/credentials').amqplain(u, p)},
kCallback(succeed(done), fail(done)));
});
test("using unsupported mechanism", function(done) {
var creds = {
mechanism: 'UNSUPPORTED',
response: function() { return Buffer.from(''); }
};
connect(processURL(baseURL), {credentials: creds},
kCallback(fail(done), succeed(done)));
});
test("with a given connection timeout", function(done) {
var timeoutServer = net.createServer(function() {}).listen(31991);
var url = new URL('amqp://localhost:31991');
connect(processURL(url), {timeout: 50}, function(err, val) {
timeoutServer.close();
if (val) done(new Error('Expected connection timeout, did not'));
else done();
});
});
});
});
});
suite('Errors on connect', function() {
var server
teardown(function() {
if (server) {
server.close();
}
})
test("closes underlying connection on authentication error", function(done) {
var bothDone = latch(2, done);
server = net.createServer(function(socket) {
socket.once('data', function(protocolHeader) {
assert.deepStrictEqual(
protocolHeader,
Buffer.from("AMQP" + String.fromCharCode(0,0,9,1))
);
util.runServer(socket, function(send, wait) {
send(defs.ConnectionStart,
{versionMajor: 0,
versionMinor: 9,
serverProperties: {},
mechanisms: Buffer.from('PLAIN'),
locales: Buffer.from('en_US')});
wait(defs.ConnectionStartOk)().then(function() {
send(defs.ConnectionClose,
{replyCode: 403,
replyText: 'ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN',
classId: 0,
methodId: 0});
});
});
});
// Wait for the connection to be closed after the authentication error
socket.once('end', function() {
bothDone();
});
}).listen(0);
connect('amqp://localhost:' + server.address().port, {}, function(err) {
if (!err) bothDone(new Error('Expected authentication error'));
bothDone();
});
});
});