[Pkg-javascript-commits] [node-mocks-http] 218/296: feat(node): IncomingMessage() mock

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:38 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-mocks-http.

commit 2f76ccd87403cda094694ea64e0084e244116a2d
Author: Johnny Estilles <johnny.estilles at agentia.asia>
Date:   Thu May 7 20:25:04 2015 +0800

    feat(node): IncomingMessage() mock
---
 lib/node/_http_incoming.js | 127 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 127 insertions(+)

diff --git a/lib/node/_http_incoming.js b/lib/node/_http_incoming.js
new file mode 100644
index 0000000..3c84858
--- /dev/null
+++ b/lib/node/_http_incoming.js
@@ -0,0 +1,127 @@
+'use strict';
+
+var util = require('util');
+var Stream = require('stream');
+
+function readStart() {}
+exports.readStart = readStart;
+
+function readStop() {}
+exports.readStop = readStop;
+
+
+function IncomingMessage() {
+  Stream.Readable.call(this);
+
+  this.httpVersionMajor = null;
+  this.httpVersionMinor = null;
+  this.httpVersion = null;
+  this.complete = false;
+  this.headers = {};
+  this.rawHeaders = [];
+  this.trailers = {};
+  this.rawTrailers = [];
+
+  this.readable = true;
+
+  this._pendings = [];
+  this._pendingIndex = 0;
+  this.upgrade = null;
+
+  // request (server) only
+  this.url = '';
+  this.method = null;
+
+  // response (client) only
+  this.statusCode = null;
+  this.statusMessage = null;
+
+  // flag for backwards compatibility grossness.
+  this._consuming = false;
+
+  // flag for when we decide that this message cannot possibly be
+  // read by the user, so there's no point continuing to handle it.
+  this._dumped = false;
+}
+util.inherits(IncomingMessage, Stream.Readable);
+
+exports.IncomingMessage = IncomingMessage;
+
+IncomingMessage.prototype.read = function() {};
+IncomingMessage.prototype._read = function() {};
+IncomingMessage.prototype.destroy = function() {};
+
+IncomingMessage.prototype.setTimeout = function(msecs, callback) {
+  if (callback) {
+    setTimeout(callback, msecs);
+  }
+};
+
+IncomingMessage.prototype._addHeaderLines = function(headers, n) {
+  if (headers && headers.length) {
+    var raw, dest;
+    if (this.complete) {
+      raw = this.rawTrailers;
+      dest = this.trailers;
+    } else {
+      raw = this.rawHeaders;
+      dest = this.headers;
+    }
+
+    for (var i = 0; i < n; i += 2) {
+      var k = headers[i];
+      var v = headers[i + 1];
+      raw.push(k);
+      raw.push(v);
+      this._addHeaderLine(k, v, dest);
+    }
+  }
+};
+
+IncomingMessage.prototype._addHeaderLine = function(field, value, dest) {
+  field = field.toLowerCase();
+  switch (field) {
+    // Array headers:
+    case 'set-cookie':
+      if (!util.isUndefined(dest[field])) {
+        dest[field].push(value);
+      } else {
+        dest[field] = [value];
+      }
+      break;
+
+    // list is taken from:
+    // https://mxr.mozilla.org/mozilla/source/netwerk/protocol/http/src/nsHttpHeaderArray.cpp
+    case 'content-type':
+    case 'content-length':
+    case 'user-agent':
+    case 'referer':
+    case 'host':
+    case 'authorization':
+    case 'proxy-authorization':
+    case 'if-modified-since':
+    case 'if-unmodified-since':
+    case 'from':
+    case 'location':
+    case 'max-forwards':
+      // drop duplicates
+      if (util.isUndefined(dest[field])) {
+        dest[field] = value;
+      }
+      break;
+
+    default:
+      // make comma-separated list
+      if (!util.isUndefined(dest[field])) {
+        dest[field] += ', ' + value;
+      } else {
+        dest[field] = value;
+      }
+  }
+};
+
+IncomingMessage.prototype._dump = function() {
+  if (!this._dumped) {
+    this._dumped = true;
+  }
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-mocks-http.git



More information about the Pkg-javascript-commits mailing list