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

100% Statements 28/28
100% Branches 0/0
100% Functions 15/15
100% Lines 28/28
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 gzip = require("../gzip");
const callApp = require("../../utils/callApp");
const getFixture = require("./getFixture");
 
describe("middleware/gzip", function () {
    const contents = getFixture("test.txt");
    const gzippedContents = getFixture("test.txt.gz");
    const app = gzip(function () {
        return {
            headers: {"Content-Type": "text/plain"},
            content: contents
        };
    });
 
    describe("when the client accepts gzip encoding", function () {
        it("gzip-encodes the response", function () {
            return callApp(app, {
                headers: {"Accept-Encoding": "gzip"},
                binary: true
            }).then(function (conn) {
                expect(conn.response.headers["Content-Encoding"]).toEqual("gzip");
                expect(conn.response.headers.Vary).toEqual("Accept-Encoding");
                return conn.response.bufferContent().then(function (buffer) {
                    expect(buffer).toEqual(gzippedContents);
                });
            });
        });
    });
 
    describe("when the client does not accept gzip encoding", function () {
        it("does not encode the content", function () {
            return callApp(app, {binary: true}).then(function (conn) {
                return conn.response.bufferContent().then(function (buffer) {
                    expect(buffer).toEqual(contents);
                });
            });
        });
    });
 
    describe("when the response is a text/event-stream", function () {
        const app = gzip(function () {
            return {
                headers: {"Content-Type": "text/event-stream"},
                content: contents
            };
        });
 
        it("does not encode the content", function () {
            return callApp(app, {binary: true}).then(function (conn) {
                return conn.response.bufferContent().then(function (buffer) {
                    expect(buffer).toEqual(contents);
                });
            });
        });
    });
});