All files / lib/middleware resHead.js

10% Statements 2/20
0% Branches 0/16
0% Functions 0/3
10% Lines 2/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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              1x   1x                                                                        
"use strict";
/**
 * Middleware to cache responses.
 *
 * @module
 */
 
var _ = require("lodash");
 
module.exports = function () {
 
    var res = this.res;
 
    var resHead = res.writeHead;
    var resHeader = res.setHeader;
 
    res.headers = res.headers || {};
 
    res.writeHead = function (statusCode, statusMsg, headers) {
 
        if (_.isPlainObject(statusMsg) && !headers) {
            headers = statusMsg;
            statusMsg = undefined;
        };
 
        if (statusCode) {
            res.statusCode = res.statusCode || statusCode;
        };
 
        if (statusMsg) {
            res.statusMessage = res.statusMessage || statusMsg;
        };
 
        if (_.isPlainObject(headers)) {
            _.assign(res.headers, headers);
        };
 
        return resHead.apply(this, arguments);
    };
 
    res.setHeader = function(name, value) {
        res.headers[name] = value;
        return resHeader.apply(this, arguments);
    };
};