Browse files

passing delayed connection test

  • Loading branch information...
1 parent 91c6f7a commit 03d4bd340385ca8ebe067921792f9e33513878d1 @substack committed Dec 17, 2012
Showing with 51 additions and 0 deletions.
  1. +51 −0 test/delayed.js
View
51 test/delayed.js
@@ -0,0 +1,51 @@
+var test = require('tap').test;
+var http = require('http');
+var net = require('net');
+var bouncy = require('../');
+
+test('bounce', function (t) {
+ var p0 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
+ var s0 = http.createServer(function (req, res) {
+ res.setHeader('content-type', 'text/plain');
+ res.write('beep boop');
+ res.end();
+ });
+ s0.listen(p0, connect);
+
+ var p1 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
+ var s1 = bouncy(function (req, bounce) {
+ setTimeout(function () {
+ bounce(net.connect(p0));
+ }, 100);
+ });
+ s1.listen(p1, connect);
+
+ var connected = 0;
+ function connect () {
+ if (++connected !== 2) return;
+ var opts = {
+ method : 'GET',
+ host : 'localhost',
+ port : p1,
+ path : '/beep',
+ headers : { connection : 'close' }
+ };
+ var req = http.request(opts, function (res) {
+ t.equal(res.statusCode, 200)
+ t.equal(res.headers['content-type'], 'text/plain');
+
+ var data = '';
+ res.on('data', function (buf) {
+ data += buf.toString();
+ });
+
+ res.on('end', function () {
+ t.equal(data, 'beep boop');
+ s0.close();
+ s1.close();
+ t.end();
+ });
+ });
+ req.end();
+ }
+});

0 comments on commit 03d4bd3

Please sign in to comment.