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

97.92% Statements 47/48
100% Branches 0/0
95.83% Functions 23/24
97.92% Lines 47/48
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   15× 15×                                                                              
const assert = require("assert");
const expect = require("expect");
const callApp = require("../../utils/callApp");
const stack = require("../stack");
const R = require("ramda");
 
function addHeader(app, headerName) {
    return function (conn) {
        return conn.call(app).then(function () {
            conn.response.headers[headerName] = "1";
        });
    };
}
 
describe("middleware/stack", function () {
    const app = stack();
 
    app.use(addHeader, "One");
    app.use(addHeader, "Two");
 
    app.map("/images", function () {
        return "an image";
    });
 
    app.get("/home", function () {
        return "welcome home!";
    });
 
    app.get("/:username", function (conn) {
        return `welcome ${conn.params.username}`;
    });
 
    app.use(addHeader, "Three");
 
    describe("a request that does not match any mappings or routes", function () {
        it("calls all middleware", function () {
            return callApp(app, "/").then(function (conn) {
                assert(conn.response.headers.One);
                assert(conn.response.headers.Two);
                assert(conn.response.headers.Three);
            });
        });
    });
 
    describe("a request that matches a location in front of some middleware", function () {
        it("calls the correct app", function () {
            return callApp(app, "/images").then(function (conn) {
                expect(conn.responseText).toEqual("an image");
            });
        });
 
        it("calls all middleware in front of that location", function () {
            return callApp(app, "/images").then(function (conn) {
                assert(conn.response.headers.One);
                assert(conn.response.headers.Two);
            });
        });
 
        it("does not call any middleware after that location", function () {
            return callApp(app, "/images").then(function (conn) {
                assert(R.isNil(conn.response.headers.Three));
            });
        });
    });
 
    describe("a request that matches a route in front of some middleware", function () {
        it("calls the correct app", function () {
            return callApp(app, "/home").then(function (conn) {
                expect(conn.responseText).toEqual("welcome home!");
            });
        });
 
        it("calls all middlware in front of that route", function () {
            return callApp(app, "/home").then(function (conn) {
                assert(conn.response.headers.One);
                assert(conn.response.headers.Two);
            });
        });
 
        it("does not call middleware after that route", function () {
            return callApp(app, "/home").then(function (conn) {
                assert(R.isNil(conn.response.headers.Three));
            });
        });
    });
});