[Pkg-javascript-commits] [node-mocks-http] 122/296: Issue #50 - implementing referrer header edge-case - making mockRequest.header() an alias of .get(), mimicking Express' implementation - ensure header names are stored in lowercase - added tests for mockRequest.get(), .header() and referer/referrer edge case

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:28 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 984afca7562caa0cedd1ffe99492e5b2bc2ee1b7
Author: Johnny Estilles <johnny.estilles at agentia.asia>
Date:   Thu Mar 19 18:20:53 2015 +0800

    Issue #50
    - implementing referrer header edge-case
    - making mockRequest.header() an alias of .get(), mimicking Express' implementation
    - ensure header names are stored in lowercase
    - added tests for mockRequest.get(), .header() and referer/referrer edge case
---
 lib/mockRequest.js       | 43 +++++++++++++++++++++++++++++++------------
 test/test-mockRequest.js | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 12 deletions(-)

diff --git a/lib/mockRequest.js b/lib/mockRequest.js
index 229f9d2..bae87e0 100644
--- a/lib/mockRequest.js
+++ b/lib/mockRequest.js
@@ -57,21 +57,39 @@ function createRequest(options) {
     }
 
     /**
-     * Function: header
+     * Return request header.
      *
-     *   Returns a particular header by name.
-     */
-    mockRequest.header = function(name) {
-        return mockRequest.headers[name];
-    };
-
-    /**
-     * Function: get
+     * The `Referrer` header field is special-cased,
+     * both `Referrer` and `Referer` are interchangeable.
+     *
+     * Examples:
+     *
+     *     mockRequest.get('Content-Type');
+     *     // => "text/plain"
      *
-     *   An copy of header.
+     *     mockRequest.get('content-type');
+     *     // => "text/plain"
+     *
+     *     mockRequest.get('Something');
+     *     // => undefined
+     *
+     * Aliased as `mockRequest.header()`.
+     *
+     * @param {String} name
+     * @return {String}
+     * @api public
      */
-    mockRequest.get = function(name) {
-        return mockRequest.headers[name];
+
+    mockRequest.get =
+    mockRequest.header = function(name) {
+        name = name.toLowerCase();
+        switch (name) {
+            case 'referer':
+            case 'referrer':
+                return mockRequest.headers.referrer || mockRequest.headers.referer;
+            default:
+                return mockRequest.headers[name];
+        }
     };
 
     /**
@@ -140,6 +158,7 @@ function createRequest(options) {
      * @param value    The value associated with the variable
      */
     mockRequest._setHeadersVariable = function(variable, value) {
+        variable = variable.toLowerCase();
         mockRequest.headers[variable] = value;
     };
 
diff --git a/test/test-mockRequest.js b/test/test-mockRequest.js
index e08a878..ab51c85 100644
--- a/test/test-mockRequest.js
+++ b/test/test-mockRequest.js
@@ -47,6 +47,50 @@ exports['url - Setting a POST'] = function(test) {
     test.done();
 };
 
+exports['get/header - Setting a header using options'] = function(test) {
+    var name = 'accept';
+    var value = 'text/plain';
+    var options = { headers: {} };
+    options.headers[name] = value;
+    var request = httpMocks.createRequest(options);
+    test.equal(request.get(name), value);
+    test.done();
+};
+
+exports['get/header - Setting a header using ._setHeadersVariable()'] = function(test) {
+    var request = httpMocks.createRequest();
+    var name = 'accept';
+    var value = 'text/plain';
+    request._setHeadersVariable(name, value);
+    test.equal(request.get(name), value);
+    test.equal(request.header(name), value);
+    test.done();
+};
+
+exports['get/header - Setting header "referer" edge-case'] = function(test) {
+    var request = httpMocks.createRequest();
+    var name = 'referer';
+    var value = 'http://localhost:5732/blah';
+    request._setHeadersVariable(name, value);
+    test.equal(request.get('referer'), value);
+    test.equal(request.get('referrer'), value);
+    test.equal(request.header('referer'), value);
+    test.equal(request.header('referrer'), value);
+    test.done();
+};
+
+exports['get/header - Setting header "referrer" edge-case'] = function(test) {
+    var request = httpMocks.createRequest();
+    var name = 'referrer';
+    var value = 'http://localhost:5732/blah';
+    request._setHeadersVariable(name, value);
+    test.equal(request.get('referer'), value);
+    test.equal(request.get('referrer'), value);
+    test.equal(request.header('referer'), value);
+    test.equal(request.header('referrer'), value);
+    test.done();
+};
+
 exports['addBody - Simple verification'] = function(test) {
     var request = httpMocks.createRequest();
 

-- 
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