Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #7 from NREL/url-rewrites
Browse files Browse the repository at this point in the history
Configurable system-wide URL redirects
  • Loading branch information
GUI committed May 19, 2015
2 parents 18489d5 + a176409 commit e62da50
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
4 changes: 4 additions & 0 deletions templates/etc/nginx/frontend_hosts.conf.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ server {
rewrite .* /api-umbrella-banned last;
}

{{~#each rewrites}}
rewrite {{{.}}};
{{~/each}}

{{~#if enable_web_backend}}
# Route the dynamic portions of the site to the Rails web app.
location ~* {{../../router.web_backend_regex}} {
Expand Down
7 changes: 7 additions & 0 deletions test/config/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,16 @@ website_backends:
hosts:
- hostname: default.foo
default: true
rewrites:
- "^/admin/rewrite_me$ https://example.com/ permanent"
- "^/hello/rewrite/(debian|el|ubuntu)/([\\d\\.]+)/(file[_-]([\\d\\.]+)-\\d+).*((\\.|_)(amd64|x86_64).(deb|rpm)) https://example.com/downloads/v$4/$3.$1$2$5? redirect"
- "^/hello/rewrite https://example.com/something/ permanent"
- hostname: withweb.foo
enable_web_backend: true
- hostname: withoutweb.foo
- hostname: with-apis-and-website.foo
rewrites:
- "^/example/rewrite_me$ https://example.com/ permanent"
dns_resolver:
minimum_ttl: 1
reload_buffer_time: 1
Expand Down
95 changes: 95 additions & 0 deletions test/integration/url_rewrites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

require('../test_helper');

var _ = require('lodash'),
request = require('request');

describe('url rewrites', function() {
beforeEach(function() {
this.options = {
followRedirect: false,
strictSSL: false,
};
});

it('performs simple nginx style rewrites on a per host basis', function(done) {
var options = _.merge({}, this.options, {
headers: {
'Host': 'default.foo',
},
});
request.get('http://localhost:9080/hello/rewrite?foo=bar', options, function(error, response) {
should.not.exist(error);
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/something/?foo=bar');
done();
});
});

it('performs more complex nginx style rewrites on a per host basis', function(done) {
var options = _.merge({}, this.options, {
headers: {
'Host': 'default.foo',
},
});
request.get('http://localhost:9080/hello/rewrite/el/7/file-0.6.0-1.el7.x86_64.rpm?foo=bar', options, function(error, response) {
should.not.exist(error);
response.statusCode.should.eql(302);
response.headers.location.should.eql('https://example.com/downloads/v0.6.0/file-0.6.0-1.el7.x86_64.rpm');
done();
});
});

it('rewrites take precendence over potential API matches', function(done) {
var options = _.merge({}, this.options, {
headers: {
'Host': 'with-apis-and-website.foo',
},
});
request.get('http://localhost:9080/example/rewrite_me', options, function(error, response) {
should.not.exist(error);
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/');

request.get('http://localhost:9080/example/rewrite_me_just_kidding', options, function(error, response, body) {
should.not.exist(error);
response.statusCode.should.eql(403);
body.should.contain('API_KEY_MISSING');
done();
});
});
});

it('rewrites take precendence over potential web admin matches', function(done) {
var options = _.merge({}, this.options, {
headers: {
'Host': 'default.foo',
},
});
request.get('https://localhost:9081/admin/rewrite_me', options, function(error, response) {
should.not.exist(error);
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/');

request.get('https://localhost:9081/admin/rewrite_me_just_kidding', options, function(error, response) {
should.not.exist(error);
response.statusCode.should.eql(404);
done();
});
});
});

it('does not perform rewrites on other hosts', function(done) {
var options = _.merge({}, this.options, {
headers: {
'Host': 'withweb.foo',
},
});
request.get('http://localhost:9080/hello/rewrite?foo=bar', options, function(error, response) {
should.not.exist(error);
response.statusCode.should.eql(404);
done();
});
});
});

0 comments on commit e62da50

Please sign in to comment.