all files / modules/middleware/__tests__/ session-test.js

100% Statements 58/58
100% Branches 2/2
100% Functions 29/29
100% Lines 58/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112   12× 12×   12×   12×                                                                                                      
const assert = require("assert");
const expect = require("expect");
const callApp = require("../../utils/callApp");
const CookieStore = require("../session/CookieStore");
const MemoryStore = require("../session/MemoryStore");
const session = require("../session");
 
function counter(request) {
    const session = request.session;
    assert(session);
 
    session.count = (session.count || 0) + 1;
 
    return JSON.stringify(session);
}
 
function getSessionCookie(setCookieHeader) {
    const match = setCookieHeader.match(/_session=[^;]+/);
    assert(match);
    return match[0];
}
 
describe("middleware/session", function () {
    describe("when using a server-side store", function () {
        const store = new MemoryStore({expireAfter: 10});
        const app = session(counter, {
            secret: "secret",
            store
        });
 
        describe("when a request is made", function () {
            it("sets a cookie in the response", function () {
                return callApp(app).then(function (conn) {
                    assert(conn.response.headers["Set-Cookie"]);
                });
            });
 
            it("instantiates a new session", function () {
                return callApp(app).then(function (conn) {
                    expect(JSON.parse(conn.responseText).count).toEqual(1);
                });
            });
 
            describe("and then another", function () {
                it("does not set a cookie in the response", function () {
                    return callApp(app).then(function (conn) {
                        const cookie = getSessionCookie(conn.response.headers["Set-Cookie"]);
 
                        return callApp(app, {headers: {Cookie: cookie}}).then(function (conn) {
                            assert(!conn.response.headers["Set-Cookie"]);
                        });
                    });
                });
 
                it("persists session data", function () {
                    return callApp(app).then(function (conn) {
                        const cookie = getSessionCookie(conn.response.headers["Set-Cookie"]);
 
                        return callApp(app, {headers: {Cookie: cookie}}).then(function (conn) {
                            expect(JSON.parse(conn.responseText).count).toEqual(2);
                        });
                    });
                });
            });
        });
    });
 
    describe("when using a client-side store", function () {
        const store = new CookieStore({secret: "secret"});
        const app = session(counter, {
            secret: "secret",
            store
        });
 
        describe("when a request is made", function () {
            it("sets a cookie in the response", function () {
                return callApp(app).then(function (conn) {
                    assert(conn.response.headers["Set-Cookie"]);
                });
            });
 
            it("instantiates a new session", function () {
                return callApp(app).then(function (conn) {
                    expect(JSON.parse(conn.responseText).count).toEqual(1);
                });
            });
 
            describe("and then another", function () {
                it("sets a cookie in the response", function () {
                    return callApp(app).then(function (conn) {
                        const cookie = getSessionCookie(conn.response.headers["Set-Cookie"]);
 
                        return callApp(app, {headers: {Cookie: cookie}}).then(function (conn) {
                            assert(conn.response.headers["Set-Cookie"]);
                        });
                    });
                });
 
                it("persists session data", function () {
                    return callApp(app).then(function (conn) {
                        const cookie = getSessionCookie(conn.response.headers["Set-Cookie"]);
 
                        return callApp(app, {headers: {Cookie: cookie}}).then(function (conn) {
                            expect(JSON.parse(conn.responseText).count).toEqual(2);
                        });
                    });
                });
            });
        });
    });
});