express-http-proxy
Advanced tools
Comparing version 0.1.1 to 0.2.0
47
index.js
@@ -35,2 +35,3 @@ var assert = require('assert'); | ||
var intercept = options.intercept; | ||
var decorateRequest = options.decorateRequest; | ||
var forwardPath = options.forwardPath; | ||
@@ -53,11 +54,7 @@ var filter = options.filter; | ||
length: req.headers['content-length'], | ||
limit: '1mb', // let options do here? | ||
limit: '1mb' // TODO let options do here? | ||
}, function(err, bodyContent) { | ||
if (err) return next(err) | ||
if (err) return next(err); | ||
if (bodyContent.length) | ||
hds['content-length'] = bodyContent.length | ||
var chunks = []; | ||
var realRequest = http.request({ | ||
var reqOpt = { | ||
hostname: (typeof host == 'function') ? host(req) : host.toString(), | ||
@@ -67,5 +64,20 @@ port: port, | ||
method: req.method, | ||
path: path | ||
}, function(rsp) { | ||
path: path, | ||
bodyContent: bodyContent | ||
}; | ||
if (decorateRequest) | ||
reqOpt = decorateRequest(reqOpt) || reqOpt; | ||
bodyContent = reqOpt.bodyContent; | ||
delete reqOpt.bodyContent; | ||
if (typeof bodyContent == 'string') | ||
reqOpt.headers['content-length'] = Buffer.byteLength(bodyContent); | ||
else if (Buffer.isBuffer(bodyContent)) // Buffer | ||
reqOpt.headers['content-length'] = bodyContent.length; | ||
var chunks = []; | ||
var realRequest = http.request(reqOpt, function(rsp) { | ||
var rspData = null; | ||
@@ -88,2 +100,16 @@ rsp.on('data', function(chunk) { | ||
} | ||
if (typeof rsp == 'string') | ||
rsp = new Buffer(rsp, 'utf8'); | ||
if (!Buffer.isBuffer(rsp)) { | ||
next(new Error("intercept should return string or buffer as data")); | ||
} | ||
if (!res.headersSent) | ||
res.set('content-length', rsp.length); | ||
else if (rsp.length != rspData.length) { | ||
next(new Error("'Content-Length' is already sent, the length of response data can not be changed")); | ||
} | ||
if (!sent) | ||
@@ -107,2 +133,3 @@ res.send(rsp); | ||
} | ||
}); | ||
@@ -133,2 +160,2 @@ | ||
return obj; | ||
}; | ||
} |
{ | ||
"name": "express-http-proxy", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "http proxy middleware for express", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
"test": "npm run lint && ./node_modules/.bin/mocha -R spec test/test.js", | ||
"lint": "./node_modules/.bin/jshint index.js test/*.js" | ||
}, | ||
@@ -28,5 +29,6 @@ "repository": { | ||
"devDependencies": { | ||
"supertest": "^0.13.0", | ||
"express": "^4.3.1", | ||
"mocha": "^1.19.0" | ||
"jshint": "^2.5.5", | ||
"mocha": "^1.19.0", | ||
"supertest": "^0.13.0" | ||
}, | ||
@@ -36,3 +38,20 @@ "dependencies": { | ||
"raw-body": "^1.1.6" | ||
} | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Wei Gao", | ||
"email": "jky239@gmail.com", | ||
"url": "https://github.com/villadora" | ||
}, | ||
{ | ||
"name": "Jérémy Lal", | ||
"email": "kapouer@melix.org", | ||
"url": "https://github.com/kapouer" | ||
}, | ||
{ | ||
"name": "Saulius Menkevičius", | ||
"email": null, | ||
"url": "https://github.com/razzmatazz" | ||
} | ||
] | ||
} |
@@ -43,3 +43,3 @@ # express-http-proxy [![NPM version](https://badge.fury.io/js/express-http-proxy.svg)](http://badge.fury.io/js/express-http-proxy) [![Build Status](https://travis-ci.org/villadora/express-http-proxy.svg?branch=master)](https://travis-ci.org/villadora/express-http-proxy) [![Dependency Status](https://gemnasium.com/villadora/express-http-proxy.svg)](https://gemnasium.com/villadora/express-http-proxy) | ||
You can also intercept the response get by proxy before send it back to the client: | ||
You can also intercept the response get by proxy before send it back to the client, or change the request options before it get sent to target: | ||
@@ -53,3 +53,9 @@ ```js | ||
data = JSON.parse(data.toString('utf8')); | ||
callback(null, JSON.stringify(data.obj)); | ||
callback(null, JSON.stringify(data)); | ||
}, | ||
decorateRequest: function(req) { | ||
req.headers['Content-Type'] = ''; | ||
req.method = 'GET"; | ||
req.bodyContent = wrap(req.bodyContent); | ||
return req; | ||
} | ||
@@ -65,2 +71,2 @@ })); | ||
MIT | ||
<!-- do not want to make nodeinit to complicated, you can edit this whenever you want. --> | ||
<!-- do not want to make nodeinit to complicated, you can edit this whenever you want. --> |
197
test/test.js
var assert = require('assert'); | ||
var express = require('express'), | ||
request = require('supertest'), | ||
proxy = require('../'); | ||
var express = require('express'); | ||
var request = require('supertest'); | ||
var proxy = require('../'); | ||
describe('bunyan-logger', function() { | ||
this.timeout(10000); | ||
describe('http-proxy', function() { | ||
this.timeout(10000); | ||
var app; | ||
beforeEach(function() { | ||
app = express(); | ||
app.use(proxy('httpbin.org')); | ||
}); | ||
var app; | ||
beforeEach(function() { | ||
app = express(); | ||
app.use(proxy('httpbin.org')); | ||
}); | ||
describe('test intercept & decorateRequest', function() { | ||
it('decorateRequest', function(done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
decorateRequest: function(req) { | ||
req.path = '/ip'; | ||
req.bodyContent = 'data'; | ||
} | ||
})); | ||
describe('test proxy cookie', function() { | ||
it('set cookie', function(done) { | ||
request(app) | ||
.get('/cookies/set?mycookie=value') | ||
.end(function(err, res) { | ||
assert(res.headers['set-cookie']); | ||
done(err); | ||
}); | ||
request(app) | ||
.get('/user-agent') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
assert(res.body.origin); | ||
done(); | ||
}); | ||
}); | ||
describe('test proxy status', function() { | ||
[304, 404, 200, 401, 500].forEach(function(status) { | ||
it(status, function(done) { | ||
request(app) | ||
.get('/status/' + status) | ||
.expect(status, done); | ||
}); | ||
it('intercept', function(done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
intercept: function(data, req, res, cb) { | ||
data = JSON.parse(data.toString('utf8')); | ||
data.intercepted = true; | ||
cb(null, JSON.stringify(data)); | ||
} | ||
})); | ||
request(app) | ||
.get('/ip') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
assert(res.body.intercepted); | ||
done(); | ||
}); | ||
}); | ||
it('test proxy get', function(done) { | ||
request(app) | ||
.get('/get') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
assert(/node-superagent/.test(res.body.headers['User-Agent'])); | ||
assert.equal(res.body.url, 'http://httpbin.org/get'); | ||
done(err); | ||
}); | ||
}); | ||
describe('test proxy cookie', function() { | ||
it('set cookie', function(done) { | ||
request(app) | ||
.get('/cookies/set?mycookie=value') | ||
.end(function(err, res) { | ||
assert(res.headers['set-cookie']); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
it('test proxy post', function(done) { | ||
describe('test proxy status', function() { | ||
[304, 404, 200, 401, 500].forEach(function(status) { | ||
it(status, function(done) { | ||
request(app) | ||
.post('/post') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
.get('/status/' + status) | ||
.expect(status, done); | ||
}); | ||
}); | ||
}); | ||
it('test proxy get', function(done) { | ||
request(app) | ||
.get('/get') | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
assert(/node-superagent/.test(res.body.headers['User-Agent'])); | ||
assert.equal(res.body.url, 'http://httpbin.org/get'); | ||
done(err); | ||
}); | ||
}); | ||
it('test proxy post', function(done) { | ||
request(app) | ||
.post('/post') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
}); | ||
it('test proxy put', function(done) { | ||
request(app) | ||
.put('/put') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
}); | ||
it('test proxy put', function(done) { | ||
request(app) | ||
.put('/put') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
it('test proxy patch', function(done) { | ||
request(app) | ||
.patch('/patch') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
it('test proxy delete', function(done) { | ||
request(app) | ||
.del('/delete') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
it('test proxy patch', function(done) { | ||
request(app) | ||
.patch('/patch') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('test proxy delete', function(done) { | ||
request(app) | ||
.del('/delete') | ||
.send({ | ||
mypost: 'hello' | ||
}) | ||
.end(function(err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
done(err); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11864
243
70
4