Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mockserver

Package Overview
Dependencies
Maintainers
2
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockserver - npm Package Compare versions

Comparing version 1.4.0 to 1.5.0

24

mockserver.js

@@ -26,2 +26,14 @@ var fs = require('fs');

/**
* Prepares headers to watch, no duplicates, non-blanks.
* Priority exports over ENV definition.
*/
var prepareWatchedHeaders = function () {
var headers = (module.exports.headers.toString() || process.env.MOCK_HEADERS || '').split(',');
return headers.filter(function(item, pos, self) {
return item && self.indexOf(item) == pos;
});
}
/**
* Parser the content of a mockfile

@@ -128,5 +140,7 @@ * returning an HTTP-ish object with

verbose: false,
headers: [],
init: function(directory, verbose) {
this.directory = directory;
this.verbose = !!verbose;
this.headers = prepareWatchedHeaders();
},

@@ -147,8 +161,4 @@ handle: function(req, res) {

var watchedHeaders = module.exports.headers;
if(watchedHeaders && !Array.isArray(watchedHeaders)) {
watchedHeaders = [watchedHeaders];
}
if(req.headers && watchedHeaders && watchedHeaders.length) {
watchedHeaders.forEach(function(header) {
if(req.headers && mockserver.headers.length) {
mockserver.headers.forEach(function(header) {
header = header.toLowerCase();

@@ -195,2 +205,2 @@ if(req.headers[header]) {

module.exports.headers = [];
module.exports.headers = null;
{
"name": "mockserver",
"version": "1.4.0",
"version": "1.5.0",
"description": "Easily mock your webservices while testing frontends.",

@@ -5,0 +5,0 @@ "main": "mockserver.js",

@@ -109,5 +109,13 @@ # mockserver

You can specify request headers to include, which allows you to change the response based on what headers are
provided. To do this, you need to let mockserver know which headers matter, by setting the
`headers` array on the mockserver object, like so:
provided.
To do this, you need to let mockserver know which headers matter,
by exposing comma-separated environment `MOCK_HEADERS` variable, like so:
```shell
$ MOCK_HEADERS=x-foo,authorization mockserver -m . -p 9001
```
Or by setting the `headers` array on the mockserver object, like so:
```js

@@ -114,0 +122,0 @@ var mockserver = require('mockserver');

@@ -12,2 +12,35 @@ var MockReq = require('mock-req');

/**
* Processes request
*/
function processRequest(url, method) {
req.url = url;
req.method = method;
mockserver(mocksDirectory, verbose)(req, res);
}
/**
* Processes request within custom ENV
*/
function processRequestEnv(url, method, envs) {
var cleanupEnv = function() {};
for (var name in envs) {
if (envs.hasOwnProperty(name)) {
process.env[name] = envs[name];
cleanupEnv = (function (name, next) {
return function () {
delete process.env[name];
next();
}
})(name, cleanupEnv);
}
}
processRequest(url, method);
cleanupEnv();
}
describe('mockserver', function() {

@@ -42,12 +75,6 @@ beforeEach(function() {

function process(url, method) {
req.url = url;
req.method = method;
mockserver(mocksDirectory, verbose)(req, res);
}
describe('mockserver()', function() {
it('should return a valid response', function() {
process('/test', 'GET');
processRequest('/test', 'GET');

@@ -60,3 +87,3 @@ assert.equal(res.body, 'Welcome!');

it('should return 404 if the mock does not exist', function() {
process('/not-there', 'GET');
processRequest('/not-there', 'GET');

@@ -68,3 +95,3 @@ assert.equal(res.status, 404);

it('should be able to handle trailing slashes without changing the name of the mockfile', function() {
process('/test/', 'GET');
processRequest('/test/', 'GET');

@@ -77,3 +104,3 @@ assert.equal(res.status, 200);

it('should be able to handle multiple headers', function() {
process('/multiple-headers/', 'GET');
processRequest('/multiple-headers/', 'GET');

@@ -86,3 +113,3 @@ assert.equal(res.status, 200);

it('should be able to handle status codes different than 200', function() {
process('/return-204', 'GET');
processRequest('/return-204', 'GET');

@@ -93,3 +120,3 @@ assert.equal(res.status, 204);

it('should be able to handle HTTP methods other than GET', function() {
process('/return-200', 'POST');
processRequest('/return-200', 'POST');

@@ -100,3 +127,3 @@ assert.equal(res.status, 200);

it('should be able to handle empty bodies', function() {
process('/return-empty-body', 'GET');
processRequest('/return-empty-body', 'GET');

@@ -108,3 +135,3 @@ assert.equal(res.status, 204);

it('should be able to correctly map /', function() {
process('/', 'GET');
processRequest('/', 'GET');

@@ -115,3 +142,3 @@ assert.equal(res.body, 'homepage');

it('should be able to map multi-level urls', function() {
process('/test1/test2', 'GET');
processRequest('/test1/test2', 'GET');

@@ -122,3 +149,3 @@ assert.equal(res.body, 'multi-level url');

it('should be able to handle GET parameters', function() {
process('/test?a=b', 'GET');
processRequest('/test?a=b', 'GET');

@@ -129,3 +156,3 @@ assert.equal(res.status, 200);

it('should default to GET.mock if no matching parameter file is found', function() {
process('/test?a=c', 'GET');
processRequest('/test?a=c', 'GET');

@@ -138,3 +165,3 @@ assert.equal(res.status, 200);

process('/request-headers', 'GET');
processRequest('/request-headers', 'GET');
assert.equal(res.status, 401);

@@ -144,3 +171,3 @@ assert.equal(res.body, 'not authorized');

req.headers['authorization'] = '1234';
process('/request-headers', 'GET');
processRequest('/request-headers', 'GET');
assert.equal(res.status, 200);

@@ -150,3 +177,3 @@ assert.equal(res.body, 'authorized');

req.headers['authorization'] = '5678';
process('/request-headers', 'GET');
processRequest('/request-headers', 'GET');
assert.equal(res.status, 200);

@@ -160,3 +187,3 @@ assert.equal(res.body, 'admin authorized');

req.headers['authorization'] = 'invalid';
process('/request-headers', 'GET');
processRequest('/request-headers', 'GET');
assert.equal(res.status, 401);

@@ -166,3 +193,3 @@ assert.equal(res.body, 'not authorized');

req.headers['authorization'] = 'invalid';
process('/request-headers', 'POST');
processRequest('/request-headers', 'POST');
assert.equal(res.status, 404);

@@ -177,3 +204,3 @@ assert.equal(res.body, 'Not Mocked');

req.headers['x-foo'] = 'Bar';
process('/request-headers', 'PUT');
processRequest('/request-headers', 'PUT');
assert.equal(res.status, 200);

@@ -183,3 +210,3 @@ assert.equal(res.body, 'header both');

req.headers['x-foo'] = 'Baz';
process('/request-headers', 'PUT');
processRequest('/request-headers', 'PUT');
assert.equal(res.status, 200);

@@ -189,3 +216,3 @@ assert.equal(res.body, 'header auth only');

req.headers['authorization'] = 78;
process('/request-headers', 'PUT');
processRequest('/request-headers', 'PUT');
assert.equal(res.status, 200);

@@ -195,3 +222,3 @@ assert.equal(res.body, 'header both out-of-order');

req.headers['authorization'] = 45;
process('/request-headers', 'PUT');
processRequest('/request-headers', 'PUT');
assert.equal(res.status, 200);

@@ -201,3 +228,3 @@ assert.equal(res.body, 'header x-foo only');

delete req.headers['authorization'];
process('/request-headers', 'PUT');
processRequest('/request-headers', 'PUT');
assert.equal(res.status, 200);

@@ -211,3 +238,3 @@ assert.equal(res.body, 'header x-foo only');

req.headers['x-foo'] = 'Bar';
process('/request-headers?a=b', 'POST');
processRequest('/request-headers?a=b', 'POST');
assert.equal(res.status, 200);

@@ -217,4 +244,28 @@ assert.equal(res.body, 'that is a long filename');

it('should be able track custom string headers with variation and query params', function() {
mockserver.headers = 'authorization,x-foo';
req.headers['authorization'] = 12;
req.headers['x-foo'] = 'Bar';
processRequest('/request-headers?a=b', 'POST');
assert.equal(res.status, 200);
assert.equal(res.body, 'that is a long filename');
});
it('should be able track custom ENV headers with variation and query params', function() {
req.headers['authorization'] = 12;
req.headers['x-foo'] = 'Bar';
processRequestEnv('/request-headers?a=b', 'POST', {
MOCK_HEADERS: 'authorization,x-foo',
});
assert.equal(res.status, 200);
assert.equal(res.body, 'that is a long filename');
});
it('should keep line feeds (U+000A)', function() {
process('/keep-line-feeds', 'GET');
processRequest('/keep-line-feeds', 'GET');

@@ -241,5 +292,5 @@ assert.equal(res.body,

req.end();
mockserver(mocksDirectory)(req, res);
mockserver(mocksDirectory, verbose)(req, res);
req.on('end', function() {

@@ -262,5 +313,5 @@ assert.equal(res.body, 'Hella');

req.end();
mockserver(mocksDirectory)(req, res);
mockserver(mocksDirectory, verbose)(req, res);
req.on('end', function() {

@@ -285,5 +336,5 @@ var jsonBody = JSON.parse(res.body);

req.end();
mockserver(mocksDirectory)(req, res);
mockserver(mocksDirectory, verbose)(req, res);
req.on('end', function() {

@@ -298,3 +349,3 @@ assert.equal(res.status, 200);

req.headers['authorization'] = 12;
process('/return-200?a=c', 'GET');
processRequest('/return-200?a=c', 'GET');

@@ -301,0 +352,0 @@ assert.equal(res.status, 404);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc