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

100% Statements 64/64
100% Branches 2/2
100% Functions 39/39
100% Lines 64/64
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 113 114 115 116 117 118 119 120 121 122 123 124     10× 26×                                                                                                                    
const expect = require("expect");
const callApp = require("../../utils/callApp");
const router = require("../router");
 
const UNDEF = "__UNDEFINED__"; // So we can test for undefined.
 
function stringifyParams(conn) {
    return JSON.stringify(conn.params, function (key, value) {
        return value === undefined ? UNDEF : value;
    });
}
 
describe("middleware/router", function () {
    const app = router();
 
    app.route("/posts/:id", "GET", stringifyParams);
    app.route("/posts/:id", ["POST", "DELETE"], stringifyParams);
    app.route("/feeds/:id.?:format?", stringifyParams);
    app.route("/files/*.*", stringifyParams);
    app.route(/\/users\/(\d+)/i, stringifyParams);
    app.route("POST /comments", stringifyParams);
 
    describe("when a match cannot be made", function () {
        it("returns 404", function () {
            return callApp(app, "/").then(function (conn) {
                expect(conn.status).toEqual(404);
            });
        });
    });
 
    describe("GET /posts/1", function () {
        it("has the correct params", function () {
            return callApp(app, "/posts/1").then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({id: "1"});
            });
        });
    });
 
    describe("POST /posts/2", function () {
        it("has the correct params", function () {
            return callApp(app, {method: "POST", url: "/posts/2"}).then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({id: "2"});
            });
        });
    });
 
    describe("DELETE /posts/3", function () {
        it("has the correct params", function () {
            return callApp(app, {method: "DELETE", url: "/posts/3"}).then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({id: "3"});
            });
        });
    });
 
    describe("GET /feeds/5", function () {
        it("has the correct params", function () {
            return callApp(app, "/feeds/5").then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({id: "5", format: UNDEF});
            });
        });
    });
 
    describe("GET /feeds/5.html", function () {
        it("has the correct params", function () {
            return callApp(app, "/feeds/5.html").then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({id: "5", format: "html"});
            });
        });
    });
 
    describe("GET /files/feed.xml", function () {
        it("has the correct params", function () {
            return callApp(app, "/files/feed.xml").then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({splat: ["feed", "xml"]});
            });
        });
    });
 
    describe("GET /files/feed.", function () {
        it("has the correct params", function () {
            return callApp(app, "/files/feed.").then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({splat: ["feed", ""]});
            });
        });
    });
 
    describe("GET /files/.xml", function () {
        it("has the correct params", function () {
            return callApp(app, "/files/.xml").then(function (conn) {
                expect(JSON.parse(conn.responseText)).toEqual({splat: ["", "xml"]});
            });
        });
    });
 
    describe("PUT /posts/1", function () {
        it("has the correct params", function () {
            return callApp(app, {method: "PUT", path: "/posts/1"}).then(function (conn) {
                expect(conn.status).toEqual(404);
            });
        });
    });
 
    describe("GET /users/1", function () {
        it("has the correct params", function () {
            return callApp(app, "/users/1").then(function (conn) {
        // Regular expressions don't have named parameters.
                expect(JSON.parse(conn.responseText)).toEqual({});
            });
        });
    });
 
    describe("POST /comments", function () {
        it("routes the request correctly", function () {
            return callApp(app, {
                method: "POST",
                url: "/comments"
            }).then(function (conn) {
                expect(conn.method).toEqual("POST");
                expect(conn.status).toEqual(200);
            });
        });
    });
});