Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
jergason committed Dec 8, 2014
1 parent 5267c20 commit e0aa003
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var request = require('co-request');
var replacePathParams = require('./lib/replace');
var debug = require('debug')('pixie-proxy');

var hasColons = /:/;

Expand All @@ -21,13 +22,22 @@ function pixie(options) {
requestOpts.url = option.host + replacePathParams(path, this.params);
}

// something possibly went wrong if they have no body but are sending a
// put or a post
if ((requestOpts.method == 'POST' || requestOpts.method == 'PUT') && !this.request.body) {
console.warn('sending PUT or POST but no request body found');
}

// LOL WE JSON NOW
if (this.request.body) {
requestOpts.json = true;
requestOpts.body = this.request.body;
}

debug(requestOpts);

var response = yield request(requestOpts);
debug(response);

// Proxy over response headers
Object.keys(response.headers).forEach(function(h) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --harmony test/*"
"test": "mocha --harmony -R dot test/*"
},
"author": "",
"license": "ISC",
"dependencies": {
"co-request": "0.2.0",
"koa-router": "3.7.0",
"debug": "2.1.0",
"path-to-regexp": "1.0.1"
},
"devDependencies": {
"co": "4.0.1",
"koa": "0.13.0",
"koa-body": "0.4.0",
"koa-router": "3.7.0",
"mocha": "2.0.1",
"supertest": "0.15.0"
}
Expand Down
9 changes: 3 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ function makeTestServer() {
this.body = {hurp: 'durp'}
});
app.post('/hurp', function* () {
console.log('this.request.body is', this.request.body);
this.set('x-some-dumb-header', 'Im-set-yo');
this.body = this.request.body;
this.set('X-Some-Dumb-Header', 'Im-set-yo');
});
return http.createServer(app.callback())
}
Expand Down Expand Up @@ -52,14 +51,13 @@ describe('pixie-proxy', function() {
});

it('proxies POST requests', function(done) {
console.log('did i get here');
var testServer = makeTestServer();
var PORT = getRandomPort();
testServer.listen(PORT, function() {

var app = koa();
app.use(router(app));
app.use(body());
app.use(router(app));

var proxy = pixie({host: 'http://localhost:' + PORT});
var postBody = {bestHobbit: 'Yolo Swaggins'};
Expand All @@ -72,8 +70,7 @@ describe('pixie-proxy', function() {
.end(function(err, res) {
assert.ifError(err);
assert.deepEqual(res.body, postBody);
console.log('res.headers is', res.headers)
assert.equal(res.headers['X-Some-Dumb-Header'], 'Im-set-yo');
assert.equal(res.headers['x-some-dumb-header'], 'Im-set-yo');
testServer.close();
done();
});
Expand Down

0 comments on commit e0aa003

Please sign in to comment.