[Pkg-javascript-commits] [node-mocks-http] 60/296: Fix json method to not override the status code if this one is already set.
Thorsten Alteholz
alteholz at moszumanska.debian.org
Mon Feb 8 18:13:21 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 bcd00089fb3c59f2f3274feb30280e4e9de4573f
Author: Sebastien Guimont <sebastieng at sympatico.ca>
Date: Mon Jan 5 11:01:45 2015 -0500
Fix json method to not override the status code if this one is already set.
---
examples/express-status-vs-json.js | 58 ++++++++++++++++++++++++++++++++++++++
lib/mockResponse.js | 5 +++-
2 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/examples/express-status-vs-json.js b/examples/express-status-vs-json.js
new file mode 100644
index 0000000..1521d61
--- /dev/null
+++ b/examples/express-status-vs-json.js
@@ -0,0 +1,58 @@
+'use strict';
+
+var httpMocks = require('../lib/http-mock');
+
+// Suppose you have the following Express route:
+
+// app.get('/user/:id', routeHandler);
+
+// And you have created a function to handle that route's call:
+
+var routeHandler = function(request, response) {
+
+ var id = request.params.id;
+
+ console.log('We have a \'%s\' request for %s (ID: %d)', request.method, request.url, id);
+
+ var body = {
+ name: 'Bob Dog',
+ age: 42,
+ email: 'bob at dog.com'
+ };
+
+ response.status(201).json(body);
+ response.end();
+};
+
+// -----------------------------------------------------------------
+// In another file, you can easily test the routeHandler function
+// with some code like this using the testing framework of your choice:
+
+exports['routeHandler - Simple testing'] = function(test) {
+
+ var request = httpMocks.createRequest({
+ method: 'GET',
+ url: '/user/42',
+ params: {
+ id: 42
+ }
+ });
+
+ var response = httpMocks.createResponse();
+
+ routeHandler(request, response);
+
+ var data = JSON.parse(response._getData());
+
+ test.equal('Bob Dog', data.name);
+ test.equal(42, data.age);
+ test.equal('bob at dog.com', data.email);
+
+ test.equal(201, response.statusCode);
+ test.ok(response._isEndCalled());
+ test.ok(response._isJSON());
+ test.ok(response._isUTF8());
+
+ test.done();
+
+};
\ No newline at end of file
diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index 6b3b727..8b668ad 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -194,7 +194,10 @@ function createResponse(options) {
mockResponse.json = function(a, b) {
mockResponse.setHeader('Content-Type', 'application/json');
- mockResponse.statusCode = 200;
+ // Sets default status code if none has been specify.
+ if(mockResponse.statusCode === -1) {
+ mockResponse.statusCode = 200;
+ }
if (a) {
if (typeof a === 'number') {
--
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