[Pkg-javascript-commits] [node-mocks-http] 270/296: added jsonp method

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:44 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 4ef96e2a0837907c5d8d31f0b940ac1b7a99530a
Author: Abe <abe at abesmith.co.uk>
Date:   Thu Oct 8 22:20:48 2015 +0300

    added jsonp method
---
 lib/mockResponse.js           | 38 ++++++++++++++++++++++++++++++++++++++
 test/lib/mockResponse.spec.js | 25 +++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index bc49230..080bf35 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -249,6 +249,44 @@ function createResponse(options) {
     };
 
     /**
+     * Function: jsonp
+     *
+     *   The 'jsonp' function from node's HTTP API that returns JSON
+     *   data to the client.
+     *
+     *  Parameters:
+     *
+     *   a - Either a statusCode or string containing JSON payload
+     *   b - Either a statusCode or string containing JSON payload
+     *
+     *  If not specified, the statusCode defaults to 200.
+     *  Second parameter is optional.
+     */
+    mockResponse.jsonp = function(a, b) {
+
+        mockResponse.setHeader('Content-Type', 'text/javascript');
+
+        if (a) {
+            if (typeof a === 'number') {
+                mockResponse.statusCode = a;
+            } else {
+                _data += JSON.stringify(a);
+            }
+        }
+        if (b) {
+            if (typeof b === 'number') {
+                mockResponse.statusCode = b;
+            } else {
+                _data += JSON.stringify(b);
+            }
+        }
+
+        mockResponse.emit('send');
+        mockResponse.emit('end');
+
+    };
+
+    /**
      * Set "Content-Type" response header with `type` through `mime.lookup()`
      * when it does not contain "/", or set the Content-Type to `type` otherwise.
      *
diff --git a/test/lib/mockResponse.spec.js b/test/lib/mockResponse.spec.js
index 46580f1..2d32a83 100644
--- a/test/lib/mockResponse.spec.js
+++ b/test/lib/mockResponse.spec.js
@@ -491,6 +491,31 @@ describe('mockResponse', function() {
 
     });
 
+    // TODO: fix in 2.0; method should mimic Express Response.jsonp()
+    describe('.jsonp()', function() {
+      var response;
+
+      beforeEach(function() {
+        response = mockResponse.createResponse();
+        sinon.spy(response, 'emit');
+      });
+
+      afterEach(function() {
+        response.emit.restore();
+        response = null;
+      });
+
+      it('method should mimic Express Response.jsonp()');
+
+      it('should emit send and end events', function() {
+        response.jsonp({});
+        expect(response.emit).to.have.been.calledTwice;
+        expect(response.emit).to.have.been.calledWith('send');
+        expect(response.emit).to.have.been.calledWith('end');
+      });
+
+    });
+
     // TODO: fix in 2.0; method should mimic Express Response.redirect()
     describe('.redirect()', function() {
 

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