test_line_breaks.js
987 Bytes
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
var net = require('net'),
tap = require('tap'),
carrier = require('../lib/carrier.js');
tap.test("multiple lines", function(t) {
var server;
var port = 4001;
var to_be_sents = ["Hel", "lo\n", "Wor", "ld\n", "Glorious", " place"];
var expecteds = to_be_sents.join('').split("\n");
t.plan(expecteds.length);
server = net.createServer(function(conn) {
var received_lines = -1;
carrier.carry(conn, function(line) {
received_lines ++;
t.equal(line, expecteds[received_lines]);
});
});
server.listen(port);
var client = net.createConnection(port);
client.on('connect', function() {
var sent_lines = 0;
var next = function() {
if (sent_lines !== to_be_sents.length) {
client.write(to_be_sents[sent_lines], function() {
sent_lines ++;
next();
});
}
else {
client.end();
}
};
next();
});
t.on("end", function() {
server.close();
});
});