Skip to content

Commit d01a870

Browse files
committed
Adds support for context arrays in #17
1 parent b48c950 commit d01a870

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed

Gruntfile.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function(grunt) {
1818
all: [
1919
'Gruntfile.js',
2020
'tasks/*.js',
21+
'lib/*.js',
2122
'<%= nodeunit.tests %>',
2223
],
2324
options: {
@@ -77,7 +78,11 @@ module.exports = function(grunt) {
7778
},
7879
{
7980
host: 'www.missingcontext.com',
80-
}
81+
},
82+
{
83+
context: ['/array1','/array2'],
84+
host: 'www.defaults.com'
85+
},
8186
],
8287
server2: {
8388
proxies: [

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ For the server task, add the configureProxies task before the connect task
116116
The available configuration options from a given proxy are generally the same as what is provided by the underlying [httpproxy](https://github.com/nodejitsu/node-http-proxy) library
117117
118118
#### options.context
119-
Type: `String`
119+
Type: `String` or `Array`
120120
121-
The context to match requests against. Matching requests will be proxied. Should start with /. Should not end with /
121+
The context(s) to match requests against. Matching requests will be proxied. Should start with /. Should not end with /
122+
Multiple contexts can be matched for the same proxy rule via an array such as:
123+
context: ['/api', 'otherapi']
122124
123125
#### options.host
124126
Type: `String`
@@ -236,4 +238,4 @@ grunt.registerTask('e2etest', function (target) {
236238
* 0.1.3 Bumped http-proxy dependency to 0.10.2
237239
* 0.1.4 Added proxy rewrite support (thanks to @slawrence)
238240
* 0.1.5 Default rejectUnauthorized to false to allow self-signed certificates over SSL
239-
* 0.1.6 Add xforward option
241+
* 0.1.6 Add xforward option, added support for context arrays, added debug logging

lib/utils.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var utils = module.exports;
33
var httpProxy = require('http-proxy');
4+
var _ = require('lodash');
45
var proxies = [];
56
var rewrite = function (req) {
67
return function (rule) {
@@ -54,11 +55,25 @@ utils.processRewrites = function (rewrites) {
5455
return rules;
5556
};
5657

58+
utils.matchContext = function(context, url) {
59+
var contexts = context;
60+
var matched = false;
61+
if (!_.isArray(contexts)) {
62+
contexts = [contexts];
63+
}
64+
contexts.forEach(function(testContext) {
65+
if (url.lastIndexOf(testContext, 0) === 0) {
66+
matched = true;
67+
}
68+
});
69+
return matched;
70+
};
71+
5772
utils.proxyRequest = function (req, res, next) {
5873
var proxied = false;
5974

6075
proxies.forEach(function(proxy) {
61-
if (!proxied && req && req.url.lastIndexOf(proxy.config.context, 0) === 0) {
76+
if (!proxied && req && utils.matchContext(proxy.config.context, req.url)) {
6277
if (proxy.config.rules.length) {
6378
proxy.config.rules.forEach(rewrite(req));
6479
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "grunt-connect-proxy",
33
"description": "Provides a http proxy as middleware for grunt connect.",
4-
"version": "0.1.5",
4+
"version": "0.1.6",
55
"homepage": "https://github.com/drewzboto/grunt-connect-proxy",
66
"author": {
77
"name": "Drewz",
@@ -28,7 +28,8 @@
2828
"test": "grunt test"
2929
},
3030
"dependencies": {
31-
"http-proxy": "~0.10.2"
31+
"http-proxy": "~0.10.3",
32+
"lodash": "~0.9.0"
3233
},
3334
"devDependencies": {
3435
"grunt-contrib-jshint": "~0.1.1",

tasks/connect_proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
'use strict';
1010
var utils = require('../lib/utils');
11+
var _ = require('lodash');
1112

1213
module.exports = function(grunt) {
1314
grunt.registerTask('configureProxies', 'Configure any specified connect proxies.', function(config) {
1415
// setup proxy
15-
var _ = grunt.util._;
1616
var httpProxy = require('http-proxy');
1717
var proxyOption;
1818
var proxyOptions = [];

test/connect_proxy_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exports.connect_proxy = {
3232
test.expect(9);
3333
var proxies = utils.proxies();
3434

35-
test.equal(proxies.length, 4, 'should return four valid proxies');
35+
test.equal(proxies.length, 5, 'should return five valid proxies');
3636
test.notEqual(proxies[0].server, null, 'server should be configured');
3737
test.equal(proxies[0].config.context, '/defaults', 'should have context set from config');
3838
test.equal(proxies[0].config.host, 'www.defaults.com', 'should have host set from config');
@@ -48,7 +48,7 @@ exports.connect_proxy = {
4848
test.expect(12);
4949
var proxies = utils.proxies();
5050

51-
test.equal(proxies.length, 4, 'should return four valid proxies');
51+
test.equal(proxies.length, 5, 'should return five valid proxies');
5252
test.notEqual(proxies[1].server, null, 'server should be configured');
5353
test.equal(proxies[1].config.context, '/full', 'should have context set from config');
5454
test.equal(proxies[1].config.host, 'www.full.com', 'should have host set from config');
@@ -77,7 +77,7 @@ exports.connect_proxy = {
7777
test.expect(5);
7878
var proxies = utils.proxies();
7979

80-
test.equal(proxies.length, 4, 'should not add the 2 invalid proxies');
80+
test.equal(proxies.length, 5, 'should not add the 2 invalid proxies');
8181
test.notEqual(proxies[0].config.context, '/missinghost', 'should not have context set from config with missing host');
8282
test.notEqual(proxies[0].config.host, 'www.missingcontext.com', 'should not have host set from config with missing context');
8383
test.notEqual(proxies[1].config.context, '/missinghost', 'should not have context set from config with missing host');
@@ -88,7 +88,7 @@ exports.connect_proxy = {
8888
invalid_rewrite: function(test) {
8989
test.expect(3);
9090
var proxies = utils.proxies();
91-
test.equal(proxies.length, 4, 'proxies should still be valid');
91+
test.equal(proxies.length, 5, 'proxies should still be valid');
9292
test.equal(proxies[3].config.rules.length, 1, 'rules array should have one valid item');
9393
test.deepEqual(proxies[3].config.rules[0], { from: new RegExp('^/in'), to: '/thisis'}, 'rules object should be converted to regex');
9494

test/server2_proxy_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ exports.server2_proxy_test = {
99
test.expect(10);
1010
var proxies = utils.proxies();
1111

12-
test.equal(proxies.length, 5, 'should return one valid proxy');
12+
test.equal(proxies.length, 6, 'should return six valid proxies');
1313
test.notEqual(proxies[0].server, null, 'server should be configured');
1414
test.equal(proxies[0].config.context, '/defaults', 'should have context set from config');
1515
test.equal(proxies[0].config.host, 'www.defaults.com', 'should have host set from config');
16-
test.equal(proxies[4].config.context, '/', 'should have context set from config');
17-
test.equal(proxies[4].config.host, 'www.server2.com', 'should have host set from config');
16+
test.equal(proxies[5].config.context, '/', 'should have context set from config');
17+
test.equal(proxies[5].config.host, 'www.server2.com', 'should have host set from config');
1818
test.equal(proxies[0].config.port, 80, 'should have default port 80');
1919
test.equal(proxies[0].config.https, false, 'should have default http');
2020
test.equal(proxies[0].config.changeOrigin, false, 'should have default change origin');

0 commit comments

Comments
 (0)