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

100% Statements 30/30
100% Branches 0/0
100% Functions 18/18
100% Lines 30/30
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                                                        
const expect = require("expect");
const callApp = require("../../utils/callApp");
const proxy = require("../proxy");
 
function ok() {
    return "ok";
}
 
function target() {
    return "target";
}
 
function returnTrue() {
    return true;
}
 
function returnFalse() {
    return false;
}
 
describe("middleware/proxy", function () {
    describe("when no test function is given", function () {
        it("forwards the request", function () {
            return callApp(proxy(ok, target)).then(function (conn) {
                expect(conn.responseText).toEqual("target");
            });
        });
    });
 
    describe("a request that passes the test function", function () {
        it("is forwarded", function () {
            return callApp(proxy(ok, target, returnTrue)).then(function (conn) {
                expect(conn.responseText).toEqual("target");
            });
        });
    });
 
    describe("a request whose URL matches the test RegExp", function () {
        beforeEach(function () {
            return callApp(proxy(ok, target, /\/test-match$/), "/test-match");
        });
 
        it("is forwarded", function () {
            return callApp(proxy(ok, target, /\/test-match$/), "/test-match").then(function (conn) {
                expect(conn.responseText).toEqual("target");
            });
        });
    });
 
    describe("a request that does not pass the test function", function () {
        it("is not forwarded", function () {
            return callApp(proxy(ok, target, returnFalse), "/test-match").then(function (conn) {
                expect(conn.responseText).toEqual("ok");
            });
        });
    });
});