add setHeaders option

This commit is contained in:
Nikolay Derkach
2015-08-20 20:05:50 -07:00
parent 8429bb3c58
commit c5a3877e6c
3 changed files with 27 additions and 1 deletions

View File

@@ -100,6 +100,8 @@ The following options are recognized by both methods:
Example: `['Origin', 'X-Requested-With']`.
* array of lowercase strings `removeHeaders` - Exclude certain headers from being included in the request.
Example: `["cookie"]`
* dictionary of lowercase strings `setHeaders` - Set headers for the request (overwrites existing ones).
Example: `{"x-powered-by": "CORS Anywhere"}`
`createServer` recognizes the following option as well:

View File

@@ -200,7 +200,8 @@ var getHandler = exports.getHandler = function(options, proxy) {
originBlacklist: [], // Requests from these origins will be blocked.
originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked.
requireHeader: null, // Require a header to be set?
removeHeaders: [] // Strip these request headers
removeHeaders: [], // Strip these request headers.
setHeaders: [] // Set these request headers.
};
if (options) {
Object.keys(corsAnywhere).forEach(function(option) {
@@ -293,6 +294,9 @@ var getHandler = exports.getHandler = function(options, proxy) {
delete req.headers[header];
});
Object.keys(corsAnywhere.setHeaders).forEach(function(header) {
req.headers[header] = corsAnywhere.setHeaders[header];
});
req.corsAnywhereRequestState = {
location: location,

View File

@@ -478,6 +478,26 @@ describe('removeHeaders', function() {
});
});
describe('setHeaders', function() {
before(function() {
cors_anywhere = createServer({
setHeaders: {'x-powered-by': 'CORS Anywhere'},
});
cors_anywhere_port = cors_anywhere.listen(0).address().port;
});
after(stopServer);
it('GET /example.com', function(done) {
request(cors_anywhere)
.get('/example.com/echoheaders')
.expect('Access-Control-Allow-Origin', '*')
.expectJSON({
host: 'example.com',
'x-powered-by': 'CORS Anywhere'
}, done);
});
});
describe('httpProxyOptions.xfwd=false', function() {
before(function() {
cors_anywhere = createServer({