Permalink
Browse files

Added PreferredHost option to resolve only, without redirecting.

  • Loading branch information...
1 parent 1f55995 commit c8bc7f0433ef6091ba1ffa31840df40d049de521 Chris Osborn committed Feb 29, 2012
Showing with 15 additions and 2 deletions.
  1. +4 −2 lib/stages/PreferredHost.js
  2. +11 −0 testing/tests/stages/PreferredHostTest.js
@@ -38,12 +38,14 @@ var PreferredHost = capsela.Stage.extend({
*
* @param host the preferred hostname:port
* @param protocol if unspecified, will use the protocol of the request
+ * @param resolveOnly if true, stage only resolves, does not redirect
*/
- init: function(host, protocol) {
+ init: function(host, protocol, resolveOnly) {
this.protocol = protocol;
this.host = host.toLowerCase();
this.baseUrl = (protocol || 'http') + '://' + this.host;
+ this.resolveOnly = resolveOnly;
},
///////////////////////////////////////////////////////////////////////////////
@@ -58,7 +60,7 @@ var PreferredHost = capsela.Stage.extend({
var host = request.getHeader('host');
var protocol = this.protocol || request.protocol;
- if (host.toLowerCase() != this.host) {
+ if (!this.resolveOnly && host.toLowerCase() != this.host) {
return new capsela.Redirect(protocol + '://' + this.host + request.url);
}
@@ -101,6 +101,17 @@ module.exports["basics"] = {
test.done();
},
+ "test no redirect": function(test) {
+
+ var stage = new PreferredHost('www.example.com', 'http', true);
+ var request = new capsela.Request('GET', '/', {host: 'example.com'});
+
+ var response = stage.service(request);
+ test.ok(!(response instanceof capsela.Redirect));
+
+ test.done();
+ },
+
"test favor http": function(test) {
var stage = new PreferredHost('www.example.com', 'http');

0 comments on commit c8bc7f0

Please sign in to comment.