UNPKG

1.35 kBJavaScriptView Raw
1var stun = require('stun');
2var servers = require('../servers').stun;
3var test = require('tape');
4var MAX_RESPONSE_TIME = 5000;
5
6servers.forEach(function(url) {
7 test('can connect to ' + url, function(t) {
8 var parts = url.split(':');
9 var host = parts[0];
10 var port = parts[1] ? parseInt(parts[1], 10) : 3478;
11 var method = stun.method.RESPONSE_S;
12 var attr = stun.attribute.MAPPED_ADDRESS;
13 var client;
14
15 function handleResponse(packet) {
16 t.equal(packet.class, 1);
17 t.equal(packet.method, method);
18 t.equal(packet.attrs[attr].family, 4);
19 t.notEqual(packet.attrs[attr].port, null);
20 t.notEqual(packet.attrs[attr].address, null);
21
22 // close the client
23 client.close();
24
25 // reset the response timer
26 clearTimeout(responseTimer);
27 }
28
29 t.plan(5);
30 console.log('attempting to connect to host: ' + host + ', port: ' + port);
31 client = stun.connect(port, host);
32 client.once('response', handleResponse);
33
34 responseTimer = setTimeout(function() {
35 client.removeListener('response', handleResponse);
36 client.removeAllListeners('error');
37 client.close();
38
39 t.fail('server did not respond within ' + MAX_RESPONSE_TIME + 'ms');
40 t.end();
41 }, MAX_RESPONSE_TIME);
42
43 client.on('error', t.ifError.bind(t));
44 client.request();
45 });
46});
\No newline at end of file