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

100% Statements 32/32
100% Branches 0/0
100% Functions 18/18
100% Lines 32/32
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                                                        
const expect = require("expect");
const callApp = require("../../utils/callApp");
const methodOverride = require("../methodOverride");
const params = require("../params");
 
describe("middleware/methodOverride", function () {
    const app = methodOverride(function (conn) {
        return conn.method;
    });
 
    describe("when the request method is given in a request parameter", function () {
        describe("and the params middleware is in front", function () {
            it("sets the request method", function () {
                return callApp(params(app), {params: {_method: "PUT"}}).then(function (conn) {
                    expect(conn.responseText).toEqual("PUT");
                });
            });
        });
 
        describe("but the params middleware is not in front", function () {
            let errors, errorHandler;
            beforeEach(function () {
                errors = [];
                errorHandler = function (error) {
                    errors.push(error);
                };
            });
 
            it("does not set the request method and generates an error", function () {
                return callApp(app, {
                    params: {_method: "PUT"},
                    onError: errorHandler
                }).then(function (conn) {
                    expect(conn.responseText).toEqual("GET");
                    expect(errors.length).toEqual(1);
                    expect(errors[0].message).toEqual("No params! Use mach.params in front of mach.methodOverride");
                });
            });
        });
    });
 
    describe("when the request method is given in a request parameter with multiple values", function () {
        describe("and the params middleware is in front", function () {
            it("sets the request method to the last given value", function () {
                return callApp(params(app), {params: {_method: ["PUT", "DELETE"]}}).then(function (conn) {
                    expect(conn.responseText).toEqual("DELETE");
                });
            });
        });
    });
 
    describe("when the request method is given in an HTTP header", function () {
        it("sets the request method", function () {
            return callApp(app, {headers: {"X-Http-Method-Override": "PUT"}}).then(function (conn) {
                expect(conn.responseText).toEqual("PUT");
            });
        });
    });
});