[Pkg-javascript-commits] [node-mocks-http] 283/296: Add support for .vary() response method

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:45 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 dda0ef2bf52c1c330a0473f2edf7f0f8fe5a50dd
Author: Matt Hinchliffe <matt at maketea.co.uk>
Date:   Thu Nov 19 16:41:55 2015 +0000

    Add support for .vary() response method
---
 lib/mockResponse.js           | 25 +++++++++++++++++++++++++
 test/lib/mockResponse.spec.js | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 4e012c1..6afa763 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -356,6 +356,31 @@ function createResponse(options) {
 
     };
 
+    /**
+     * Function: vary
+     *
+     *   Adds the field/s to the Vary response header
+     *
+     * Examples:
+     *
+     *    res.vary('A-B-Test');
+     *    res.vary(['A-B-Test', 'Known-User']);
+     */
+    mockResponse.vary = function(fields) {
+        var header = mockResponse.getHeader('Vary') || '';
+        var values = header.length ? header.split(', ') : [];
+
+        fields = Array.isArray(fields) ? fields : [ fields ];
+
+        fields = fields.filter(function(field) {
+            return values.indexOf(field) === -1;
+        });
+
+        values = values.concat(fields);
+
+        return mockResponse.setHeader('Vary', values.join(', '));
+    };
+
    /**
     * Set header `field` to `val`, or pass
     * an object of header fields.
diff --git a/test/lib/mockResponse.spec.js b/test/lib/mockResponse.spec.js
index 2d32a83..0bd3123 100644
--- a/test/lib/mockResponse.spec.js
+++ b/test/lib/mockResponse.spec.js
@@ -297,6 +297,39 @@ describe('mockResponse', function() {
 
     });
 
+    describe('.vary()', function() {
+      var response;
+
+      beforeEach(function() {
+        response = mockResponse.createResponse();
+        sinon.spy(response, 'setHeader');
+      });
+
+      afterEach(function() {
+        response.setHeader.restore();
+        response = null;
+      });
+
+      it('should set vary header, when called with a single field', function() {
+        response.vary('value1');
+        expect(response.setHeader).to.have.been.calledWith('Vary', 'value1');
+        expect(response.get('Vary')).to.equal('value1');
+      });
+
+      it('should set vary header, when called with a an array of fields', function() {
+        response.vary([ 'value1', 'value2' ]);
+        expect(response.setHeader).to.have.been.calledWith('Vary', 'value1, value2');
+        expect(response.get('Vary')).to.equal('value1, value2');
+      });
+
+      it('should not duplicate vary header values', function() {
+        response.vary([ 'value1', 'value2' ]);
+        response.vary([ 'value1', 'value3' ]);
+        expect(response.get('Vary')).to.equal('value1, value2, value3');
+      });
+
+    });
+
     describe('.set()/.header()', function() {
       var response;
 

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