all files / modules/__tests__/ Message-test.js

97.32% Statements 109/112
100% Branches 0/0
94.92% Functions 56/59
97.32% Lines 109/112
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                                                                                                                                                                                           
/* jshint -W058 */
const assert = require("assert");
const expect = require("expect");
const bodec = require("bodec");
const MaxLengthExceededError = require("../utils/MaxLengthExceededError");
const Message = require("../Message");
 
describe("a Message with \"Content-Type: text/plain\"", function () {
    let message;
    beforeEach(function () {
        message = new Message(null, {"Content-Type": "text/plain"});
    });
 
    it("has the correct mediaType", function () {
        expect(message.mediaType).toEqual("text/plain");
    });
});
 
describe("a Message with \"Content-Type: text/html; charset=utf-8\"", function () {
    let message;
    beforeEach(function () {
        message = new Message(null, {"Content-Type": "text/html; charset=utf-8"});
    });
 
    it("has the correct mediaType", function () {
        expect(message.mediaType).toEqual("text/html");
    });
 
    it("has the correct charset", function () {
        expect(message.charset).toEqual("utf-8");
    });
 
    describe("and the media type is modified", function () {
        beforeEach(function () {
            message.mediaType = "text/plain";
        });
 
        it("has the correct mediaType", function () {
            expect(message.mediaType).toEqual("text/plain");
        });
 
        it("has the correct charset", function () {
            expect(message.charset).toEqual("utf-8");
        });
    });
 
    describe("and the charset is modified", function () {
        beforeEach(function () {
            message.charset = "iso-5589-1";
        });
 
        it("has the correct mediaType", function () {
            expect(message.mediaType).toEqual("text/html");
        });
 
        it("has the correct charset", function () {
            expect(message.charset).toEqual("iso-5589-1");
        });
    });
});
 
describe("Message#addHeader", function () {
    let message;
    beforeEach(function () {
        message = new Message();
    });
 
    it("normalizes header names", function () {
        message.addHeader("content-type", "text/html");
        expect(message.headers["Content-Type"]).toEqual("text/html");
    });
 
    describe("when the header has not been previously set", function () {
        it("sets the header to the given value", function () {
            message.addHeader("Test", "value");
            expect(message.headers.Test).toEqual("value");
 
            message.addHeader("Test-Int", 1);
            expect(message.headers["Test-Int"]).toEqual(1);
        });
    });
 
    describe("when the header has been previously set", function () {
        beforeEach(function () {
            message.addHeader("Test", "previousValue");
        });
 
        it("sets the header to an array of header values", function () {
            message.addHeader("Test", "value");
            expect(message.headers.Test).toEqual(["previousValue", "value"]);
        });
    });
});
 
describe("Message#bufferContent", function () {
    let message;
    beforeEach(function () {
        message = new Message();
    });
 
    it("responds to the message content being set", function () {
        message.content = "foo";
        return message
      .bufferContent()
      .then(function (content) {
          expect(bodec.toString(content)).toEqual("foo");
 
          message.content = "bar";
          return message.bufferContent();
      })
      .then(function (content) {
          expect(bodec.toString(content)).toEqual("bar");
      });
    });
});
 
describe("Message#parseContent", function () {
    let message;
    beforeEach(function () {
        message = new Message();
    });
 
    describe("when using an unknown Content-Type", function () {
        beforeEach(function () {
            message.headers["Content-Type"] = "text/plain";
        });
 
        it("returns the content as a string", function () {
            return message.parseContent().then(function (params) {
                expect(params).toEqual("");
            });
        });
    }); // text/plain
 
    describe("when using Content-Type: application/json", function () {
        describe("when the content is valid JSON", function () {
            let object;
            beforeEach(function () {
                object = {a: 1, b: "some value"};
                message = new Message(
          JSON.stringify(object), {"Content-Type": "application/json"}
        );
            });
 
            it("parses the content", function () {
                return message.parseContent().then(function (params) {
                    expect(params).toEqual(object);
                });
            });
        });
 
        describe("when the content is not valid JSON", function () {
            beforeEach(function () {
                message = new Message(
          "hello world", {"Content-Type": "application/json"}
        );
            });
 
            it("throws an error", function () {
                return message.parseContent().then(function () {
                    assert(false, "successfully parsed invalid JSON");
                }, function (error) {
                    assert(error);
                });
            });
        });
 
        describe("when the content is too large", function () {
            beforeEach(function () {
                message = new Message(
          "{}", {"Content-Type": "application/json"}
        );
            });
 
            it("throws MaxLengthExceededError", function () {
                return message.parseContent(1).then(function () {
                    assert(false, "successfully parsed a content stream that is too large");
                }, function (error) {
                    assert(error);
                    expect(error).toBeA(MaxLengthExceededError);
                });
            });
        });
    }); // application/json
 
    describe("when using Content-Type: application/x-www-form-urlencoded", function () {
        describe("when the content is URL-encoded", function () {
            beforeEach(function () {
                message = new Message(
          "a=1&b=some+value", {"Content-Type": "application/x-www-form-urlencoded"}
        );
            });
 
            it("parses the content", function () {
                return message.parseContent().then(function (params) {
                    expect(params).toEqual({a: "1", b: "some value"});
                });
            });
        });
 
        describe("when the content is too large", function () {
            beforeEach(function () {
                message = new Message(
          "a=b", {"Content-Type": "application/x-www-form-urlencoded"}
        );
            });
 
            it("throws MaxLengthExceededError", function () {
                return message.parseContent(1).then(function () {
                    assert(false, "successfully parsed a content stream that is too large");
                }, function (error) {
                    assert(error);
                    expect(error).toBeA(MaxLengthExceededError);
                });
            });
        });
    }); // application/x-www-form-urlencoded
});