This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathHttpSocket.js
105 lines (89 loc) · 3.11 KB
/
HttpSocket.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
/* Test the Http socket
*/
var HttpSocket = require('../lib/HttpSocket'),
http = require('http'),
MessageCollector = require('./messageCollector'),
assert = require('chai').assert;
/*global describe before it after*/
describe('HttpSocket', function () {
var s, e, messages, lastHeaders;
before(function (done) {
e = new HttpSocket({ host: 'http://localhost:8125' });
lastHeaders = null;
messages = new MessageCollector();
s = http.createServer(function (req, res) {
lastHeaders = req.headers;
req.setEncoding('ascii');
var m = '';
req.on('data', function (data) {
m += data;
});
req.on('end', function (data) {
if (data) { m += data; }
messages.addMessage(m);
res.end();
});
});
s.listen(8125, undefined, undefined, done);
});
after(function () {
s.close();
});
it("Respects host-configuration", function (done) {
var w = new HttpSocket({host: 'some_other_host.sbhr.dk'});
w.send('wrong_message');
setTimeout(function () {
assert.lengthOf(messages._packetsReceived, 0);
done();
}, 25);
});
it("Sends data immediately with maxBufferSize = 0", function (done) {
var withoutBuffer = new HttpSocket({maxBufferSize: 0, host: 'http://localhost:8125'}),
start = Date.now();
withoutBuffer.send('do_not_buffer');
messages.expectMessage('do_not_buffer', function () {
assert.closeTo(Date.now() - start, 0, 25);
withoutBuffer.close();
done();
}, 500);
});
it("Doesn't send data immediately with maxBufferSize > 0", function (done) {
var withBuffer = new HttpSocket({socketTimeout: 25, host: 'http://localhost:8125'});
withBuffer.send('buffer_this');
var start = Date.now();
messages.expectMessage('buffer_this', function (err) {
assert.operator(Date.now() - start, '>=', 25);
withBuffer.close();
done(err);
});
});
it("Sends headers", function (done) {
var headers = {'X-Test': 'Test'};
var withHeaders = new HttpSocket({headers: headers, host: 'http://localhost:8125'});
withHeaders.send('no heders kthxbai');
messages.expectMessage('no heders kthxbai', function (err) {
assert.isNotNull(lastHeaders);
assert.equal(lastHeaders['x-test'], 'Test');
withHeaders.close();
done(err);
});
});
it("Send 500 messages", function (done) {
this.slow(500);
// Send messages
for (var i = 0; i < 500; i += 1) {
e.send('foobar' + i);
}
e.close();
setTimeout(function () {
// Received some packets
assert.closeTo(
messages._packetsReceived.length,
500, // Should get 500
5 // ±5
);
messages._packetsReceived = [];
return done();
}, 25);
});
});