all files / modules/headers/__tests__/ Accept-test.js

100% Statements 34/34
100% Branches 0/0
100% Functions 14/14
100% Lines 34/34
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 61 62 63 64                                                           
/* jshint -W058 */
const assert = require("assert");
const expect = require("expect");
const Accept = require("../Accept");
 
describe("Accept", function () {
    let header;
 
    describe("with no value", function () {
        beforeEach(function () {
            header = new Accept();
        });
 
        it("has the correct toString representation", function () {
            expect(header.toString()).toEqual("Accept: */*");
        });
 
        it("has the correct quality factors", function () {
            expect(header.qualityFactorForMediaType("text/html")).toEqual(1);
        });
 
        it("accepts text/html", function () {
            assert(header.accepts("text/html"));
        });
    });
 
    describe("with a value of \"text/html\"", function () {
        beforeEach(function () {
            header = new Accept("text/html");
        });
 
        it("has the correct quality factors", function () {
            expect(header.qualityFactorForMediaType("text/html")).toEqual(1);
            expect(header.qualityFactorForMediaType("image/png")).toEqual(0);
        });
 
        it("accepts text/html", function () {
            assert(header.accepts("text/html"));
        });
 
        it("does not accept image/png", function () {
            assert(!header.accepts("image/png"));
        });
    });
 
    describe("with a value of \"text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5\"",
        function () {
            beforeEach(function () {
                header = new Accept(
                    "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"
                );
            });
 
            it("has the correct quality factors", function () {
                expect(header.qualityFactorForMediaType("text/html;level=1")).toEqual(1);
                expect(header.qualityFactorForMediaType("text/html")).toEqual(0.7);
                expect(header.qualityFactorForMediaType("text/plain")).toEqual(0.3);
                expect(header.qualityFactorForMediaType("image/jpeg")).toEqual(0.5);
                expect(header.qualityFactorForMediaType("text/html;level=2")).toEqual(0.4);
                expect(header.qualityFactorForMediaType("text/html;level=3")).toEqual(0.7);
            });
        });
});