<commit_before>// Load modules

var Lab = require('lab');
var Hapi = require('hapi');


// Declare internals

var internals = {};


// Test shortcuts

var expect = Lab.expect;
var before = Lab.before;
var after = Lab.after;
var describe = Lab.experiment;
var it = Lab.test;


describe('Cookie', function () {

    var server = new Hapi.Server();
    before(function (done) {

        server.pack.require('../', function (err) {

            expect(err).to.not.exist;

            server.auth.strategy('default', 'cookie', {
                password: 'password',
                ttl: 60 * 1000,
                domain: 'example.com',
                cookie: 'special',
                clearInvalid: true,
                validateFunc: function (session, callback) {

                    var override = Hapi.utils.clone(session);
                    override.something = 'new';

                    return callback(null, session.user === 'valid', override);
                }
            });

            server.route({
                method: 'GET', path: '/login/{user}',
                config: {
                    auth: { mode: 'try' },
                    handler: function (request, reply) {

                        request.auth.session.set({ user: request.params.user });
                        return reply(request.params.user);
                    }
                }
            });

            server.route({
                method: 'GET', path: '/resource', handler: function (request, reply) {

                    expect(request.auth.credentials.something).to.equal('new');
                    return reply('resource');
                },
                config: { auth: true }
            });

            server.route({
                method: 'GET', path: '/logout', handler: function (request, reply) {

                    request.auth.session.clear();
                    return reply('logged-out');
                }, config: { auth: true }
            });

            done();
        });
    });

    it('authenticates a request', function (done) {

        server.inject('/login/valid', function (res) {

            expect(res.result).to.equal('valid');
            var header = res.headers['set-cookie'];
            expect(header.length).to.equal(1);
            expect(header[0]).to.contain('Max-Age=60');
            var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

            server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                expect(res.statusCode).to.equal(200);
                expect(res.result).to.equal('resource');
                done();
            });
        });
    });

    it('ends a session', function (done) {

        server.inject('/login/valid', function (res) {

            expect(res.result).to.equal('valid');
            var header = res.headers['set-cookie'];
            expect(header.length).to.equal(1);
            expect(header[0]).to.contain('Max-Age=60');
            var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

            server.inject({ method: 'GET', url: '/logout', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                expect(res.statusCode).to.equal(200);
                expect(res.result).to.equal('logged-out');
                expect(res.headers['set-cookie'][0]).to.equal('special=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; Domain=example.com; Path=/');
                done();
            });
        });
    });

    it('fails a request with invalid session', function (done) {

        server.inject('/login/invalid', function (res) {

            expect(res.result).to.equal('invalid');
            var header = res.headers['set-cookie'];
            expect(header.length).to.equal(1);
            expect(header[0]).to.contain('Max-Age=60');
            var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

            server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                expect(res.headers['set-cookie'][0]).to.equal('special=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; Domain=example.com; Path=/');
                expect(res.statusCode).to.equal(401);
                done();
            });
        });
    });

    it('authenticates a request', function (done) {

        var server = new Hapi.Server();
        server.pack.require('../', function (err) {

            expect(err).to.not.exist;

            server.auth.strategy('default', 'cookie', {
                password: 'password',
                ttl: 60 * 1000,
                cookie: 'special',
                clearInvalid: true
            });

            server.route({
                method: 'GET', path: '/login/{user}',
                config: {
                    auth: { mode: 'try' },
                    handler: function (request, reply) {

                        request.auth.session.set({ user: request.params.user });
                        return reply(request.params.user);
                    }
                }
            });

            server.route({
                method: 'GET', path: '/resource', handler: function (request, reply) {

                    expect(request.auth.credentials.user).to.equal('steve');
                    return reply('resource');
                },
                config: { auth: true }
            });

            server.inject('/login/steve', function (res) {

                expect(res.result).to.equal('steve');
                var header = res.headers['set-cookie'];
                expect(header.length).to.equal(1);
                expect(header[0]).to.contain('Max-Age=60');
                var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

                server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                    expect(res.statusCode).to.equal(200);
                    expect(res.result).to.equal('resource');
                    done();
                });
            });
        });
    });

    describe('redirection', function (done) {

        it('sends to login page (uri without query)', function (done) {

            var server = new Hapi.Server();
            server.pack.require('../', function (err) {

                expect(err).to.not.exist;

                server.auth.strategy('default', 'cookie', {
                    password: 'password',
                    ttl: 60 * 1000,
                    redirectTo: 'http://example.com/login',
                    appendNext: true
                });

                server.route({
                    method: 'GET', path: '/', handler: function (request, reply) {

                        return reply('never');
                    }, config: { auth: true }
                });

                server.inject('/', function (res) {

                    expect(res.statusCode).to.equal(302);
                    expect(res.headers.location).to.equal('http://example.com/login?next=%2F');
                    done();
                });
            });
        });<commit_msg>Coverage. Closes #5<commit_after>// Load modules

var Lab = require('lab');
var Hapi = require('hapi');


// Declare internals

var internals = {};


// Test shortcuts

var expect = Lab.expect;
var before = Lab.before;
var after = Lab.after;
var describe = Lab.experiment;
var it = Lab.test;


describe('Cookie', function () {

    var server = new Hapi.Server();
    before(function (done) {

        server.pack.require('../', function (err) {

            expect(err).to.not.exist;

            server.auth.strategy('default', 'cookie', {
                password: 'password',
                ttl: 60 * 1000,
                domain: 'example.com',
                cookie: 'special',
                clearInvalid: true,
                validateFunc: function (session, callback) {

                    var override = Hapi.utils.clone(session);
                    override.something = 'new';

                    return callback(null, session.user === 'valid', override);
                }
            });

            server.route({
                method: 'GET', path: '/login/{user}',
                config: {
                    auth: { mode: 'try' },
                    handler: function (request, reply) {

                        request.auth.session.set({ user: request.params.user });
                        return reply(request.params.user);
                    }
                }
            });

            server.route({
                method: 'GET', path: '/resource', handler: function (request, reply) {

                    expect(request.auth.credentials.something).to.equal('new');
                    return reply('resource');
                },
                config: { auth: true }
            });

            server.route({
                method: 'GET', path: '/logout', handler: function (request, reply) {

                    request.auth.session.clear();
                    return reply('logged-out');
                }, config: { auth: true }
            });

            done();
        });
    });

    it('authenticates a request', function (done) {

        server.inject('/login/valid', function (res) {

            expect(res.result).to.equal('valid');
            var header = res.headers['set-cookie'];
            expect(header.length).to.equal(1);
            expect(header[0]).to.contain('Max-Age=60');
            var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

            server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                expect(res.statusCode).to.equal(200);
                expect(res.result).to.equal('resource');
                done();
            });
        });
    });

    it('ends a session', function (done) {

        server.inject('/login/valid', function (res) {

            expect(res.result).to.equal('valid');
            var header = res.headers['set-cookie'];
            expect(header.length).to.equal(1);
            expect(header[0]).to.contain('Max-Age=60');
            var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

            server.inject({ method: 'GET', url: '/logout', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                expect(res.statusCode).to.equal(200);
                expect(res.result).to.equal('logged-out');
                expect(res.headers['set-cookie'][0]).to.equal('special=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; Domain=example.com; Path=/');
                done();
            });
        });
    });

    it('fails a request with invalid session', function (done) {

        server.inject('/login/invalid', function (res) {

            expect(res.result).to.equal('invalid');
            var header = res.headers['set-cookie'];
            expect(header.length).to.equal(1);
            expect(header[0]).to.contain('Max-Age=60');
            var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

            server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                expect(res.headers['set-cookie'][0]).to.equal('special=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; Domain=example.com; Path=/');
                expect(res.statusCode).to.equal(401);
                done();
            });
        });
    });

    it('authenticates a request', function (done) {

        var server = new Hapi.Server();
        server.pack.require('../', function (err) {

            expect(err).to.not.exist;

            server.auth.strategy('default', 'cookie', {
                password: 'password',
                ttl: 60 * 1000,
                cookie: 'special',
                clearInvalid: true
            });

            server.route({
                method: 'GET', path: '/login/{user}',
                config: {
                    auth: { mode: 'try' },
                    handler: function (request, reply) {

                        request.auth.session.set({ user: request.params.user });
                        return reply(request.params.user);
                    }
                }
            });

            server.route({
                method: 'GET', path: '/resource', handler: function (request, reply) {

                    expect(request.auth.credentials.user).to.equal('steve');
                    return reply('resource');
                },
                config: { auth: true }
            });

            server.inject('/login/steve', function (res) {

                expect(res.result).to.equal('steve');
                var header = res.headers['set-cookie'];
                expect(header.length).to.equal(1);
                expect(header[0]).to.contain('Max-Age=60');
                var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

                server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                    expect(res.statusCode).to.equal(200);
                    expect(res.result).to.equal('resource');
                    done();
                });
            });
        });
    });

    it('errors in validation function', function (done) {

        var server = new Hapi.Server();
        server.pack.require('../', function (err) {

            expect(err).to.not.exist;

            server.auth.strategy('default', 'cookie', {
                password: 'password',
                ttl: 60 * 1000,
                cookie: 'special',
                clearInvalid: true,
                validateFunc: function (session, callback) { return callback(new Error('boom')); }
            });

            server.route({
                method: 'GET', path: '/login/{user}',
                config: {
                    auth: { mode: 'try' },
                    handler: function (request, reply) {

                        request.auth.session.set({ user: request.params.user });
                        return reply(request.params.user);
                    }
                }
            });

            server.route({
                method: 'GET', path: '/resource', handler: function (request, reply) {

                    expect(request.auth.credentials.user).to.equal('steve');
                    return reply('resource');
                },
                config: { auth: true }
            });

            server.inject('/login/steve', function (res) {

                expect(res.result).to.equal('steve');
                var header = res.headers['set-cookie'];
                expect(header.length).to.equal(1);
                expect(header[0]).to.contain('Max-Age=60');
                var cookie = header[0].match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/);

                server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } }, function (res) {

                    expect(res.statusCode).to.equal(401);
                    done();
                });
            });
        });
    });

    describe('redirection', function (done) {

        it('sends to login page (uri without query)', function (done) {

            var server = new Hapi.Server();
            server.pack.require('../', function (err) {

                expect(err).to.not.exist;

                server.auth.strategy('default', 'cookie', {
                    password: 'password',
                    ttl: 60 * 1000,
                    redirectTo: 'http://example.com/login',
                    appendNext: true
                });

                server.route({
                    method: 'GET', path: '/', handler: function (request, reply) {

                        return reply('never');
                    }, config: { auth: true }
                });

                server.inject('/', function (res) {

                    expect(res.statusCode).to.equal(302);
                    expect(res.headers.location).to.equal('http://example.com/login?next=%2F');
                    done();
                });
            });
        });
{'fraction_non_alphanumeric': 0.1843307039914596, 'fraction_numerical': 0.016487752802324892, 'mean_word_length': 1.4652046783625732, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655832', 'n_tokens_mistral': 4996, 'n_tokens_neox': 4508, 'n_words': 832}