Skip to content

Commit

Permalink
Basic HTTP proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
oesmith committed Sep 30, 2012
0 parents commit 807ad39
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Puffing Billy

*TODO*
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var http = require('http');

module.exports = http.createServer(function (req, res) {
var proxy_req = http.request({
hostname: req.headers['host'],
path: req.url,
method: req.method,
headers: req.headers
});
proxy_req.addListener('response', function (proxy_res) {
proxy_res.addListener('data', function(chunk) {
res.write(chunk, 'binary');
});
proxy_res.addListener('end', function() {
res.end();
});
res.writeHead(proxy_res.statusCode, proxy_res.headers);
});
req.addListener('data', function(chunk) {
proxy_req.write(chunk, 'binary');
});
req.addListener('end', function() {
proxy_req.end();
});
});

12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "puffing-billy",
"version": "0.0.0",
"description": "An HTTP(S) cache for faking data in browser tests.",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": "",
"author": "Olly Smith <[email protected]>",
"license": "BSD"
}
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var assert = require('assert'),
http = require('http'),
billy = require('../index.js');

describe('puffing-billy', function () {
beforeEach(function () {
billy.listen(8080);
});

afterEach(function () {
billy.close();
});

it('should proxy stuff', function (done) {
http.get({
host: 'localhost',
port: 8080,
path: 'http://olly.oesmith.co.uk/~oliver/puffing-billy/foo.txt',
headers: {
Host: 'olly.oesmith.co.uk'
}
}, function (res) {
var body = "";
assert.equal(res.statusCode, 200);
res.on('data', function (data) {
body += data;
});
res.on('end', function () {
assert.equal(body, 'foobar\n');
done();
});
res.on('error', function () {
assert(false, 'request failed');
done();
});
});
});
});

0 comments on commit 807ad39

Please sign in to comment.