[Pkg-javascript-commits] [node-mocks-http] 251/296: Adding support for mockRequest.is() method for content-type inspection
Thorsten Alteholz
alteholz at moszumanska.debian.org
Mon Feb 8 18:13:42 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 336ea0c68b88cbd2af4f621db5c65e4ae6c38135
Author: James Wing <jvwing at gmail.com>
Date: Thu Jul 16 10:48:14 2015 -0700
Adding support for mockRequest.is() method for content-type inspection
---
lib/mockRequest.js | 33 ++++++++++++++++++++++++++++++++
package.json | 3 ++-
test/lib/mockRequest.spec.js | 45 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 80 insertions(+), 1 deletion(-)
diff --git a/lib/mockRequest.js b/lib/mockRequest.js
index 8a93677..56c920e 100644
--- a/lib/mockRequest.js
+++ b/lib/mockRequest.js
@@ -30,6 +30,7 @@
*/
var url = require('url');
+var typeis = require('type-is');
function convertKeysToLowerCase(map) {
var newMap = {};
@@ -108,6 +109,38 @@ function createRequest(options) {
};
/**
+ * Function: is
+ *
+ * Checks for matching content types in the content-type header.
+ * Requires a request body, identified by transfer-encoding or content-length headers
+ *
+ * Examples:
+ *
+ * mockRequest.headers['content-type'] = 'text/html';
+ * mockRequest.headers['transfer-encoding'] = 'chunked';
+ * mockRequest.headers['content-length'] = '100';
+ *
+ * mockRequest.is('html');
+ * // => "html"
+ *
+ * mockRequest.is('json');
+ * // => false
+ *
+ * mockRequest.is(['json', 'html', 'text']);
+ * // => "html"
+ *
+ * @param {String|String[]} types content type or array of types to match
+ * @return {String|false|null} Matching content type as string, false if no match, null if request has no body.
+ * @api public
+ */
+ mockRequest.is = function(types) {
+ if (!Array.isArray(types)) {
+ types = [].slice.call(arguments);
+ }
+ return typeis(mockRequest, types);
+ };
+
+ /**
* Function: param
*
* Return the value of param name when present.
diff --git a/package.json b/package.json
index d0e2d8e..cb3433b 100644
--- a/package.json
+++ b/package.json
@@ -40,7 +40,8 @@
"node": ">=0.6"
},
"dependencies": {
- "mime": "^1.3.4"
+ "mime": "^1.3.4",
+ "type-is": "^1.6.4"
},
"devDependencies": {
"chai": "^2.2.0",
diff --git a/test/lib/mockRequest.spec.js b/test/lib/mockRequest.spec.js
index a08c37d..6ded151 100644
--- a/test/lib/mockRequest.spec.js
+++ b/test/lib/mockRequest.spec.js
@@ -272,6 +272,51 @@ describe('mockRequest', function() {
});
+ describe('.is()', function() {
+ var request;
+
+ afterEach(function() {
+ request = null;
+ });
+
+ it('should return type, when found in content-type header', function() {
+ var options = {
+ headers: {
+ 'content-type': 'text/html',
+ 'transfer-encoding': 'chunked'
+ }
+ };
+
+ request = mockRequest.createRequest(options);
+ expect(request.is('html')).to.equal('html');
+ });
+
+ it('should return first matching type, given array of types', function() {
+ var options = {
+ headers: {
+ 'content-type': 'text/html',
+ 'transfer-encoding': 'chunked'
+ }
+ };
+
+ request = mockRequest.createRequest(options);
+ expect(request.is(['json', 'html', 'text'])).to.equal('html');
+ });
+
+ it('should return false when type not found', function() {
+ var options = {
+ headers: {
+ 'content-type': 'text/html',
+ 'transfer-encoding': 'chunked'
+ }
+ };
+
+ request = mockRequest.createRequest(options);
+ expect(request.is(['json'])).to.equal(false);
+ });
+
+ });
+
describe('.param()', function() {
var request;
--
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