Initial import from local backup (Documents-Playground/pakerpale)
This commit is contained in:
160
node_modules/express-http-proxy/test/bodyEncoding.js
generated
vendored
Normal file
160
node_modules/express-http-proxy/test/bodyEncoding.js
generated
vendored
Normal file
@@ -0,0 +1,160 @@
|
||||
'use strict';
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var fs = require('fs');
|
||||
var os = require('os');
|
||||
var proxy = require('../');
|
||||
var startProxyTarget = require('./support/proxyTarget');
|
||||
|
||||
|
||||
describe('body encoding', function () {
|
||||
var server;
|
||||
|
||||
before(function () {
|
||||
server = startProxyTarget(8109, 1000);
|
||||
});
|
||||
|
||||
after(function () {
|
||||
server.close();
|
||||
});
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var pngHex = '89504e470d0a1a0a0' +
|
||||
'000000d4948445200' +
|
||||
'00000100000001080' +
|
||||
'60000001f15c48900' +
|
||||
'00000a49444154789' +
|
||||
'c6300010000050001' +
|
||||
'0d0a2db4000000004' +
|
||||
'9454e44ae426082';
|
||||
var pngData = new Buffer(pngHex, 'hex');
|
||||
|
||||
it('allow raw data', function (done) {
|
||||
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
|
||||
var app = express();
|
||||
|
||||
app.use(proxy('localhost:8109', {
|
||||
reqBodyEncoding: null,
|
||||
proxyReqBodyDecorator: function (bodyContent) {
|
||||
assert((new Buffer(bodyContent).toString('hex')).indexOf(pngData.toString('hex')) >= 0,
|
||||
'body should contain same data');
|
||||
return bodyContent;
|
||||
}
|
||||
}));
|
||||
|
||||
fs.writeFile(filename, pngData, function (err) {
|
||||
if (err) { throw err; }
|
||||
request(app)
|
||||
.post('/post')
|
||||
.attach('image', filename)
|
||||
.end(function (err) {
|
||||
fs.unlinkSync(filename);
|
||||
// This test is both broken and I think unnecessary.
|
||||
// Its broken because http.bin no longer supports /post, but this test assertion is based on the old
|
||||
// httpbin behavior.
|
||||
// The assertion in the decorateRequest above verifies the test title.
|
||||
//var response = new Buffer(res.body.attachment.data).toString('base64');
|
||||
//assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data');
|
||||
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('when user sets parseReqBody', function () {
|
||||
it('should not parse body', function (done) {
|
||||
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
|
||||
var app = express();
|
||||
app.use(proxy('localhost:8109', {
|
||||
parseReqBody: false,
|
||||
proxyReqBodyDecorator: function (bodyContent) {
|
||||
assert(!bodyContent, 'body content should not be parsed.');
|
||||
return bodyContent;
|
||||
}
|
||||
}));
|
||||
|
||||
fs.writeFile(filename, pngData, function (err) {
|
||||
if (err) { throw err; }
|
||||
request(app)
|
||||
.post('/post')
|
||||
.attach('image', filename)
|
||||
.end(function (err) {
|
||||
fs.unlinkSync(filename);
|
||||
// This test is both broken and I think unnecessary.
|
||||
// Its broken because http.bin no longer supports /post, but this test assertion is based on the old
|
||||
// httpbin behavior.
|
||||
// The assertion in the decorateRequest above verifies the test title.
|
||||
// var response = new Buffer(res.body.attachment.data).toString('base64');
|
||||
// assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data');
|
||||
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should not fail on large limit', function (done) {
|
||||
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
|
||||
var app = express();
|
||||
app.use(proxy('localhost:8109', {
|
||||
parseReqBody: false,
|
||||
limit: '20gb',
|
||||
}));
|
||||
fs.writeFile(filename, pngData, function (err) {
|
||||
if (err) { throw err; }
|
||||
request(app)
|
||||
.post('/post')
|
||||
.attach('image', filename)
|
||||
.end(function (err) {
|
||||
fs.unlinkSync(filename);
|
||||
assert(err === null);
|
||||
// This test is both broken and I think unnecessary.
|
||||
// Its broken because http.bin no longer supports /post, but this test assertion is based on the old
|
||||
// httpbin behavior.
|
||||
// The assertion in the decorateRequest above verifies the test title.
|
||||
//var response = new Buffer(res.body.attachment.data).toString('base64');
|
||||
//assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data');
|
||||
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should fail with an error when exceeding limit', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('localhost:8109', {
|
||||
limit: 1,
|
||||
}));
|
||||
// silence jshint warning about unused vars - express error handler *needs* 4 args
|
||||
app.use(function (err, req, res, next) { // eslint-disable-line no-unused-vars
|
||||
res.json(err);
|
||||
});
|
||||
request(app)
|
||||
.post('/post')
|
||||
.send({ some: 'json' })
|
||||
.end(function (err, response) {
|
||||
assert(response.body.message === 'request entity too large');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('when user sets reqBodyEncoding', function () {
|
||||
it('should set the accepts-charset header', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('localhost:8109', {
|
||||
reqBodyEncoding: 'utf-16'
|
||||
}));
|
||||
request(app)
|
||||
.get('/headers')
|
||||
.end(function (err, res) {
|
||||
if (err) { throw err; }
|
||||
assert.equal(res.body.headers['accept-charset'], 'utf-16');
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
55
node_modules/express-http-proxy/test/catchingErrors.js
generated
vendored
Normal file
55
node_modules/express-http-proxy/test/catchingErrors.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('when server responds with an error', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
var slowTarget;
|
||||
var serverReference;
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
serverReference.close();
|
||||
});
|
||||
|
||||
var STATUS_CODES = [
|
||||
{ code: 403, text: 'Forbidden', toString: 'Error: cannot GET /proxy (403)' },
|
||||
{ code: 404, text: 'Not Found', toString: 'Error: cannot GET /proxy (404)' },
|
||||
{ code: 500, text: 'Internal Server Error', toString: 'Error: cannot GET /proxy (500)' }
|
||||
];
|
||||
|
||||
STATUS_CODES.forEach(function (statusCode) {
|
||||
it('express-http-proxy responds with ' + statusCode.text +
|
||||
'when proxy server responds ' + statusCode.code, function (done) {
|
||||
slowTarget = express();
|
||||
slowTarget.use(function (req, res) { res.sendStatus(statusCode.code); });
|
||||
serverReference = slowTarget.listen(12345);
|
||||
|
||||
app.use('/proxy', proxy('http://127.0.0.1:12345', {
|
||||
reqAsBuffer: true,
|
||||
reqBodyEncoding: null,
|
||||
parseReqBody: false
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/proxy')
|
||||
.expect(statusCode.code)
|
||||
.end(function (err, res) {
|
||||
assert(err === null);
|
||||
assert(res.error);
|
||||
assert(res.error.text === statusCode.text);
|
||||
assert(res.error.toString() === statusCode.toString);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
48
node_modules/express-http-proxy/test/cookies.js
generated
vendored
Normal file
48
node_modules/express-http-proxy/test/cookies.js
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
var proxyRouteFn = [{
|
||||
method: 'get',
|
||||
path: '/cookieTest',
|
||||
fn: function (req, res) {
|
||||
Object.keys(req.cookies).forEach(function (key) {
|
||||
res.cookie(key, req.cookies[key]);
|
||||
});
|
||||
res.sendStatus(200);
|
||||
}
|
||||
}];
|
||||
|
||||
describe('proxies cookie', function () {
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
var proxyServer;
|
||||
|
||||
beforeEach(function () {
|
||||
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
|
||||
app = express();
|
||||
app.use(proxy('localhost:12346'));
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
it('set cookie', function (done) {
|
||||
request(app)
|
||||
.get('/cookieTest')
|
||||
.set('Cookie', 'myApp-token=12345667')
|
||||
.end(function (err, res) {
|
||||
var cookiesMatch = res.headers['set-cookie'].filter(function (item) {
|
||||
return item.match(/myApp-token=12345667/);
|
||||
});
|
||||
assert(cookiesMatch);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
50
node_modules/express-http-proxy/test/decorateUserResHeaders.js
generated
vendored
Normal file
50
node_modules/express-http-proxy/test/decorateUserResHeaders.js
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('when userResHeaderDecorator is defined', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
var serverReference;
|
||||
|
||||
afterEach(function () {
|
||||
serverReference.close();
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
var pTarget = express();
|
||||
pTarget.use(function (req, res) { res.json(req.headers); });
|
||||
serverReference = pTarget.listen(12345);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
serverReference.close();
|
||||
});
|
||||
|
||||
it('provides an interface for updating headers', function (done) {
|
||||
|
||||
app.use('/proxy', proxy('http://127.0.0.1:12345', {
|
||||
userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) {
|
||||
headers.boltedonheader = 'franky';
|
||||
return headers;
|
||||
}
|
||||
}));
|
||||
|
||||
app.use(function (req, res) {
|
||||
res.sendStatus(200);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/proxy')
|
||||
.expect(function (res) {
|
||||
assert(res.headers.boltedonheader === 'franky');
|
||||
})
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
113
node_modules/express-http-proxy/test/filter.js
generated
vendored
Normal file
113
node_modules/express-http-proxy/test/filter.js
generated
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
var proxyRouteFn = [{
|
||||
method: 'get',
|
||||
path: '/',
|
||||
fn: function (req, res) {
|
||||
return res.status(209).send('Proxy Server');
|
||||
}
|
||||
}];
|
||||
|
||||
function nextMethod(req, res, next) {
|
||||
res.status(201).send('Client Application');
|
||||
next();
|
||||
}
|
||||
|
||||
describe('filter', function () {
|
||||
var app = express();
|
||||
var proxyServer;
|
||||
|
||||
beforeEach(function () {
|
||||
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
|
||||
app = express();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
describe('when filter function returns true', function () {
|
||||
it('continues route processing', function (done) {
|
||||
// should capture every possible path
|
||||
|
||||
app.use(proxy('localhost:12346', {
|
||||
filter: function () { return true; }
|
||||
}));
|
||||
|
||||
// because prior line should *always* fire and return, we should never get here.
|
||||
|
||||
app.use(nextMethod);
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(209)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when filter function returns false', function () {
|
||||
|
||||
it('hanldes route processing', function (done) {
|
||||
// should capture every possible path
|
||||
|
||||
app.use(proxy('localhost:12346', {
|
||||
filter: function () { return false; }
|
||||
}));
|
||||
|
||||
// because prior line should *always* fire skip, we should get here.
|
||||
|
||||
app.use(nextMethod);
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(201)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('promise form', function () {
|
||||
describe('when filter function returns true', function () {
|
||||
it('continues route processing', function (done) {
|
||||
// should capture every possible path
|
||||
|
||||
app.use(proxy('localhost:12346', {
|
||||
filter: function () { return new Promise(function (resolve) { resolve(true); }); }
|
||||
}));
|
||||
|
||||
// because prior line should *always* fire and return, we should never get here.
|
||||
|
||||
app.use(nextMethod);
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(209)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when filter function returns false', function () {
|
||||
|
||||
it('hanldes route processing', function (done) {
|
||||
// should capture every possible path
|
||||
|
||||
app.use(proxy('localhost:12346', {
|
||||
filter: function () { return new Promise(function (resolve) { resolve(false); }); }
|
||||
}));
|
||||
|
||||
// because prior line should *always* fire skip, we should get here.
|
||||
|
||||
app.use(nextMethod);
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(201)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
136
node_modules/express-http-proxy/test/handleProxyError.js
generated
vendored
Normal file
136
node_modules/express-http-proxy/test/handleProxyError.js
generated
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
var proxyRouteFn = [{
|
||||
method: 'get',
|
||||
path: '/:errorCode',
|
||||
fn: function (req, res) {
|
||||
var errorCode = req.params.errorCode;
|
||||
if (errorCode === 'timeout') {
|
||||
return res.status(504).send('mock timeout');
|
||||
}
|
||||
return res.status(parseInt(errorCode)).send('test case error');
|
||||
}
|
||||
}];
|
||||
|
||||
describe('error handling can be over-ridden by user', function () {
|
||||
var app = express();
|
||||
var proxyServer;
|
||||
|
||||
beforeEach(function () {
|
||||
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
|
||||
app = express();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
describe('when user provides a null function', function () {
|
||||
|
||||
describe('when author sets a timeout that fires', function () {
|
||||
it('passes 504 directly to client', function (done) {
|
||||
app.use(proxy('localhost:12346', {
|
||||
timeout: 1,
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/200')
|
||||
.expect(504)
|
||||
.expect('X-Timeout-Reason', 'express-http-proxy reset the request.')
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
it('passes status code (e.g. 504) directly to the client', function (done) {
|
||||
app.use(proxy('localhost:12346'));
|
||||
request(app)
|
||||
.get('/504')
|
||||
.expect(504)
|
||||
.expect(function (res) {
|
||||
assert(res.text === 'test case error');
|
||||
return res;
|
||||
})
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('passes status code (e.g. 500) back to the client', function (done) {
|
||||
app.use(proxy('localhost:12346'));
|
||||
request(app)
|
||||
.get('/500')
|
||||
.expect(500)
|
||||
.end(function (err, res) {
|
||||
assert(res.text === 'test case error');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when user provides a handler function', function () {
|
||||
var intentionallyWeirdStatusCode = 399;
|
||||
var intentionallyQuirkyStatusMessage = 'monkey skunky';
|
||||
|
||||
describe('when author sets a timeout that fires', function () {
|
||||
it('allows author to skip handling and handle in application step', function (done) {
|
||||
app.use(proxy('localhost:12346', {
|
||||
timeout: 1,
|
||||
proxyErrorHandler: function (err, res, next) {
|
||||
next(err);
|
||||
}
|
||||
}));
|
||||
|
||||
app.use(function (err, req, res, next) { // eslint-disable-line no-unused-vars
|
||||
if (err.code === 'ECONNRESET') {
|
||||
res.status(intentionallyWeirdStatusCode).send(intentionallyQuirkyStatusMessage);
|
||||
}
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/200')
|
||||
.expect(function (res) {
|
||||
assert(res.text === intentionallyQuirkyStatusMessage);
|
||||
return res;
|
||||
})
|
||||
.expect(intentionallyWeirdStatusCode)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
it('allows authors to sub in their own handling', function (done) {
|
||||
app.use(proxy('localhost:12346', {
|
||||
timeout: 1,
|
||||
proxyErrorHandler: function (err, res, next) {
|
||||
switch (err && err.code) {
|
||||
case 'ECONNRESET': { return res.status(405).send('504 became 405'); }
|
||||
case 'ECONNREFUSED': { return res.status(200).send('gotcher back'); }
|
||||
default: { next(err); }
|
||||
}
|
||||
} }));
|
||||
|
||||
request(app)
|
||||
.get('/timeout')
|
||||
.expect(405)
|
||||
.expect(function (res) {
|
||||
assert(res.text === '504 became 405');
|
||||
return res;
|
||||
})
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('passes status code (e.g. 500) back to the client', function (done) {
|
||||
app.use(proxy('localhost:12346'));
|
||||
request(app)
|
||||
.get('/500')
|
||||
.expect(500)
|
||||
.end(function (err, res) {
|
||||
assert(res.text === 'test case error');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
45
node_modules/express-http-proxy/test/headers.js
generated
vendored
Normal file
45
node_modules/express-http-proxy/test/headers.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('proxies headers', function () {
|
||||
this.timeout(10000);
|
||||
|
||||
var http;
|
||||
|
||||
beforeEach(function () {
|
||||
http = express();
|
||||
http.use(proxy('http://httpbin.org', {
|
||||
headers: {
|
||||
'X-Current-president': 'taft'
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
it('passed as options', function (done) {
|
||||
request(http)
|
||||
.get('/headers')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert(res.body.headers['X-Current-President'] === 'taft');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('passed as on request', function (done) {
|
||||
request(http)
|
||||
.get('/headers')
|
||||
.set('X-Powerererer', 'XTYORG')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert(res.body.headers['X-Powerererer']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
70
node_modules/express-http-proxy/test/host.js
generated
vendored
Normal file
70
node_modules/express-http-proxy/test/host.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('host can be a dynamic function', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app = express();
|
||||
describe('and memoization can be disabled', function () {
|
||||
var firstProxyApp = express();
|
||||
var secondProxyApp = express();
|
||||
// TODO: This seems like a bug factory. We will have intermittent port conflicts, yeah?
|
||||
|
||||
function randomNumberInPortRange() {
|
||||
return Math.floor(Math.random() * 48000) + 1024;
|
||||
}
|
||||
var firstPort = randomNumberInPortRange();
|
||||
var secondPort = randomNumberInPortRange();
|
||||
|
||||
var hostFn = function (req) {
|
||||
return 'localhost:' + req.params.port;
|
||||
};
|
||||
|
||||
app.use('/proxy/:port', proxy(hostFn, { memoizeHost: false }));
|
||||
|
||||
firstProxyApp
|
||||
.get('/', function (req, res) { res.sendStatus(204); })
|
||||
.listen(firstPort);
|
||||
|
||||
secondProxyApp
|
||||
.get('/', function (req, res) { res.sendStatus(200); })
|
||||
.listen(secondPort);
|
||||
|
||||
it('when not memoized, host resolves to a second value on the seecond call', function (done) {
|
||||
request(app)
|
||||
.get('/proxy/' + firstPort)
|
||||
.expect(204)
|
||||
.end(function (err) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
request(app)
|
||||
.get('/proxy/' + secondPort)
|
||||
.expect(200, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('host can be an ip address', function () {
|
||||
it('with a port', function (done) {
|
||||
var app = express();
|
||||
app.use('/proxy/', proxy('127.0.0.1:3020'));
|
||||
|
||||
var targetApp = express();
|
||||
targetApp
|
||||
.get('/', function (req, res) { res.sendStatus(211); })
|
||||
.listen(3020);
|
||||
|
||||
|
||||
request(app)
|
||||
.get('/proxy/')
|
||||
.expect(211)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
59
node_modules/express-http-proxy/test/https.js
generated
vendored
Normal file
59
node_modules/express-http-proxy/test/https.js
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('proxies https', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
});
|
||||
|
||||
function assertSecureRequest(app, done) {
|
||||
request(app)
|
||||
.get('/get?show_env=1')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert(res.body.headers['X-Forwarded-Port'] === '443', 'Expects forwarded 443 Port');
|
||||
assert(res.body.headers['X-Forwarded-Proto'] === 'https', 'Expects forwarded protocol to be https');
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
describe('when host is a String', function () {
|
||||
describe('and includes "https" as protocol', function () {
|
||||
it('proxys via https', function (done) {
|
||||
app.use(proxy('https://httpbin.org'));
|
||||
assertSecureRequest(app, done);
|
||||
});
|
||||
});
|
||||
describe('option https is set to "true"', function () {
|
||||
it('proxys via https', function (done) {
|
||||
app.use(proxy('http://httpbin.org', { https: true }));
|
||||
assertSecureRequest(app, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when host is a Function', function () {
|
||||
describe('returned value includes "https" as protocol', function () {
|
||||
it('proxys via https', function (done) {
|
||||
app.use(proxy(function () { return 'https://httpbin.org'; }));
|
||||
assertSecureRequest(app, done);
|
||||
});
|
||||
});
|
||||
describe('option https is set to "true"', function () {
|
||||
it('proxys via https', function (done) {
|
||||
app.use(proxy(function () { return 'http://httpbin.org'; }, { https: true }));
|
||||
assertSecureRequest(app, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
57
node_modules/express-http-proxy/test/maybeSkipToNextHandler.js
generated
vendored
Normal file
57
node_modules/express-http-proxy/test/maybeSkipToNextHandler.js
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var http = require('http');
|
||||
var assert = require('assert');
|
||||
|
||||
describe('when skipToNextHandlerFilter is defined', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
var slowTarget;
|
||||
var serverReference;
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
slowTarget = express();
|
||||
slowTarget.use(function (req, res) { res.sendStatus(404); });
|
||||
serverReference = slowTarget.listen(12345);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
serverReference.close();
|
||||
});
|
||||
|
||||
var OUTCOMES = [
|
||||
{ shouldSkip: true, expectedStatus: 200 },
|
||||
{ shouldSkip: false, expectedStatus: 404 }
|
||||
];
|
||||
|
||||
OUTCOMES.forEach(function (outcome) {
|
||||
describe('and returns ' + outcome.shouldSkip, function () {
|
||||
it('express-http-proxy' + (outcome.shouldSkip ? ' skips ' : ' doesnt skip ') + 'to next()', function (done) {
|
||||
|
||||
app.use('/proxy', proxy('http://127.0.0.1:12345', {
|
||||
skipToNextHandlerFilter: function (/*res*/) {
|
||||
return outcome.shouldSkip;
|
||||
}
|
||||
}));
|
||||
|
||||
app.use(function (req, res) {
|
||||
assert(res.expressHttpProxy instanceof Object);
|
||||
assert(res.expressHttpProxy.res instanceof http.IncomingMessage);
|
||||
assert(res.expressHttpProxy.req instanceof Object);
|
||||
res.sendStatus(200);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/proxy')
|
||||
.expect(outcome.expectedStatus)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
110
node_modules/express-http-proxy/test/middlewareCompatibility.js
generated
vendored
Normal file
110
node_modules/express-http-proxy/test/middlewareCompatibility.js
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var bodyParser = require('body-parser');
|
||||
var proxy = require('../');
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
|
||||
|
||||
var proxyRouteFn = [{
|
||||
method: 'post',
|
||||
path: '/poster',
|
||||
fn: function (req, res) {
|
||||
res.send(req.body);
|
||||
}
|
||||
}];
|
||||
|
||||
describe('middleware compatibility', function () {
|
||||
var proxyServer;
|
||||
|
||||
beforeEach(function () {
|
||||
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
it('should use req.body if defined', function (done) {
|
||||
var app = express();
|
||||
|
||||
// Simulate another middleware that puts req stream into the body
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
var received = [];
|
||||
req.on('data', function onData(chunk) {
|
||||
if (!chunk) { return; }
|
||||
received.push(chunk);
|
||||
});
|
||||
req.on('end', function onEnd() {
|
||||
received = Buffer.concat(received).toString('utf8');
|
||||
req.body = JSON.parse(received);
|
||||
req.body.foo = 1;
|
||||
next();
|
||||
});
|
||||
});
|
||||
|
||||
app.use(proxy('localhost:12346', {
|
||||
intercept: function (rsp, data, req) {
|
||||
assert(req.body);
|
||||
assert.equal(req.body.foo, 1);
|
||||
assert.equal(req.body.mypost, 'hello');
|
||||
return data;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.post('/poster')
|
||||
.send({ mypost: 'hello' })
|
||||
.expect(function (res) {
|
||||
assert.equal(res.body.foo, 1);
|
||||
assert.equal(res.body.mypost, 'hello');
|
||||
})
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('should stringify req.body when it is a json body so it is written to proxy request', function (done) {
|
||||
var app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: false
|
||||
}));
|
||||
app.use(proxy('localhost:12346'));
|
||||
request(app)
|
||||
.post('/poster')
|
||||
.send({
|
||||
mypost: 'hello',
|
||||
doorknob: 'wrect'
|
||||
})
|
||||
.expect(function (res) {
|
||||
assert.equal(res.body.doorknob, 'wrect');
|
||||
assert.equal(res.body.mypost, 'hello');
|
||||
})
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('should convert req.body to a Buffer when reqAsBuffer is set', function (done) {
|
||||
var app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: false
|
||||
}));
|
||||
app.use(proxy('localhost:12346', {
|
||||
reqAsBuffer: true
|
||||
}));
|
||||
request(app)
|
||||
.post('/poster')
|
||||
.send({
|
||||
mypost: 'hello',
|
||||
doorknob: 'wrect'
|
||||
})
|
||||
.expect(function (res) {
|
||||
assert.equal(res.body.doorknob, 'wrect');
|
||||
assert.equal(res.body.mypost, 'hello');
|
||||
})
|
||||
.end(done);
|
||||
});
|
||||
|
||||
});
|
||||
41
node_modules/express-http-proxy/test/params.js
generated
vendored
Normal file
41
node_modules/express-http-proxy/test/params.js
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
var proxyRouteFn = [{
|
||||
method: 'get',
|
||||
path: '/test',
|
||||
fn: function (req, res) {
|
||||
res.send(req.url);
|
||||
}
|
||||
}];
|
||||
|
||||
describe('proxies query parameters', function () {
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
var proxyServer;
|
||||
|
||||
beforeEach(function () {
|
||||
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
|
||||
app = express();
|
||||
app.use(proxy('localhost:12346'));
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
it('repeats query params to proxy server', function (done) {
|
||||
request(app)
|
||||
.get('/test?a=1&b=2&c=3')
|
||||
.end(function (err, res) {
|
||||
assert.equal(res.text, '/test?a=1&b=2&c=3');
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
95
node_modules/express-http-proxy/test/path.js
generated
vendored
Normal file
95
node_modules/express-http-proxy/test/path.js
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var startProxyTarget = require('./support/proxyTarget');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
describe('uses remote path', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app = express();
|
||||
var proxyRoutes = ['/somePath/', '/somePath/longer/path', '/somePath/long/path/with/many/tokens'];
|
||||
var proxyKeyPath = '/somePath';
|
||||
var server;
|
||||
|
||||
afterEach(function () {
|
||||
server.close();
|
||||
});
|
||||
|
||||
proxyRoutes.forEach(function (path) {
|
||||
it('uses path component from inbound request', function (done) {
|
||||
|
||||
var modifiedPath = path.replace(new RegExp(proxyKeyPath), '');
|
||||
|
||||
var proxyRouteFn = {
|
||||
method: 'get',
|
||||
path: modifiedPath,
|
||||
fn: function (req, res) {
|
||||
res.json({ path: path, modifiedPath: modifiedPath });
|
||||
}
|
||||
};
|
||||
|
||||
server = startProxyTarget(8309, 1000, [proxyRouteFn]);
|
||||
|
||||
app.use('/somePath/', proxy('http://localhost:8309'));
|
||||
|
||||
request(app)
|
||||
.get(path)
|
||||
.expect(200)
|
||||
.end(function (err, response) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
expect(response.path === path);
|
||||
expect(response.modifiedPath === path);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('host can be a dynamic function', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app = express();
|
||||
var firstProxyApp = express();
|
||||
var secondProxyApp = express();
|
||||
var firstPort = 10031;
|
||||
var secondPort = 10032;
|
||||
|
||||
app.use('/proxy/:port', proxy(function (req) {
|
||||
return 'localhost:' + req.params.port;
|
||||
}, {
|
||||
memoizeHost: false
|
||||
}));
|
||||
|
||||
firstProxyApp.use('/', function (req, res) {
|
||||
res.sendStatus(204);
|
||||
});
|
||||
firstProxyApp.listen(firstPort);
|
||||
|
||||
secondProxyApp.use('/', function (req, res) {
|
||||
res.sendStatus(200);
|
||||
});
|
||||
secondProxyApp.listen(secondPort);
|
||||
|
||||
it('can proxy with session value', function (done) {
|
||||
request(app)
|
||||
.get('/proxy/' + firstPort)
|
||||
.expect(204)
|
||||
.end(function (err) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
request(app)
|
||||
.get('/proxy/' + secondPort)
|
||||
.expect(200, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
84
node_modules/express-http-proxy/test/port.js
generated
vendored
Normal file
84
node_modules/express-http-proxy/test/port.js
generated
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
function proxyTarget(port) {
|
||||
|
||||
var other = express();
|
||||
other.get('/', function (req, res) {
|
||||
res.send('Success');
|
||||
});
|
||||
return other.listen(port);
|
||||
}
|
||||
|
||||
describe('proxies to requested port', function () {
|
||||
var other;
|
||||
var http;
|
||||
|
||||
beforeEach(function () {
|
||||
http = express();
|
||||
other = proxyTarget(56001);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
other.close();
|
||||
});
|
||||
|
||||
|
||||
function assertSuccess(server, done) {
|
||||
request(http)
|
||||
.get('/')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert(res.text === 'Success');
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
describe('when host is a String', function () {
|
||||
it('when passed as an option', function (done) {
|
||||
|
||||
http.use(proxy('http://localhost', {
|
||||
port: 56001
|
||||
}));
|
||||
|
||||
assertSuccess(http, done);
|
||||
});
|
||||
|
||||
it('when passed on the host string', function (done) {
|
||||
|
||||
http.use(proxy('http://localhost:56001'));
|
||||
|
||||
assertSuccess(http, done);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('when host is a function', function () {
|
||||
|
||||
|
||||
it('and port is on the String returned', function (done) {
|
||||
|
||||
http.use(proxy(
|
||||
function () { return 'http://localhost:56001'; }
|
||||
));
|
||||
|
||||
assertSuccess(http, done);
|
||||
});
|
||||
|
||||
it('and port passed as an option', function (done) {
|
||||
|
||||
http.use(proxy(
|
||||
function () { return 'http://localhost'; },
|
||||
{ port: 56001 }
|
||||
));
|
||||
|
||||
assertSuccess(http, done);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
65
node_modules/express-http-proxy/test/postBody.js
generated
vendored
Normal file
65
node_modules/express-http-proxy/test/postBody.js
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var bodyParser = require('body-parser');
|
||||
|
||||
|
||||
function createLocalApplicationServer() {
|
||||
var app = express();
|
||||
return app;
|
||||
}
|
||||
|
||||
function createProxyApplicationServer() {
|
||||
var pTarget = express();
|
||||
pTarget.use(bodyParser.json());
|
||||
pTarget.use(bodyParser.urlencoded({ extended: true }));
|
||||
pTarget.use(function (req, res) {
|
||||
assert(req.body.name === 'tobi'); //, 'Assert that the value posted to the local server is passed to the proxy');
|
||||
res.json(req.body);
|
||||
});
|
||||
return pTarget.listen(12345);
|
||||
}
|
||||
|
||||
describe('when proxy request is a POST', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var localServer;
|
||||
var proxyServer;
|
||||
|
||||
beforeEach(function () {
|
||||
localServer = createLocalApplicationServer();
|
||||
proxyServer = createProxyApplicationServer();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
var testCases = [
|
||||
{ name: 'form encoded', encoding: 'application/x-www-form-urlencoded' },
|
||||
{ name: 'JSON encoded', encoding: 'application/json' }
|
||||
];
|
||||
|
||||
testCases.forEach(function (test) {
|
||||
it('should deliver the post body when ' + test.name, function (done) {
|
||||
|
||||
localServer.use('/proxy', proxy('http://127.0.0.1:12345'));
|
||||
localServer.use(function (req, res) { res.sendStatus(200); });
|
||||
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); });
|
||||
|
||||
request(localServer)
|
||||
.post('/proxy')
|
||||
.send({ name: 'tobi' })
|
||||
.set('Content-Type', test.encoding)
|
||||
.expect(function (res) {
|
||||
assert(res.body.name === 'tobi');
|
||||
})
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
70
node_modules/express-http-proxy/test/preserveHostHdr.js
generated
vendored
Normal file
70
node_modules/express-http-proxy/test/preserveHostHdr.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
var proxyRouteFn = [{
|
||||
method: 'get',
|
||||
path: '/hostHdrTest',
|
||||
fn: function (req, res) {
|
||||
res.send(req.headers.host);
|
||||
}
|
||||
}];
|
||||
|
||||
describe('preserves host header only when requested', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
var proxyServer;
|
||||
|
||||
describe('when preserveHostHdr is true', function () {
|
||||
before(function () {
|
||||
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
|
||||
app = express();
|
||||
app.use(proxy('localhost:12346', {
|
||||
preserveHostHdr: true
|
||||
}));
|
||||
});
|
||||
|
||||
after(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
it('host is passed forward', function (done) {
|
||||
request(app)
|
||||
.get('/hostHdrTest')
|
||||
.set('host', 'hamburger-helper')
|
||||
.end(function (err, res) {
|
||||
assert(res.text === 'hamburger-helper');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when preserveHostHdr is absent or false', function () {
|
||||
before(function () {
|
||||
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
|
||||
app = express();
|
||||
app.use(proxy('localhost:12346'));
|
||||
});
|
||||
|
||||
after(function () {
|
||||
proxyServer.close();
|
||||
});
|
||||
|
||||
it('host is not passed forward', function (done) {
|
||||
request(app)
|
||||
.get('/hostHdrTest')
|
||||
.set('host', 'hamburger-helper')
|
||||
.end(function (err, res) {
|
||||
assert(res.text !== 'hamburger-helper');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
150
node_modules/express-http-proxy/test/proxyReqOptDecorator.js
generated
vendored
Normal file
150
node_modules/express-http-proxy/test/proxyReqOptDecorator.js
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var http = require('http');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
|
||||
describe('proxyReqOptDecorator', function () {
|
||||
var server;
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
before(function () {
|
||||
var handlers = [{
|
||||
method: 'get',
|
||||
path: '/working',
|
||||
fn: function (req, res) {
|
||||
res.send({ headers: req.headers });
|
||||
}
|
||||
}];
|
||||
|
||||
server = proxyTarget(12345, 100, handlers);
|
||||
});
|
||||
|
||||
after(function () {
|
||||
server.close();
|
||||
});
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
app.use(proxy('localhost:12345'));
|
||||
});
|
||||
|
||||
describe('allows authors to modify a number of request parameters', function () {
|
||||
it('modify headers', function (done) {
|
||||
app = express();
|
||||
app.use(proxy('localhost:12345', {
|
||||
proxyReqOptDecorator: function (reqOpt) {
|
||||
reqOpt.headers['user-agent'] = 'test user agent';
|
||||
reqOpt.headers.mmmmmmmmmm = 'misty mountain hop';
|
||||
return reqOpt;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/working')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
|
||||
assert.equal(res.body.headers['user-agent'], 'test user agent');
|
||||
assert.equal(res.body.headers.mmmmmmmmmm, 'misty mountain hop');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when proxyReqOptDecorator is a simple function (non Promise)', function () {
|
||||
it('can modify headers', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('localhost:12345', {
|
||||
proxyReqOptDecorator: function (reqOpt, req) {
|
||||
reqOpt.headers['user-agent'] = 'test user agent';
|
||||
assert(req instanceof http.IncomingMessage);
|
||||
return reqOpt;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/working')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert.equal(res.body.headers['user-agent'], 'test user agent');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when proxyReqOptDecorator is a Promise', function () {
|
||||
it('should mutate the proxied request', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('localhost:12345', {
|
||||
proxyReqOptDecorator: function (reqOpt, req) {
|
||||
assert(req instanceof http.IncomingMessage);
|
||||
return new Promise(function (resolve) {
|
||||
reqOpt.headers['user-agent'] = 'test user agent';
|
||||
resolve(reqOpt);
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/working')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert.equal(res.body.headers['user-agent'], 'test user agent');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the Promise is rejected', function () {
|
||||
it('returns err to host application for processing', function (done) {
|
||||
var app = express();
|
||||
|
||||
app.use(proxy('/reject-promise', {
|
||||
proxyReqOptDecorator: function () {
|
||||
return Promise.reject('An arbitrary rejection message.');
|
||||
}
|
||||
}));
|
||||
|
||||
/* jshint ignore:start */
|
||||
app.use(function (err, req, res, next) { // eslint-disable-line no-unused-vars
|
||||
assert(err === 'An arbitrary rejection message.');
|
||||
res.status(221).send(err);
|
||||
});
|
||||
/* jshint ignore:end */
|
||||
|
||||
request(app)
|
||||
.get('/reject-promise')
|
||||
.expect(221, done); // ensures we've entered the handler with assert above
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('proxyReqOptDecorator has access to the source request\'s data', function () {
|
||||
it('should have access to ip', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('localhost:12345', {
|
||||
proxyReqOptDecorator: function (reqOpts, req) {
|
||||
assert(req instanceof http.IncomingMessage);
|
||||
assert(req.ip);
|
||||
return reqOpts;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/working')
|
||||
.end(function (err) {
|
||||
if (err) { return done(err); }
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
64
node_modules/express-http-proxy/test/proxyReqPathResolver.js
generated
vendored
Normal file
64
node_modules/express-http-proxy/test/proxyReqPathResolver.js
generated
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var http = require('http');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
|
||||
var aliases = ['forwardPath', 'forwardPathAsync', 'proxyReqPathResolver'];
|
||||
|
||||
describe('resolveProxyReqPath', function () {
|
||||
var server;
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
before(function () {
|
||||
var handlers = [{
|
||||
method: 'get',
|
||||
path: '/working',
|
||||
fn: function (req, res) {
|
||||
res.sendStatus(200);
|
||||
}
|
||||
}];
|
||||
|
||||
server = proxyTarget(12345, 100, handlers);
|
||||
});
|
||||
|
||||
after(function () {
|
||||
server.close();
|
||||
});
|
||||
|
||||
aliases.forEach(function (alias) {
|
||||
describe('when author uses option ' + alias, function () {
|
||||
it('the proxy request path is the result of the function', function (done) {
|
||||
var app = express();
|
||||
var opts = {};
|
||||
opts[alias] = function () { return '/working'; };
|
||||
app.use(proxy('localhost:12345', opts));
|
||||
|
||||
request(app)
|
||||
.get('/failing')
|
||||
.expect(200)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('the ' + alias + ' method has access to request object', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('localhost:12345', {
|
||||
forwardPath: function (req) {
|
||||
assert.ok(req instanceof http.IncomingMessage);
|
||||
return '/working';
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/foobar')
|
||||
.expect(200)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
116
node_modules/express-http-proxy/test/resolveProxyReqPath.js
generated
vendored
Normal file
116
node_modules/express-http-proxy/test/resolveProxyReqPath.js
generated
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var ScopeContainer = require('../lib/scopeContainer');
|
||||
var resolveProxyReqPath = require('../app/steps/resolveProxyReqPath');
|
||||
var expect = require('chai').expect;
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
|
||||
describe('resolveProxyReqPath', function () {
|
||||
var container;
|
||||
|
||||
beforeEach(function () {
|
||||
container = new ScopeContainer();
|
||||
});
|
||||
|
||||
var tests = [
|
||||
{
|
||||
resolverType: 'undefined',
|
||||
resolverFn: undefined,
|
||||
data: [
|
||||
{ url: 'http://localhost:12345', parsed: '/' },
|
||||
{ url: 'http://g.com/123?45=67', parsed: '/123?45=67' }
|
||||
]
|
||||
},
|
||||
{
|
||||
resolverType: 'a syncronous function',
|
||||
resolverFn: function () { return 'the craziest thing'; },
|
||||
data: [
|
||||
{ url: 'http://localhost:12345', parsed: 'the craziest thing' },
|
||||
{ url: 'http://g.com/123?45=67', parsed: 'the craziest thing' }
|
||||
]
|
||||
},
|
||||
{
|
||||
resolverType: 'a Promise',
|
||||
resolverFn: function () {
|
||||
return new Promise(function (resolve) {
|
||||
resolve('the craziest think');
|
||||
});
|
||||
},
|
||||
data: [
|
||||
{ url: 'http://localhost:12345', parsed: 'the craziest think' },
|
||||
{ url: 'http://g.com/123?45=67', parsed: 'the craziest think' }
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
describe('when proxyReqPathResolver', function () {
|
||||
|
||||
tests.forEach(function (test) {
|
||||
describe('is ' + test.resolverType, function () {
|
||||
describe('it returns a promise which resolves a container with expected url', function () {
|
||||
test.data.forEach(function (data) {
|
||||
it(data.url, function (done) {
|
||||
container.user.req = { url: data.url };
|
||||
container.options.proxyReqPathResolver = test.resolverFn;
|
||||
var r = resolveProxyReqPath(container);
|
||||
|
||||
assert(r instanceof Promise, 'Expect resolver to return a thennable');
|
||||
|
||||
r.then(function (container) {
|
||||
var response;
|
||||
try {
|
||||
response = container.proxy.reqBuilder.path;
|
||||
if (!response) {
|
||||
throw new Error('reqBuilder.url is undefined');
|
||||
}
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
expect(response).to.equal(data.parsed);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('testing example code in docs', function () {
|
||||
it('works as advertised', function (done) {
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
var proxyRouteFn = [{
|
||||
method: 'get',
|
||||
path: '/tent',
|
||||
fn: function (req, res) {
|
||||
res.send(req.url);
|
||||
}
|
||||
}];
|
||||
|
||||
var proxyServer = proxyTarget(12345, 100, proxyRouteFn);
|
||||
var app = express();
|
||||
app.use(proxy('localhost:12345', {
|
||||
proxyReqPathResolver: function (req) {
|
||||
var parts = req.url.split('?');
|
||||
var queryString = parts[1];
|
||||
var updatedPath = parts[0].replace(/test/, 'tent');
|
||||
return updatedPath + (queryString ? '?' + queryString : '');
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/test?a=1&b=2&c=3')
|
||||
.end(function (err, res) {
|
||||
assert.equal(res.text, '/tent?a=1&b=2&c=3');
|
||||
proxyServer.close();
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
40
node_modules/express-http-proxy/test/session.js
generated
vendored
Normal file
40
node_modules/express-http-proxy/test/session.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('preserveReqSession', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
app.use(proxy('httpbin.org'));
|
||||
});
|
||||
|
||||
it('preserveReqSession', function (done) {
|
||||
var app = express();
|
||||
app.use(function (req, res, next) {
|
||||
req.session = 'hola';
|
||||
next();
|
||||
});
|
||||
app.use(proxy('httpbin.org', {
|
||||
preserveReqSession: true,
|
||||
proxyReqOptDecorator: function (reqOpts) {
|
||||
assert(reqOpts.session, 'hola');
|
||||
return reqOpts;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/user-agent')
|
||||
.end(function (err) {
|
||||
if (err) { return done(err); }
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
31
node_modules/express-http-proxy/test/status.js
generated
vendored
Normal file
31
node_modules/express-http-proxy/test/status.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var mockEndpoint = require('../lib/mockHTTP.js');
|
||||
|
||||
describe('proxies status code', function () {
|
||||
var proxyServer = express();
|
||||
var port = 21239;
|
||||
var proxiedEndpoint = 'http://localhost:' + port;
|
||||
var server;
|
||||
|
||||
proxyServer.use(proxy(proxiedEndpoint));
|
||||
|
||||
beforeEach(function () {
|
||||
server = mockEndpoint.listen(21239);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
server.close();
|
||||
});
|
||||
|
||||
[304, 404, 200, 401, 500].forEach(function (status) {
|
||||
it('on ' + status, function (done) {
|
||||
request(proxyServer)
|
||||
.get('/status/' + status)
|
||||
.expect(status, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
121
node_modules/express-http-proxy/test/streaming.js
generated
vendored
Normal file
121
node_modules/express-http-proxy/test/streaming.js
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var http = require('http');
|
||||
var startProxyTarget = require('./support/proxyTarget');
|
||||
var proxy = require('../');
|
||||
|
||||
function chunkingProxyServer() {
|
||||
var proxyRouteFn = [{
|
||||
method: 'get',
|
||||
path: '/stream',
|
||||
fn: function (req, res) {
|
||||
res.write('0');
|
||||
setTimeout(function () { res.write('1'); }, 100);
|
||||
setTimeout(function () { res.write('2'); }, 200);
|
||||
setTimeout(function () { res.write('3'); }, 300);
|
||||
setTimeout(function () { res.end(); }, 500);
|
||||
}
|
||||
}];
|
||||
|
||||
return startProxyTarget(8309, 1000, proxyRouteFn);
|
||||
}
|
||||
|
||||
function simulateUserRequest() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var req = http.request({ hostname: 'localhost', port: 8308, path: '/stream' }, function (res) {
|
||||
var chunks = [];
|
||||
res.on('data', function (chunk) { chunks.push(chunk.toString()); });
|
||||
res.on('end', function () { resolve(chunks); });
|
||||
});
|
||||
|
||||
req.on('error', function (e) {
|
||||
reject('problem with request:', e.message);
|
||||
});
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function startLocalServer(proxyOptions) {
|
||||
var app = express();
|
||||
app.get('/stream', proxy('http://localhost:8309', proxyOptions));
|
||||
return app.listen(8308);
|
||||
}
|
||||
|
||||
describe('streams / piped requests', function () {
|
||||
this.timeout(3000);
|
||||
|
||||
var server;
|
||||
var targetServer;
|
||||
|
||||
beforeEach(function () {
|
||||
targetServer = chunkingProxyServer();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
server.close();
|
||||
targetServer.close();
|
||||
});
|
||||
|
||||
describe('when streaming options are truthy', function () {
|
||||
var TEST_CASES = [{
|
||||
name: 'vanilla, no options defined',
|
||||
options: {}
|
||||
}, {
|
||||
name: 'proxyReqOptDecorator is defined',
|
||||
options: { proxyReqOptDecorator: function (reqBuilder) { return reqBuilder; } }
|
||||
}, {
|
||||
//// Keep around this case for manually testing that this for sure fails for a few cycles. 2018 NMK
|
||||
//name: 'proxyReqOptDecorator never returns',
|
||||
//options: { proxyReqOptDecorator: function () { return new Promise(function () {}); } }
|
||||
//}, {
|
||||
|
||||
name: 'proxyReqOptDecorator is a Promise',
|
||||
options: { proxyReqOptDecorator: function (reqBuilder) { return Promise.resolve(reqBuilder); } }
|
||||
}];
|
||||
|
||||
TEST_CASES.forEach(function (testCase) {
|
||||
describe(testCase.name, function () {
|
||||
it('chunks are received without any buffering, e.g. before request end', function (done) {
|
||||
server = startLocalServer(testCase.options);
|
||||
simulateUserRequest()
|
||||
.then(function (res) {
|
||||
// Assume that if I'm getting a chunked response, it will be an array of length > 1;
|
||||
|
||||
assert(res instanceof Array, 'res is an Array');
|
||||
assert.equal(res.length, 4);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when streaming options are falsey', function () {
|
||||
var TEST_CASES = [{
|
||||
name: 'skipToNextHandler is defined',
|
||||
options: { skipToNextHandlerFilter: function () { return false; } }
|
||||
}];
|
||||
|
||||
TEST_CASES.forEach(function (testCase) {
|
||||
describe(testCase.name, function () {
|
||||
it('response arrives in one large chunk', function (done) {
|
||||
server = startLocalServer(testCase.options);
|
||||
|
||||
simulateUserRequest()
|
||||
.then(function (res) {
|
||||
// Assume that if I'm getting a un-chunked response, it will be an array of length = 1;
|
||||
|
||||
assert(res instanceof Array);
|
||||
assert.equal(res.length, 1);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
55
node_modules/express-http-proxy/test/support/proxyTarget.js
generated
vendored
Normal file
55
node_modules/express-http-proxy/test/support/proxyTarget.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var cookieParser = require('cookie-parser');
|
||||
|
||||
function proxyTarget(port, timeout, handlers) {
|
||||
var target = express();
|
||||
|
||||
timeout = timeout || 100;
|
||||
|
||||
// parse application/x-www-form-urlencoded
|
||||
target.use(bodyParser.urlencoded({ extended: false }));
|
||||
|
||||
// parse application/json
|
||||
target.use(bodyParser.json());
|
||||
target.use(cookieParser());
|
||||
|
||||
target.use(function(req, res, next) {
|
||||
setTimeout(function() {
|
||||
next();
|
||||
},timeout);
|
||||
});
|
||||
|
||||
if (handlers) {
|
||||
handlers.forEach(function(handler) {
|
||||
target[handler.method](handler.path, handler.fn);
|
||||
});
|
||||
}
|
||||
|
||||
target.get('/get', function (req, res) {
|
||||
res.send('OK');
|
||||
});
|
||||
|
||||
target.post('/post', function(req, res) {
|
||||
req.pipe(res);
|
||||
});
|
||||
|
||||
target.use('/headers', function(req, res) {
|
||||
res.json({ headers: req.headers });
|
||||
});
|
||||
|
||||
target.use(function(err, req, res, next) {
|
||||
res.send(err);
|
||||
next();
|
||||
});
|
||||
|
||||
target.use(function(req, res) {
|
||||
res.sendStatus(404);
|
||||
});
|
||||
|
||||
return target.listen(port);
|
||||
}
|
||||
|
||||
module.exports = proxyTarget;
|
||||
63
node_modules/express-http-proxy/test/timeout.js
generated
vendored
Normal file
63
node_modules/express-http-proxy/test/timeout.js
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var proxyTarget = require('./support/proxyTarget');
|
||||
|
||||
describe('honors timeout option', function () {
|
||||
|
||||
var other;
|
||||
var http;
|
||||
|
||||
beforeEach(function () {
|
||||
http = express();
|
||||
other = proxyTarget(56001, 1000, [{
|
||||
method: 'get',
|
||||
path: '/',
|
||||
fn: function (req, res) { res.sendStatus(200); }
|
||||
}]);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
other.close();
|
||||
});
|
||||
|
||||
function assertSuccess(server, done) {
|
||||
request(http)
|
||||
.get('/')
|
||||
.expect(200)
|
||||
.end(done);
|
||||
}
|
||||
|
||||
function assertConnectionTimeout(server, done) {
|
||||
request(http)
|
||||
.get('/')
|
||||
.expect(504)
|
||||
.expect('X-Timeout-Reason', 'express-http-proxy reset the request.')
|
||||
.end(done);
|
||||
}
|
||||
|
||||
describe('when timeout option is set lower than server response time', function () {
|
||||
it('should fail with CONNECTION TIMEOUT', function (done) {
|
||||
|
||||
http.use(proxy('http://localhost:56001', {
|
||||
timeout: 100,
|
||||
}));
|
||||
|
||||
assertConnectionTimeout(http, done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when timeout option is set higher than server response time', function () {
|
||||
it('should succeed', function (done) {
|
||||
|
||||
http.use(proxy('http://localhost:56001', {
|
||||
timeout: 1200,
|
||||
}));
|
||||
|
||||
assertSuccess(http, done);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
41
node_modules/express-http-proxy/test/traceDebugging.js
generated
vendored
Normal file
41
node_modules/express-http-proxy/test/traceDebugging.js
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
var debug = require('debug');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
var proxyTarget = require('../test/support/proxyTarget');
|
||||
|
||||
/* This test is specifically written because of critical errors thrown while debug logging. */
|
||||
describe.only('trace debugging does not cause the application to fail', function () {
|
||||
var proxyServer;
|
||||
|
||||
beforeEach(function () { debug.enable('express-http-proxy'); proxyServer = proxyTarget(3000); });
|
||||
afterEach(function () { debug.disable('express-http-proxy'); proxyServer.close(); });
|
||||
|
||||
it('happy path', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('localhost:3000'));
|
||||
request(app)
|
||||
.get('/get')
|
||||
.expect(200)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('when agent is a deeply nested object', function (done) {
|
||||
var app = express();
|
||||
var HttpAgent = require('http').Agent;
|
||||
var httpAgent = new HttpAgent({ keepAlive: true, keepAliveMsecs: 60e3 });
|
||||
app.use(proxy('localhost:3000', {
|
||||
proxyReqOptDecorator: function (proxyReqOpts) {
|
||||
proxyReqOpts.agent = httpAgent;
|
||||
return proxyReqOpts;
|
||||
}
|
||||
}));
|
||||
request(app)
|
||||
.get('/get')
|
||||
.expect(200)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
37
node_modules/express-http-proxy/test/urlParsing.js
generated
vendored
Normal file
37
node_modules/express-http-proxy/test/urlParsing.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('url parsing', function () {
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
it('can parse a url with a port', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('http://httpbin.org:80'));
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function (err) {
|
||||
if (err) { return done(err); }
|
||||
assert(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('does not throw `Uncaught RangeError` if you have both a port and a trailing slash', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('http://httpbin.org:80/'));
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function (err) {
|
||||
if (err) { return done(err); }
|
||||
assert(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
213
node_modules/express-http-proxy/test/userResDecorator.js
generated
vendored
Normal file
213
node_modules/express-http-proxy/test/userResDecorator.js
generated
vendored
Normal file
@@ -0,0 +1,213 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('userResDecorator', function () {
|
||||
|
||||
describe('when handling a 304', function () {
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
var slowTarget;
|
||||
var serverReference;
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
slowTarget = express();
|
||||
slowTarget.use(function (req, res) { res.sendStatus(304); });
|
||||
serverReference = slowTarget.listen(12345);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
serverReference.close();
|
||||
});
|
||||
|
||||
it('skips any handling', function (done) {
|
||||
app.use('/proxy', proxy('http://127.0.0.1:12345', {
|
||||
userResDecorator: function (/*res*/) {
|
||||
throw new Error('expected to never get here because this step should be skipped for 304');
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/proxy')
|
||||
.expect(304)
|
||||
.end(done);
|
||||
});
|
||||
});
|
||||
|
||||
it('has access to original response', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('httpbin.org', {
|
||||
userResDecorator: function (proxyRes, proxyResData) {
|
||||
assert(proxyRes.connection);
|
||||
assert(proxyRes.socket);
|
||||
assert(proxyRes.headers);
|
||||
assert(proxyRes.headers['content-type']);
|
||||
return proxyResData;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app).get('/').end(done);
|
||||
});
|
||||
|
||||
it('works with promises', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('httpbin.org', {
|
||||
userResDecorator: function (proxyRes, proxyResData) {
|
||||
return new Promise(function (resolve) {
|
||||
proxyResData.funkyMessage = 'oi io oo ii';
|
||||
setTimeout(function () {
|
||||
resolve(proxyResData);
|
||||
}, 200);
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/ip')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
|
||||
assert(res.body.funkyMessage = 'oi io oo ii');
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('can modify the response data', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('httpbin.org', {
|
||||
userResDecorator: function (proxyRes, proxyResData) {
|
||||
proxyResData = JSON.parse(proxyResData.toString('utf8'));
|
||||
proxyResData.intercepted = true;
|
||||
return JSON.stringify(proxyResData);
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/ip')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
|
||||
assert(res.body.intercepted);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('can modify the response headers, [deviant case, supported by pass-by-reference atm]', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('httpbin.org', {
|
||||
userResDecorator: function (rsp, data, req, res) {
|
||||
res.set('x-wombat-alliance', 'mammels');
|
||||
res.set('content-type', 'wiki/wiki');
|
||||
return data;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/ip')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert(res.headers['content-type'] === 'wiki/wiki');
|
||||
assert(res.headers['x-wombat-alliance'] === 'mammels');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can mutuate an html response', function (done) {
|
||||
var app = express();
|
||||
app.use(proxy('httpbin.org', {
|
||||
userResDecorator: function (rsp, data) {
|
||||
data = data.toString().replace('Oh', '<strong>Hey</strong>');
|
||||
assert(data !== '');
|
||||
return data;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/html')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert(res.text.indexOf('<strong>Hey</strong>') > -1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can change the location of a redirect', function (done) {
|
||||
|
||||
function redirectingServer(port, origin) {
|
||||
var app = express();
|
||||
app.get('/', function (req, res) {
|
||||
res.status(302);
|
||||
res.location(origin + '/proxied/redirect/url');
|
||||
res.send();
|
||||
});
|
||||
return app.listen(port);
|
||||
}
|
||||
|
||||
var redirectingServerPort = 8012;
|
||||
var redirectingServerOrigin = ['http://localhost', redirectingServerPort].join(':');
|
||||
|
||||
var server = redirectingServer(redirectingServerPort, redirectingServerOrigin);
|
||||
|
||||
var proxyApp = express();
|
||||
var preferredPort = 3000;
|
||||
|
||||
proxyApp.use(proxy(redirectingServerOrigin, {
|
||||
userResDecorator: function (rsp, data, req, res) {
|
||||
var proxyReturnedLocation = res._headers.location;
|
||||
res.location(proxyReturnedLocation.replace(redirectingServerPort, preferredPort));
|
||||
return data;
|
||||
}
|
||||
}));
|
||||
|
||||
request(proxyApp)
|
||||
.get('/')
|
||||
.expect(function (res) {
|
||||
res.headers.location.match(/localhost:3000/);
|
||||
})
|
||||
.end(function () {
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('test userResDecorator on html response from github', function () {
|
||||
|
||||
/*
|
||||
Github provided a unique situation where the encoding was different than
|
||||
utf-8 when we didn't explicitly ask for utf-8. This test helped sort out
|
||||
the issue, and even though its a little too on the nose for a specific
|
||||
case, it seems worth keeping around to ensure we don't regress on this
|
||||
issue.
|
||||
*/
|
||||
|
||||
it('is able to read and manipulate the response', function (done) {
|
||||
this.timeout(15000); // give it some extra time to get response
|
||||
var app = express();
|
||||
app.use(proxy('https://github.com/villadora/express-http-proxy', {
|
||||
userResDecorator: function (targetResponse, data) {
|
||||
data = data.toString().replace('DOCTYPE', 'WINNING');
|
||||
assert(data !== '');
|
||||
return data;
|
||||
}
|
||||
}));
|
||||
|
||||
request(app)
|
||||
.get('/html')
|
||||
.end(function (err, res) {
|
||||
if (err) { return done(err); }
|
||||
assert(res.text.indexOf('WINNING') > -1);
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
90
node_modules/express-http-proxy/test/verbs.js
generated
vendored
Normal file
90
node_modules/express-http-proxy/test/verbs.js
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var request = require('supertest');
|
||||
var proxy = require('../');
|
||||
|
||||
describe('http verbs', function () {
|
||||
this.timeout(10000);
|
||||
|
||||
var app;
|
||||
|
||||
beforeEach(function () {
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: false }));
|
||||
app.use(proxy('httpbin.org'));
|
||||
});
|
||||
|
||||
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 post by x-www-form-urlencoded', function (done) {
|
||||
request(app)
|
||||
.post('/post')
|
||||
.set('Content-Type', 'application/x-www-form-urlencoded')
|
||||
.send('mypost=hello')
|
||||
.end(function (err, res) {
|
||||
assert.equal(JSON.stringify(res.body.form), '{"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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user