UNPKG

express-htaccess-middleware

Version:

An express middleware that interprets .htaccess rewrite rules.

303 lines (237 loc) 7.58 kB
var RewriteMiddleware = require('../lib/middleware'); var express = require('./helpers/express'); var expect = require('chai').expect; var path = require('path'); var supertest = require('supertest'); var async = require('async'); var app = null; describe('50-rewritecond', function() { before(function (done) { var file = path.resolve(__dirname, 'htaccess_files', '50-rewritecond.htaccess'); express(0, {file: file}, function(err, server, expressInstance) { app = server; expressInstance.get('/dest7.html', function (req, res) { res.send('content of dest7.html'); }); done(); }); }); after(function (done) { app.close(); done(); }); it('HTTP_USER_AGENT', function (done) { this.request = supertest(app) .get('/source1.html') .set('User-Agent', 'Blacklisted-agent1') .end(function (err, res) { expect(res.statusCode).to.equal(307); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/dest1.html'); done(); }); }); it('HTTP_USER_AGENT - OR,NC flags', function (done) { async.series([ function(next) { this.request = supertest(app) .get('/source2.html') .set('User-Agent', 'whitelisted-agent1') .end(function (err, res) { expect(res.statusCode).to.not.equal(403); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/source2.html') .set('User-Agent', 'Blacklisted-agent1') .end(function (err, res) { expect(res.statusCode).to.equal(403); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/source2.html') .set('User-Agent', 'Blacklisted-AGENT2') .end(function (err, res) { expect(res.statusCode).to.equal(403); next(); }); }.bind(this), ], done); }); it('REQUEST_METHOD/HTTP_REFERER', function (done) { async.series([ function(next) { this.request = supertest(app) .get('/test.html') .set('User-Agent', 'whitelisted-agent1') .set('Referer', 'http://www.otherdomain.com/page.html') .end(function (err, res) { expect(res.statusCode).to.not.equal(410); next(); }); }.bind(this), function(next) { this.request = supertest(app) .post('/test.html') .set('User-Agent', 'whitelisted-agent1') .set('Referer', 'http://www.olddomain.com/page.html') .end(function (err, res) { expect(res.statusCode).to.not.equal(410); next(); }); }.bind(this), function(next) { this.request = supertest(app) .post('/test.html') .set('User-Agent', 'whitelisted-agent1') .set('Referer', 'http://www.otherdomain.com/page.html') .end(function (err, res) { expect(res.statusCode).to.equal(410); next(); }); }.bind(this), ], done); }); it('HTTP_HOST', function (done) { async.series([ function(next) { this.request = supertest(app) .get('/test.html') .set('User-Agent', 'whitelisted-agent1') .set('Host', 'www.somedomain.com') .end(function (err, res) { expect(res.statusCode).to.equal(404); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/test.html') .set('User-Agent', 'whitelisted-agent1') .set('Host', 'www.olddomain.com') .end(function (err, res) { expect(res.statusCode).to.equal(301); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('http://www.newdomain.com/test.html'); next(); }); }.bind(this), ], done); }); it('REQUEST_URI', function (done) { this.request = supertest(app) .get('/source2.html') .end(function (err, res) { expect(res.statusCode).to.equal(302); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/dest2.html'); done(); }); }); it('THE_REQUEST', function (done) { async.series([ function(next) { this.request = supertest(app) .get('/source3.html') .end(function (err, res) { expect(res.statusCode).to.equal(404); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/source3.html') .query('test=1') .end(function (err, res) { expect(res.statusCode).to.equal(302); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/dest3.html?test=1'); next(); }); }.bind(this), ], done); }); it('ENV', function (done) { async.series([ function(next) { this.request = supertest(app) .get('/source4.html') .end(function (err, res) { expect(res.statusCode).to.equal(302); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/dest4.html'); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/source5.html') .end(function (err, res) { expect(res.statusCode).to.equal(302); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/dest5.html'); next(); }); }.bind(this), ], done); }); it('QUERY_STRING', function (done) { async.series([ function(next) { this.request = supertest(app) .get('/page1') .query('var=val') .end(function (err, res) { expect(res.statusCode).to.equal(302); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/page2?var=val'); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/path') .query('var=val') .end(function (err, res) { expect(res.statusCode).to.equal(302); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/path/var/val'); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/source6.html') .query('var=val&a=1') .end(function (err, res) { expect(res.statusCode).to.equal(404); next(); }); }.bind(this), function(next) { this.request = supertest(app) .get('/source6.html') .query('var=val') .end(function (err, res) { expect(res.statusCode).to.equal(302); expect(res.header).to.have.property('location'); expect(res.header.location).to.equal('/dest6.html'); next(); }); }.bind(this), ], done); }); it('with quotes', function (done) { this.request = supertest(app) .get('/source7.html') .end(function (err, res) { expect(res.statusCode).to.equal(200); expect(res.text).to.equal('content of dest7.html'); done(); }); }); });