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

100% Statements 23/23
100% Branches 0/0
100% Functions 13/13
100% Lines 23/23
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                                        
const expect = require("expect");
const callApp = require("../../utils/callApp");
const charset = require("../charset");
 
function ok() {
    return 200;
}
 
function jsonISO88591(conn) {
    conn.json(200, "hi there");
    conn.response.contentType = "application/json; charset=iso-8859-1";
}
 
function json(conn) {
    conn.json(200, "hi there");
}
 
describe("middleware/charset", function () {
    describe("when the response does not have a Content-Type", function () {
        it("does not set a charset", function () {
            return callApp(charset(ok, "utf-8")).then(function (conn) {
                expect(conn.response.charset).toBe(null);
            });
        });
    });
 
    describe("when the response has a Content-Type with a charset", function () {
        it("does not modify the existing charset", function () {
            return callApp(charset(jsonISO88591, "utf-8")).then(function (conn) {
                expect(conn.response.charset).toBe("iso-8859-1");
            });
        });
    });
 
    describe("when the response has a Content-Type with no charset", function () {
        it("adds the given charset", function () {
            return callApp(charset(json, "utf-8")).then(function (conn) {
                expect(conn.response.charset).toEqual("utf-8");
            });
        });
    });
});