Socket
Socket
Sign inDemoInstall

fetch-mock

Package Overview
Dependencies
Maintainers
1
Versions
226
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-mock - npm Package Compare versions

Comparing version 2.2.10 to 2.2.11

4

es5/client.js

@@ -7,3 +7,3 @@ 'use strict';

const FetchMock = require('./fetch-mock');
var FetchMock = require('./fetch-mock');

@@ -15,3 +15,3 @@ module.exports = new FetchMock({

Blob: window.Blob,
debug: function () {}
debug: function debug() {}
});
'use strict';
let Headers;
let Response;
let stream;
let Blob;
let theGlobal;
let debug;
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
var Headers = undefined;
var Response = undefined;
var stream = undefined;
var Blob = undefined;
var theGlobal = undefined;
var debug = undefined;
function mockResponse(url, config) {

@@ -32,3 +38,3 @@ debug('mocking response for ' + url);

const opts = config.opts || {};
var opts = config.opts || {};
opts.url = url;

@@ -40,5 +46,5 @@ opts.status = config.status || 200;

let body = config.body;
var body = config.body;
if (config.body != null && typeof body === 'object') {
if (config.body != null && (typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') {
body = JSON.stringify(body);

@@ -50,3 +56,3 @@ }

if (stream) {
let s = new stream.Readable();
var s = new stream.Readable();
if (body != null) {

@@ -93,21 +99,25 @@ s.push(body, 'utf-8');

if (typeof route.matcher === 'string') {
let expectedUrl = route.matcher;
if (route.matcher.indexOf('^') === 0) {
debug('constructing starts with string matcher for route: ' + route.name);
expectedUrl = expectedUrl.substr(1);
(function () {
var expectedUrl = route.matcher;
if (route.matcher.indexOf('^') === 0) {
debug('constructing starts with string matcher for route: ' + route.name);
expectedUrl = expectedUrl.substr(1);
route.matcher = function (url, options) {
return matchMethod(options) && url.indexOf(expectedUrl) === 0;
};
} else {
debug('constructing string matcher for route: ' + route.name);
route.matcher = function (url, options) {
return matchMethod(options) && url === expectedUrl;
};
}
})();
} else if (route.matcher instanceof RegExp) {
(function () {
debug('constructing regex matcher for route: ' + route.name);
var urlRX = route.matcher;
route.matcher = function (url, options) {
return matchMethod(options) && url.indexOf(expectedUrl) === 0;
return matchMethod(options) && urlRX.test(url);
};
} else {
debug('constructing string matcher for route: ' + route.name);
route.matcher = function (url, options) {
return matchMethod(options) && url === expectedUrl;
};
}
} else if (route.matcher instanceof RegExp) {
debug('constructing regex matcher for route: ' + route.name);
const urlRX = route.matcher;
route.matcher = function (url, options) {
return matchMethod(options) && urlRX.test(url);
};
})();
}

@@ -117,4 +127,6 @@ return route;

class FetchMock {
constructor(opts) {
var FetchMock = (function () {
function FetchMock(opts) {
_classCallCheck(this, FetchMock);
Headers = opts.Headers;

@@ -132,225 +144,249 @@ Response = opts.Response;

useNonGlobalFetch(func) {
this.mockedContext = this;
this.realFetch = func;
}
registerRoute(name, matcher, response) {
debug('registering routes');
let routes;
if (name instanceof Array) {
routes = name;
} else if (arguments.length === 3) {
routes = [{
name,
matcher,
response
}];
} else {
routes = [name];
_createClass(FetchMock, [{
key: 'useNonGlobalFetch',
value: function useNonGlobalFetch(func) {
this.mockedContext = this;
this.realFetch = func;
}
}, {
key: 'registerRoute',
value: function registerRoute(name, matcher, response) {
debug('registering routes');
var routes = undefined;
if (name instanceof Array) {
routes = name;
} else if (arguments.length === 3) {
routes = [{
name: name,
matcher: matcher,
response: response
}];
} else {
routes = [name];
}
debug('registering routes: ' + routes.map(r => r.name));
debug('registering routes: ' + routes.map(function (r) {
return r.name;
}));
this.routes = this.routes.concat(routes.map(compileRoute));
}
unregisterRoute(names) {
if (!names) {
debug('unregistering all routes');
this.routes = [];
return;
this.routes = this.routes.concat(routes.map(compileRoute));
}
if (!(names instanceof Array)) {
names = [names];
}
}, {
key: 'unregisterRoute',
value: function unregisterRoute(names) {
debug('unregistering routes: ' + names);
this.routes = this.routes.filter(route => {
const keep = names.indexOf(route.name) === -1;
if (!keep) {
debug('unregistering route ' + route.name);
if (!names) {
debug('unregistering all routes');
this.routes = [];
return;
}
return keep;
});
}
if (!(names instanceof Array)) {
names = [names];
}
getRouter(config) {
debug('building router');
debug('unregistering routes: ' + names);
let routes;
if (config.routes) {
debug('applying one time only routes');
if (!(config.routes instanceof Array)) {
config.routes = [config.routes];
}
const preRegisteredRoutes = {};
this.routes.forEach(route => {
preRegisteredRoutes[route.name] = route;
});
routes = config.routes.map(route => {
if (typeof route === 'string') {
debug('applying preregistered route ' + route);
return preRegisteredRoutes[route];
} else {
debug('applying one time route ' + route.name);
return compileRoute(route);
this.routes = this.routes.filter(function (route) {
var keep = names.indexOf(route.name) === -1;
if (!keep) {
debug('unregistering route ' + route.name);
}
return keep;
});
} else {
debug('no one time only routes defined. Using preregistered routes only');
routes = this.routes;
}
}, {
key: 'getRouter',
value: function getRouter(config) {
var _this = this;
const routeNames = {};
routes.forEach(route => {
if (routeNames[route.name]) {
throw 'Route names must be unique';
}
routeNames[route.name] = true;
});
debug('building router');
config.responses = config.responses || {};
var routes = undefined;
return (url, opts) => {
let response;
debug('searching for matching route for ' + url);
routes.some(route => {
if (config.routes) {
(function () {
debug('applying one time only routes');
if (!(config.routes instanceof Array)) {
config.routes = [config.routes];
}
if (route.matcher(url, opts)) {
debug('Found matching route (' + route.name + ') for ' + url);
this.push(route.name, [url, opts]);
var preRegisteredRoutes = {};
_this.routes.forEach(function (route) {
preRegisteredRoutes[route.name] = route;
});
routes = config.routes.map(function (route) {
if (typeof route === 'string') {
debug('applying preregistered route ' + route);
return preRegisteredRoutes[route];
} else {
debug('applying one time route ' + route.name);
return compileRoute(route);
}
});
})();
} else {
debug('no one time only routes defined. Using preregistered routes only');
routes = this.routes;
}
if (config.responses[route.name]) {
debug('Overriding response for ' + route.name);
response = config.responses[route.name];
} else {
debug('Using default response for ' + route.name);
response = route.response;
}
if (typeof response === 'function') {
debug('Constructing dynamic response for ' + route.name);
response = response(url, opts);
}
return true;
var routeNames = {};
routes.forEach(function (route) {
if (routeNames[route.name]) {
throw 'Route names must be unique';
}
routeNames[route.name] = true;
});
debug('returning response for ' + url);
return response;
};
}
config.responses = config.responses || {};
push(name, call) {
this._calls[name] = this._calls[name] || [];
this._calls[name].push(call);
}
return function (url, opts) {
var response = undefined;
debug('searching for matching route for ' + url);
routes.some(function (route) {
mock(matcher, method, response) {
if (route.matcher(url, opts)) {
debug('Found matching route (' + route.name + ') for ' + url);
_this.push(route.name, [url, opts]);
let config;
if (response) {
if (config.responses[route.name]) {
debug('Overriding response for ' + route.name);
response = config.responses[route.name];
} else {
debug('Using default response for ' + route.name);
response = route.response;
}
config = {
routes: [{
name: '_mock',
matcher,
method,
response
}]
if (typeof response === 'function') {
debug('Constructing dynamic response for ' + route.name);
response = response(url, opts);
}
return true;
}
});
debug('returning response for ' + url);
return response;
};
} else if (method) {
config = {
routes: [{
name: '_mock',
matcher,
response: method
}]
};
} else if (matcher instanceof Array) {
config = {
routes: matcher
};
} else if (matcher && matcher.matcher) {
config = {
routes: [matcher]
};
} else {
config = matcher;
}
debug('mocking fetch');
if (this.isMocking) {
throw 'fetch-mock is already mocking routes. Call .restore() before mocking again or use .reMock() if this is intentional';
}, {
key: 'push',
value: function push(name, call) {
this._calls[name] = this._calls[name] || [];
this._calls[name].push(call);
}
}, {
key: 'mock',
value: function mock(matcher, method, response) {
this.isMocking = true;
var config = undefined;
if (response) {
return this.mockedContext.fetch = this.constructMock(config);
}
config = {
routes: [{
name: '_mock',
matcher: matcher,
method: method,
response: response
}]
};
} else if (method) {
config = {
routes: [{
name: '_mock',
matcher: matcher,
response: method
}]
};
} else if (matcher instanceof Array) {
config = {
routes: matcher
};
} else if (matcher && matcher.matcher) {
config = {
routes: [matcher]
};
} else {
config = matcher;
}
constructMock(config) {
debug('constructing mock function');
config = config || {};
const router = this.getRouter(config);
config.greed = config.greed || 'none';
debug('mocking fetch');
return (url, opts) => {
const response = router(url, opts);
if (response) {
debug('response found for ' + url);
return mockResponse(url, response);
} else {
debug('response not found for ' + url);
this.push('__unmatched', [url, opts]);
if (config.greed === 'good') {
debug('sending default good response');
return mockResponse(url, { body: 'unmocked url: ' + url });
} else if (config.greed === 'bad') {
debug('sending default bad response');
return mockResponse(url, { throws: 'unmocked url: ' + url });
} else {
debug('forwarding to default fetch');
return this.realFetch(url, opts);
}
if (this.isMocking) {
throw 'fetch-mock is already mocking routes. Call .restore() before mocking again or use .reMock() if this is intentional';
}
};
}
restore() {
debug('restoring fetch');
this.isMocking = false;
this.mockedContext.fetch = this.realFetch;
this.reset();
debug('fetch restored');
}
this.isMocking = true;
reMock() {
this.restore();
this.mock.apply(this, [].slice.apply(arguments));
}
return this.mockedContext.fetch = this.constructMock(config);
}
}, {
key: 'constructMock',
value: function constructMock(config) {
var _this2 = this;
reset() {
debug('resetting call logs');
this._calls = {};
}
debug('constructing mock function');
config = config || {};
var router = this.getRouter(config);
config.greed = config.greed || 'none';
calls(name) {
return name ? this._calls[name] || [] : this._calls._mock || this._calls;
}
called(name) {
if (!name) {
return !!Object.keys(this._calls).length;
return function (url, opts) {
var response = router(url, opts);
if (response) {
debug('response found for ' + url);
return mockResponse(url, response);
} else {
debug('response not found for ' + url);
_this2.push('__unmatched', [url, opts]);
if (config.greed === 'good') {
debug('sending default good response');
return mockResponse(url, { body: 'unmocked url: ' + url });
} else if (config.greed === 'bad') {
debug('sending default bad response');
return mockResponse(url, { throws: 'unmocked url: ' + url });
} else {
debug('forwarding to default fetch');
return _this2.realFetch(url, opts);
}
}
};
}
return !!(this._calls[name] && this._calls[name].length);
}
}
}, {
key: 'restore',
value: function restore() {
debug('restoring fetch');
this.isMocking = false;
this.mockedContext.fetch = this.realFetch;
this.reset();
debug('fetch restored');
}
}, {
key: 'reMock',
value: function reMock() {
this.restore();
this.mock.apply(this, [].slice.apply(arguments));
}
}, {
key: 'reset',
value: function reset() {
debug('resetting call logs');
this._calls = {};
}
}, {
key: 'calls',
value: function calls(name) {
return name ? this._calls[name] || [] : this._calls._mock || this._calls;
}
}, {
key: 'called',
value: function called(name) {
if (!name) {
return !!Object.keys(this._calls).length;
}
return !!(this._calls[name] && this._calls[name].length);
}
}]);
return FetchMock;
})();
module.exports = FetchMock;
'use strict';
const Response = require('node-fetch').Response;
const Headers = require('node-fetch').Headers;
const stream = require('stream');
const FetchMock = require('./fetch-mock');
var Response = require('node-fetch').Response;
var Headers = require('node-fetch').Headers;
var stream = require('stream');
var FetchMock = require('./fetch-mock');

@@ -8,0 +8,0 @@ module.exports = new FetchMock({

@@ -1,1 +0,1 @@

{"name":"fetch-mock","version":"2.2.10","description":"Mock http requests made using fetch (or isomorphic-fetch)","main":"es5/client.js","scripts":{"test":"make test","prepublish":"babel src --out-dir es5"},"repository":{"type":"git","url":"https://github.com/wheresrhys/fetch-mock.git"},"keywords":["fetch","http","mock","testing","spy"],"author":"Rhys Evans","license":"ISC","bugs":{"url":"https://github.com/wheresrhys/fetch-mock/issues"},"homepage":"https://github.com/wheresrhys/fetch-mock","dependencies":{"node-fetch":"^1.3.3","debug":"^2.2.0"},"devDependencies":{"babel":"^6.0.15","babel-cli":"^6.1.2","babel-preset-es2015":"^6.1.2","babelify":"^7.2.0","browserify":"^12.0.0","chai":"^2.3.0","karma":"^0.12.31","karma-browserify":"^4.1.2","karma-chai":"^0.1.0","karma-chrome-launcher":"^0.1.8","karma-firefox-launcher":"^0.1.6","karma-mocha":"^0.1.10","karma-phantomjs-launcher":"^0.2.1","mocha":"^2.2.4","npm-prepublish":"^1.2.0","sinon":"^1.17.0","whatwg-fetch":"^0.10.1"}}
{"name":"fetch-mock","version":"2.2.11","description":"Mock http requests made using fetch (or isomorphic-fetch)","main":"es5/client.js","scripts":{"test":"make test","prepublish":"babel src --out-dir es5 --presets es2015"},"repository":{"type":"git","url":"https://github.com/wheresrhys/fetch-mock.git"},"keywords":["fetch","http","mock","testing","spy"],"author":"Rhys Evans","license":"ISC","bugs":{"url":"https://github.com/wheresrhys/fetch-mock/issues"},"homepage":"https://github.com/wheresrhys/fetch-mock","dependencies":{"node-fetch":"^1.3.3","debug":"^2.2.0"},"devDependencies":{"babel":"^6.0.15","babel-cli":"^6.1.2","babel-preset-es2015":"^6.1.2","babelify":"^7.2.0","browserify":"^12.0.0","chai":"^2.3.0","karma":"^0.12.31","karma-browserify":"^4.1.2","karma-chai":"^0.1.0","karma-chrome-launcher":"^0.1.8","karma-firefox-launcher":"^0.1.6","karma-mocha":"^0.1.10","karma-phantomjs-launcher":"^0.2.1","mocha":"^2.2.4","npm-prepublish":"^1.2.0","sinon":"^1.17.0","whatwg-fetch":"^0.10.1"}}
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