Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#51] Add proxy support for external requests.
- Loading branch information
Showing
6 changed files
with
146 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
|
||
var request = require('request'); | ||
|
||
/** | ||
* Cross-Origin Requests Middleware. | ||
* | ||
* Add support for cross-origin resource requests by proxying all of the requests | ||
* through this route. Each request includes an encoded path that is the original | ||
* URL. | ||
* | ||
* Options: | ||
* | ||
* - `options` {Object} is unused. | ||
*/ | ||
|
||
module.exports = function(options) { | ||
return function(req, res, next) { | ||
if (req.url.indexOf('/proxy/') >= 0) { | ||
var url = decodeURIComponent(req.url.replace('/proxy/', '')); | ||
var proxy = request(url); | ||
req.pipe(proxy); | ||
proxy.pipe(res); | ||
} | ||
else { | ||
next(); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script type="text/javascript"> | ||
// | ||
// Proxy | ||
// | ||
// Intercept XHR calls that would violate single-origin policy. | ||
// These requests will be proxied through the server. | ||
// | ||
(function() { | ||
var xhr = {}; | ||
xhr.open = XMLHttpRequest.prototype.open; | ||
|
||
XMLHttpRequest.prototype.open = function(method, url) { | ||
var parser = document.createElement('a'); | ||
parser.href = url; | ||
|
||
// proxy the cross-origin request | ||
if (!parser.hostname.match(window.location.hostname)) { | ||
url = '/proxy/' + encodeURIComponent(url); | ||
alert(url); | ||
} | ||
|
||
xhr.open.apply(this, arguments); | ||
}; | ||
})(window); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
|
||
var chdir = require('chdir'), | ||
gaze = require('gaze'), | ||
phonegap = require('../../lib'), | ||
request = require('supertest'); | ||
|
||
/*! | ||
* Specification: Proxy middleware. | ||
*/ | ||
|
||
describe('proxy middleware', function() { | ||
beforeEach(function() { | ||
spyOn(gaze, 'Gaze').andReturn({ on: function() {} }); | ||
}); | ||
|
||
describe('single-origin request', function() { | ||
it('should be served normally', function(done) { | ||
chdir('spec/fixture/app-with-cordova', function() { | ||
request(phonegap()) | ||
.get('/cordova.js') | ||
.end(function(e, res) { | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.text).toMatch('i am cordova'); | ||
this.app.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
// | ||
// better to mock the requests but this is awkward with request lib. | ||
// | ||
describe('cross-origin request', function() { | ||
it('should proxy http:', function() { | ||
chdir('spec/fixture/app-with-cordova', function() { | ||
request(phonegap()) | ||
.get('/proxy/http%3A%2F%2Fphonegap.com') | ||
.end(function(e, res) { | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.text).toMatch('PhoneGap'); | ||
this.app.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should proxy https:', function() { | ||
chdir('spec/fixture/app-with-cordova', function() { | ||
request(phonegap()) | ||
.get('/proxy/https%3A%2F%2Fajax.googleapis.com%2Fajax%2Fservices%2Fsearch%2Fweb%3Fv%3D1.0%26q%3DAdobe%2520PhoneGap') | ||
.end(function(e, res) { | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.text).toMatch('GsearchResultClass'); | ||
this.app.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |