From 9134ffee48f8ea1576d9a901257a49ce32edb5b2 Mon Sep 17 00:00:00 2001 From: Fauntleroy Date: Tue, 25 Feb 2014 16:46:20 -0800 Subject: [PATCH 1/3] check redirect freshness on request --- lib/redirect.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/redirect.js b/lib/redirect.js index 50286ff..a58bacc 100644 --- a/lib/redirect.js +++ b/lib/redirect.js @@ -13,18 +13,24 @@ var Redirect = function( redirect_data, options ){ var server = options.server; var router = server.router; var status = redirect_data.permanent ? PERMANENT_STATUS : TEMPORARY_STATUS; - var expired = ( moment() > moment(redirect_data.end) ); - var premature = ( moment() < moment(redirect_data.start) ); + var start = redirect_data.start; + var end = redirect_data.end; + var from = redirect_data.from; + var to = redirect_data.to; + var expired = ( moment() > moment( end ) ); // don't create redirect route at all if expired + // don't check prematurity yet since it could become valid later if( !expired ){ - var route = this.route = path.normalize( redirect_data.from ).replace( /\\/g, '/' ); + var route = this.route = path.normalize( from ).replace( /\\/g, '/' ); router.get( route, function( req, res, next ){ + var expired = ( moment() > moment( end ) ); + var premature = ( moment() < moment( start ) ); // if redirect is expired or premature skip it if( !expired && !premature ){ - res.redirect( status, redirect_data.to ); + res.redirect( status, to ); } else { next(); From 15afb0c2cfd7bda5aef19ce4eb8c68227c8bcefd Mon Sep 17 00:00:00 2001 From: Fauntleroy Date: Tue, 25 Feb 2014 16:59:43 -0800 Subject: [PATCH 2/3] don't mock http requests before every test once will surely be enough --- test/solidus.js | 56 ++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/test/solidus.js b/test/solidus.js index a55b6af..1babdac 100644 --- a/test/solidus.js +++ b/test/solidus.js @@ -24,12 +24,7 @@ describe( 'Solidus', function(){ var solidus_server; - beforeEach( function( done ){ - process.chdir( site1_path ); - solidus_server = solidus.start({ - log_level: 0, - port: 9009 - }); + before( function( done ){ // mock http endpoints for resources nock('https://solid.us').get('/basic/1').reply( 200, { test: true } ); nock('https://solid.us').get('/basic/2').reply( 200, { test: true } ); @@ -47,30 +42,33 @@ describe( 'Solidus', function(){ nock('https://solid.us').get('/resource/options/dynamic/query?test=').reply( 200, { test: false } ); nock('https://solid.us').get('/resource/options/double/dynamic/query?test2=&test=').reply( 200, { test: false } ); - async.parallel( - [ - // compressed resources - function( callback ){ - zlib.gzip( '{"test":true}', function( _, result ){ - nock('https://solid.us').get('/compressed/gzip').reply( 200, result, { 'Content-Encoding': 'gzip' } ); - callback(); - }); - }, - function( callback ){ - zlib.deflate( '{"test":true}', function( _, result ){ - nock('https://solid.us').get('/compressed/deflate').reply( 200, result, { 'Content-Encoding': 'deflate' } ); - callback(); - }); - }, - // hack that will work until .start callback is complete - function( callback ){ - solidus_server.on( 'ready', callback ); - } - ], - function(){ - done(); + async.parallel([ + // compressed resources + function( callback ){ + zlib.gzip( '{"test":true}', function( _, result ){ + nock('https://solid.us').get('/compressed/gzip').reply( 200, result, { 'Content-Encoding': 'gzip' } ); + callback(); + }); + }, + function( callback ){ + zlib.deflate( '{"test":true}', function( _, result ){ + nock('https://solid.us').get('/compressed/deflate').reply( 200, result, { 'Content-Encoding': 'deflate' } ); + callback(); + }); } - ); + ], + function(){ + done(); + }); + }); + + beforeEach( function( done ){ + process.chdir( site1_path ); + solidus_server = solidus.start({ + log_level: 0, + port: 9009 + }); + solidus_server.on( 'ready', done ); }); afterEach( function(){ From 72151a9418411069bd7d0adee123f66bbdf73da0 Mon Sep 17 00:00:00 2001 From: Fauntleroy Date: Wed, 26 Feb 2014 17:39:13 -0800 Subject: [PATCH 3/3] add tests for temporary redirects --- test/solidus.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/solidus.js b/test/solidus.js index 1babdac..119c94c 100644 --- a/test/solidus.js +++ b/test/solidus.js @@ -5,6 +5,7 @@ var path = require('path'); var assert = require('assert'); var async = require('async'); var fs = require('fs'); +var moment = require('moment'); var request = require('supertest'); var nock = require('nock'); var zlib = require('zlib'); @@ -62,8 +63,32 @@ describe( 'Solidus', function(){ }); }); + var original_redirects = []; + beforeEach( function( done ){ process.chdir( site1_path ); + // Generate time-based redirects + // These are used to ensure that temporary redirects are properly checked + original_redirects = fs.readFileSync( 'redirects.json', DEFAULT_ENCODING ); + var original_redirects_arr = JSON.parse( original_redirects ); + var redirect_date_format = 'YYYY-MM-DD HH:mm:ss'; + var temporal_redirects = [{ + start: moment().add( 's', 5 ).format( redirect_date_format ), + from: '/future-redirect', + to: '/' + }, { + start: moment().subtract( 's', 5 ).format( redirect_date_format ), + end: moment().add( 's', 5 ).format( redirect_date_format ), + from: '/current-redirect', + to: '/' + }, { + start: moment().subtract( 's', 10 ).format( redirect_date_format ), + end: moment().subtract( 's', 5 ).format( redirect_date_format ), + from: '/past-redirect', + to: '/' + }]; + var combined_redirects = JSON.stringify( original_redirects_arr.concat( temporal_redirects ) ); + fs.writeFileSync( 'redirects.json', combined_redirects, DEFAULT_ENCODING ); solidus_server = solidus.start({ log_level: 0, port: 9009 @@ -73,6 +98,7 @@ describe( 'Solidus', function(){ afterEach( function(){ solidus_server.stop(); + fs.writeFileSync( 'redirects.json', original_redirects, DEFAULT_ENCODING ); process.chdir( original_path ); }); @@ -288,6 +314,15 @@ describe( 'Solidus', function(){ }, function( callback ){ s_request.get('/redirect5').expect( 301, callback ); + }, + function( callback ){ + s_request.get('/past-redirect').expect( 404, callback ); + }, + function( callback ){ + s_request.get('/current-redirect').expect( 302, callback ); + }, + function( callback ){ + s_request.get('/future-redirect').expect( 404, callback ); } ], function( err, results ){ if( err ) throw err;