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

100% Statements 25/25
100% Branches 0/0
100% Functions 14/14
100% Lines 25/25
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                                          
const expect = require("expect");
const callApp = require("../../utils/callApp");
const rewrite = require("../rewrite");
 
function echoPathname(conn) {
    return conn.pathname;
}
 
describe("middleware/rewrite", function () {
    let app = rewrite(echoPathname, "/abc", "/xyz");
    app = rewrite(app, /\/def/g, "/xyz");
    app = rewrite(app, "/abc.jpeg", "/def.jpeg");
 
    describe("GET /abc", function () {
        it("rewrites the pathname properly", function () {
            return callApp(app, "/abc").then(function (conn) {
                expect(conn.responseText).toEqual("/xyz");
            });
        });
    });
 
    describe("GET /def", function () {
        it("rewrites the pathname properly", function () {
            return callApp(app, "/def").then(function (conn) {
                expect(conn.responseText).toEqual("/xyz");
            });
        });
    });
 
    describe("GET /def/path/def", function () {
        it("rewrites the pathname properly", function () {
            return callApp(app, "/def/path/def").then(function (conn) {
                expect(conn.responseText).toEqual("/xyz/path/xyz");
            });
        });
    });
 
    describe("when the pattern contains special RegExp chars", function () {
        it("rewrites the pathname properly", function () {
            return callApp(app, "/abcdjpeg").then(function (conn) {
                expect(conn.responseText).toEqual("/abcdjpeg");
            });
        });
    });
});