Skip to content

Commit

Permalink
100 % coverage and bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
mickelindahl committed Oct 13, 2016
1 parent de602a9 commit f2b97f9
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 132 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ node_modules

# Ignore Idea stuff
.idea

# No coverage reports
coverage.html
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

'use strict';

const debug = require('debug')('hapi-redirect:index')

exports.register = function ( server, options, next ) {

if (typeof options =='object') options=[options];
if (options.constructor !== Array ) options=[options];

let lookup={};
options.forEach((val)=>{
Expand All @@ -19,14 +21,15 @@ exports.register = function ( server, options, next ) {

}


})

// onPreResponse intercepts ALL errors
server.ext( 'onPreResponse', ( request, reply ) => {

const response = request.response;

debug(lookup)

if ( response.isBoom ) {
let statusCode = response.output.payload.statusCode;

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Library for redirecting unauthorized request to another endpoint",
"main": "index.js",
"scripts": {
"test": "node_modules/.bin/lab -m 10000 -r lcov | ./node_modules/.bin/coveralls"
"test": "node_modules/.bin/lab -m 10000 -r lcov | ./node_modules/.bin/coveralls",
"test-cov-html": "lab -r html -o coverage.html"
},
"repository": {
"type": "git",
Expand All @@ -24,6 +25,7 @@
},
"homepage": "https://github.com/mickelindahl/hapi_redirect#readme",
"devDependencies": {
"bluebird": "^3.4.6",
"code": "^4.0.0",
"coveralls": "^2.11.14",
"debug": "^2.2.0",
Expand Down
69 changes: 0 additions & 69 deletions server.js

This file was deleted.

178 changes: 120 additions & 58 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,101 +7,163 @@

const Lab = require( 'lab' );
const code = require( 'code' );
const debug = require('debug')('hapi_redirect:test');
const server = require('../server.js');

//debug(server)
const debug = require( 'debug' )( 'hapi_redirect:test' );
const start_server = require( '../test_server.js' );

let lab = exports.lab = Lab.script();

lab.experiment( 'redirect', ()=> {

debug('start server');

//start_server( ()=>{} )

lab.test( 'redirect at 401 (unauthorized)', ( done )=> {

lab.experiment( 'redirect', ()=> {
start_server( {
redirect: {
status_code: "401",
redirect: "/login"
}
} ).then( ( server )=> {
let options = {
method: "GET",
url: "/private",
//credentials: {} // To bypass auth strategy
};

lab.before( {}, ( done )=> {
server.inject( options, ( response )=> {

var iv = setInterval(function () {
if (server.app.readyForTest == true) {
clearInterval(iv);
code.expect( response.statusCode ).to.equal( 302 );
server.stop()
done();
}
}, 50);

} );
} );
} );

lab.test( 'redirect at 401 (unauthorized)', ( done )=> {

let options = {
method: "GET",
url: "/private",
//credentials: {} // To bypass auth strategy
};
lab.test( 'redirect at 200 (authorized)', ( done )=> {

debug('before inject');
start_server( {
redirect: {
status_code: "401",
redirect: "/login"
}
} ).then( ( server )=> {

server.inject( options, ( response )=> {
let options = {
method: "GET",
url: "/private",
credentials: {} // To bypass auth strategy
};

debug(response)
server.inject( options, ( response )=> {

code.expect( response.statusCode ).to.equal( 302 );
done();
code.expect( response.statusCode ).to.equal( 200 );
server.stop()
done();

} );
} );
} );

} )

lab.test( 'redirect at 200 (authorized)', ( done )=> {

let options = {
method: "GET",
url: "/private",
credentials: {} // To bypass auth strategy
};
lab.test( 'redirect at 404 with host defined (unauthorized)', ( done )=> {

debug('before inject')
start_server( {
host: 'localhost',
redirect: {
status_code: "401",
redirect: "/login",
host:"http://0.0.0.0:300"
}
} ).then( ( server )=> {

server.inject( options, ( response )=> {
let options = {
method: "GET",
url: "/private",
// credentials: {} // To bypass auth strategy
};

debug(response)
server.inject( options, ( response )=> {

code.expect( response.statusCode ).to.equal( 200 );
done();
code.expect( response.statusCode ).to.equal( 302 );
server.stop()
done();

} );
} );

} )
} );

lab.test( 'no redirect at 404 (not found)', ( done )=> {
start_server( {
redirect: {
status_code: "401",
redirect: "/login"
}
} ).then( ( server )=> {
let options = {
method: "GET",
url: "/no",
credentials: {} // To bypass auth strategy
};

let options = {
method: "GET",
url: "/no",
credentials: {} // To bypass auth strategy
};

debug('before inject')

server.inject( options, ( response )=> {
debug( 'before inject' )

debug(response)
server.inject( options, ( response )=> {

code.expect( response.statusCode ).to.equal( 404 );
done();
code.expect( response.statusCode ).to.equal( 404 );
done();

} );
} );

} )

lab.test( 'login ok', ( done )=> {
start_server( {
redirect: {
status_code: "401",
redirect: "/login"
}
} ).then( ( server )=> {
let options = {
method: "GET",
url: "/login",
credentials: {} // To bypass auth strategy
};

debug( 'before inject' )

} );
server.inject( options, ( response )=> {

code.expect( response.statusCode ).to.equal( 200 );
done();

} );
} );
} )

lab.test( 'login ok', ( done )=> {
start_server( {
redirect: [{
status_code: "401",
redirect: "/login"
},
{
status_code: "200",
redirect: "/login"
}
]
} ).then( ( server )=> {
let options = {
method: "GET",
url: "/login",
credentials: {} // To bypass auth strategy
};

debug( 'before inject' )

server.inject( options, ( response )=> {

code.expect( response.statusCode ).to.equal( 200 );
done();

//module.exports = server;
} );
} );
} )
} );
Loading

0 comments on commit f2b97f9

Please sign in to comment.