openrosa-request-middleware
Advanced tools
Comparing version 1.0.1 to 1.1.0
41
index.js
@@ -1,26 +0,35 @@ | ||
'use strict'; | ||
'use strict' | ||
// These headers are required according to https://bitbucket.org/javarosa/javarosa/wiki/OpenRosaRequest | ||
var OpenRosaHeaders = { | ||
"X-OpenRosa-Version": "1.0" | ||
}; | ||
'X-OpenRosa-Version': '1.0' | ||
} | ||
module.exports = function(options) { | ||
return function(req, res, next) { | ||
var err, prop; | ||
/** | ||
* Creates middleware to add OpenRosa headers to any response, and only accept requests from clients sending the correct headers, as per the [OpenRosaRequest spec](https://bitbucket.org/javarosa/javarosa/wiki/OpenRosaRequest) | ||
* @param {object} [options] | ||
* @param {boolean} [options.strict=true] If true, only respond to requests with the correct OpenRosa headers. If false, does not care about request headers. Default=true | ||
* @return {[type]} [description] | ||
*/ | ||
module.exports = function (options) { | ||
options = options || {} | ||
if (options.strict !== false) options.strict = true | ||
return function (req, res, next) { | ||
var err, prop | ||
for (var header in OpenRosaHeaders) { | ||
res.setHeader(header, OpenRosaHeaders[header]); | ||
res.setHeader(header, OpenRosaHeaders[header]) | ||
} | ||
for (prop in OpenRosaHeaders) { | ||
if (req.headers[prop.toLowerCase()] !== OpenRosaHeaders[prop]) { | ||
err = new Error('Request missing required header "' + prop + "'"); | ||
err.status = 400; | ||
return next(err); | ||
} | ||
if (options.strict && req.headers[prop.toLowerCase()] !== OpenRosaHeaders[prop]) { | ||
err = new Error('Request missing required header "' + prop + "'") | ||
err.status = 400 | ||
return next(err) | ||
} | ||
} | ||
next(); | ||
}; | ||
}; | ||
next() | ||
} | ||
} |
{ | ||
"name": "openrosa-request-middleware", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Basic middleware for serving OpenRosa compliant responses for express middleware", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js | node_modules/.bin/tap-spec" | ||
"test": "standard && node test.js | node_modules/.bin/tap-spec" | ||
}, | ||
@@ -20,2 +20,3 @@ "repository": { | ||
"node-mocks-http": "^1.2.4", | ||
"standard": "5.4.1", | ||
"tap-spec": "^2.1.2", | ||
@@ -22,0 +23,0 @@ "tape": "^3.0.3" |
@@ -15,1 +15,3 @@ OpenRosa Request Middleware | ||
Note that it exports a factory that creates the middleware when invoked. | ||
pass `options = {strict: false}` to ignore request headers |
68
test.js
@@ -1,42 +0,52 @@ | ||
var test = require('tape'), | ||
httpMocks = require('node-mocks-http'); | ||
var test = require('tape') | ||
var httpMocks = require('node-mocks-http') | ||
var middleware = require('./')(); | ||
var middleware = require('./')() | ||
test('Middleware accepts requests with X-OpenRosa-Version header', function(t) { | ||
t.plan(1); | ||
test('Middleware accepts requests with X-OpenRosa-Version header', function (t) { | ||
t.plan(1) | ||
var req = httpMocks.createRequest(), | ||
res = httpMocks.createResponse(); | ||
var req = httpMocks.createRequest() | ||
var res = httpMocks.createResponse() | ||
req.headers['x-openrosa-version'] = "1.0"; | ||
req.headers['x-openrosa-version'] = '1.0' | ||
middleware(req, res, function(err) { | ||
t.error(err, 'should not throw error'); | ||
}); | ||
middleware(req, res, function (err) { | ||
t.error(err, 'should not throw error') | ||
}) | ||
}) | ||
}); | ||
test('Middleware rejects requests without X-OpenRosa-Version header', function (t) { | ||
t.plan(2) | ||
test('Middleware rejects requests without X-OpenRosa-Version header', function(t) { | ||
t.plan(2); | ||
var req = httpMocks.createRequest() | ||
var res = httpMocks.createResponse() | ||
var req = httpMocks.createRequest(), | ||
res = httpMocks.createResponse(); | ||
middleware(req, res, function (err) { | ||
t.ok(err, 'got an error') | ||
t.equal(err.status, 400, 'correct error status code') | ||
}) | ||
}) | ||
middleware(req, res, function(err) { | ||
t.ok(err, 'got an error'); | ||
t.equal(err.status, 400, 'correct error status code'); | ||
}); | ||
test('Middleware accepts requests without X-OpenRosa-Version header if options.strict === false', function (t) { | ||
t.plan(1) | ||
}); | ||
var unstrictMiddleware = require('./')({strict: false}) | ||
var req = httpMocks.createRequest() | ||
var res = httpMocks.createResponse() | ||
test('Middleware adds X-OpenRosa-Version header to response', function(t) { | ||
t.plan(1); | ||
unstrictMiddleware(req, res, function (err) { | ||
t.error(err, 'should not throw error') | ||
}) | ||
}) | ||
var req = httpMocks.createRequest(), | ||
res = httpMocks.createResponse(); | ||
test('Middleware adds X-OpenRosa-Version header to response', function (t) { | ||
t.plan(1) | ||
middleware(req, res, function(err) { | ||
t.equal(res.get('X-OpenRosa-Version'), '1.0', 'correct header on response'); | ||
}); | ||
}); | ||
var req = httpMocks.createRequest() | ||
var res = httpMocks.createResponse() | ||
middleware(req, res, function () { | ||
t.equal(res.get('X-OpenRosa-Version'), '1.0', 'correct header on response') | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4533
6
67
17
4