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

100% Statements 16/16
100% Branches 0/0
100% Functions 8/8
100% Lines 16/16
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                                          
const expect = require("expect");
const callApp = require("../../utils/callApp");
const paramsMiddleware = require("../params");
 
function stringifyParams(conn) {
    return JSON.stringify(conn.params);
}
 
describe("middleware/params", function () {
    describe("when both query and content parameters are present", function () {
        it("merges query and content parameters, giving precedence to content", function () {
            return callApp(paramsMiddleware(stringifyParams), {
                method: "POST",
                url: "/?a=b&c=d",
                headers: {"Content-Type": "application/x-www-form-urlencoded"},
                content: "a=c"
            }).then(function (conn) {
                const params = JSON.parse(conn.responseText);
                expect(params.a).toEqual("c");
                expect(params.c).toEqual("d");
            });
        });
    });
 
    describe("when the request content length exceeds the maximum allowed length", function () {
        it("returns 413", function () {
            return callApp(paramsMiddleware(stringifyParams, {maxLength: 100}), {
                method: "POST",
                headers: {"Content-Type": "application/x-www-form-urlencoded"},
                content: `q=${Array(100).join("a")}`
            }).then(function (conn) {
                expect(conn.status).toEqual(413);
            });
        });
    });
});