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

100% Statements 48/48
100% Branches 0/0
100% Functions 25/25
100% Lines 48/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 88 89 90 91 92 93                                                                                          
const expect = require("expect");
const callApp = require("../../utils/callApp");
const createMapper = require("../mapper");
 
describe("middleware/mapper", function () {
    function showInfo(conn) {
        return {
            status: 200,
            headers: {
                Basename: conn.basename,
                Pathname: conn.pathname
            }
        };
    }
 
    let app;
    beforeEach(function () {
        app = createMapper({
            "/one": showInfo,
            "http://example.org/two": showInfo,
            "http://example.net/three": showInfo
        });
    });
 
    describe("when the request does not match a mapping", function () {
        it("passes the request through to the default app", function () {
            return callApp(app, "/three").then(function (conn) {
                expect(conn.status).toEqual(404);
            });
        });
    });
 
    describe("when a request matches a mapping by path", function () {
        describe("and the mapping has a different host", function () {
            it("passes the request through to the default app", function () {
                return callApp(app, "http://example.com/two").then(function (conn) {
                    expect(conn.status).toEqual(404);
                });
            });
        });
 
        describe("and the mapping has no host", function () {
            it("passes the request through to the mapping", function () {
                return callApp(app, "/one/messages").then(function (conn) {
                    expect(conn.status).toEqual(200);
                    expect(conn.response.headers.Basename).toEqual("/one");
                    expect(conn.response.headers.Pathname).toEqual("/messages");
                });
            });
        });
 
        describe("and the mapping has a matching host", function () {
            it("passes the request through to the mapping", function () {
                return callApp(app, "http://example.org/two/messages").then(function (conn) {
                    expect(conn.status).toEqual(200);
                    expect(conn.response.headers.Basename).toEqual("/two");
                    expect(conn.response.headers.Pathname).toEqual("/messages");
                });
            });
 
            describe("and the request is on a different port", function () {
                it("passes the request through to the mapping", function () {
                    return callApp(app, "http://example.org:5000/two/messages").then(function (conn) {
                        expect(conn.status).toEqual(200);
                        expect(conn.response.headers.Basename).toEqual("/two");
                        expect(conn.response.headers.Pathname).toEqual("/messages");
                    });
                });
            });
 
            describe("that was specified with a custom port", function () {
                it("passes the request through to the mapping", function () {
                    return callApp(app, "http://example.net/three/messages").then(function (conn) {
                        expect(conn.status).toEqual(200);
                        expect(conn.response.headers.Basename).toEqual("/three");
                        expect(conn.response.headers.Pathname).toEqual("/messages");
                    });
                });
            });
        });
 
        describe("and there is no remaining path", function () {
            it("passes the request through to the mapping", function () {
                return callApp(app, "/one").then(function (conn) {
                    expect(conn.status).toEqual(200);
                    expect(conn.response.headers.Basename).toEqual("/one");
                    expect(conn.response.headers.Pathname).toEqual("/");
                });
            });
        });
    });
});