Permalink
Browse files

x-forwarded tests pass

  • Loading branch information...
1 parent 51899cd commit 7c7788df58fe70bb612bb8a6692ad13ff0ba8f51 @substack committed Oct 11, 2011
Showing with 53 additions and 1 deletion.
  1. +2 −1 index.js
  2. +51 −0 test/x_headers.js
View
@@ -51,7 +51,8 @@ var handler = bouncy.handler = function (cb, c) {
opts.headers['x-forwarded-for'] = c.remoteAddress;
}
if (!opts.headers.hasOwnProperty('x-forwarded-port')) {
- opts.headers['x-forwarded-port'] = c.remotePort;
+ var m = (req.headers.host || '').match(/:(\d+)/);
+ opts.headers['x-forwarded-port'] = m && m[1] || 80;
}
if (!opts.headers.hasOwnProperty('x-forwarded-proto')) {
opts.headers['x-forwarded-proto'] = 'http';
View
@@ -0,0 +1,51 @@
+var test = require('tap').test;
+var http = require('http');
+var net = require('net');
+var bouncy = require('../');
+
+test('check for x-forwarded default headers', function (t) {
+ t.plan(6);
+
+ 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');
+ t.equal(req.headers['x-forwarded-for'], '127.0.0.1');
+ t.equal(req.headers['x-forwarded-port'], p1.toString());
+ t.equal(req.headers['x-forwarded-proto'], 'http');
+ res.end();
+ });
+ s0.listen(p0, connect);
+
+ var p1 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
+ var s1 = bouncy(function (req, bounce) { bounce(p0) });
+ s1.listen(p1, connect);
+
+ var connected = 0;
+ function connect () {
+ if (++connected !== 2) return;
+ var opts = {
+ method : 'GET',
+ host : 'localhost',
+ port : p1,
+ path : '/'
+ };
+ 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 7c7788d

Please sign in to comment.