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

axios-mock-adapter

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-mock-adapter - npm Package Compare versions

Comparing version 1.12.0 to 1.13.0

73

dist/axios-mock-adapter.js

@@ -116,5 +116,3 @@ (function webpackUniversalModuleDefinition(root, factory) {

this.originalAdapter = axiosInstance.defaults.adapter;
this.delayResponse = options && options.delayResponse > 0
? options.delayResponse
: null;
this.delayResponse = options && options.delayResponse > 0 ? options.delayResponse : null;
axiosInstance.defaults.adapter = adapter.call(this);

@@ -138,3 +136,3 @@ }

var _this = this;
var matcher = matcher === undefined ? /.*/ : matcher;
var matcher = matcher === undefined ? /.*/ : matcher;

@@ -179,3 +177,2 @@ function reply(code, response, headers) {

}
};

@@ -191,3 +188,12 @@ };

} else {
handlers[method].push(handler);
if (handlers[method].length) {
handlers[method].forEach(function(item, index) {
if (item[0] === handler[0]) {
handlers[method].splice(index, 1, handler);
}
handlers[method].push(handler);
});
} else {
handlers[method].push(handler);
}
}

@@ -224,3 +230,11 @@ }

var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data, config.params, config.headers);
var handler = utils.findHandler(
mockAdapter.handlers,
config.method,
config.url,
config.data,
config.params,
config.headers,
config.baseURL
);

@@ -230,11 +244,14 @@ if (handler) {

if (handler.length === 2) { // passThrough handler
if (handler.length === 2) {
// passThrough handler
// tell axios to use the original adapter instead of our mock, fixes #35
config.adapter = mockAdapter.originalAdapter;
mockAdapter
.axiosInstance
.request(config)
.then(resolve, reject);
mockAdapter.axiosInstance.request(config).then(resolve, reject);
} else if (!(handler[3] instanceof Function)) {
utils.settle(resolve, reject, makeResponse(handler.slice(3), config), mockAdapter.delayResponse);
utils.settle(
resolve,
reject,
makeResponse(handler.slice(3), config),
mockAdapter.delayResponse
);
} else {

@@ -262,7 +279,13 @@ var result = handler[3](config);

}
} else { // handler not found
utils.settle(resolve, reject, {
status: 404,
config: config
}, mockAdapter.delayResponse);
} else {
// handler not found
utils.settle(
resolve,
reject,
{
status: 404,
config: config
},
mockAdapter.delayResponse
);
}

@@ -299,8 +322,16 @@ }

function findHandler(handlers, method, url, body, parameters, headers) {
function combineUrls(baseURL, url) {
if (baseURL) {
return baseURL.replace(/\/+$/, '') + '/' + url.replace(/^\/+/, '');
}
return url;
}
function findHandler(handlers, method, url, body, parameters, headers, baseURL) {
return find(handlers[method.toLowerCase()], function(handler) {
if (typeof handler[0] === 'string') {
return isUrlMatching(url, handler[0]) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
return (isUrlMatching(url, handler[0]) || isUrlMatching(combineUrls(baseURL, url), handler[0])) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
} else if (handler[0] instanceof RegExp) {
return handler[0].test(url) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
return (handler[0].test(url) || handler[0].test(combineUrls(baseURL, url))) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
}

@@ -307,0 +338,0 @@ });

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

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("axios")):"function"==typeof define&&define.amd?define(["axios"],t):"object"==typeof exports?exports.AxiosMockAdapter=t(require("axios")):e.AxiosMockAdapter=t(e.axios)}("undefined"!=typeof self?self:this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";function r(){return function(e){var t=this;if(3!==arguments.length)return new Promise(function(n,r){u(t,n,r,e)});u(t,arguments[0],arguments[1],arguments[2])}.bind(this)}function o(){this.handlers=a.reduce(function(e,t){return e[t]=[],e},{}),this.replyOnceHandlers=[]}function i(e,t){o.call(this),e&&(this.axiosInstance=e,this.originalAdapter=e.defaults.adapter,this.delayResponse=t&&t.delayResponse>0?t.delayResponse:null,e.defaults.adapter=r.call(this))}function s(e,t,n){"any"===e?a.forEach(function(e){t[e].push(n)}):t[e].push(n)}var u=n(1),a=["get","post","head","delete","patch","put","options"];i.prototype.adapter=r,i.prototype.restore=function(){this.axiosInstance&&(this.axiosInstance.defaults.adapter=this.originalAdapter)},i.prototype.reset=o,a.concat("any").forEach(function(e){var t="on"+e.charAt(0).toUpperCase()+e.slice(1);i.prototype[t]=function(t,n,r){function o(o,u,a){var c=[t,n,r,o,u,a];return s(e,i.handlers,c),i}var i=this,t=void 0===t?/.*/:t;return{reply:o,replyOnce:function(o,u,a){var c=[t,n,r,o,u,a];return s(e,i.handlers,c),i.replyOnceHandlers.push(c),i},passThrough:function(){var r=[t,n];return s(e,i.handlers,r),i},networkError:function(){o(function(e){var t=new Error("Network Error");return t.config=e,Promise.reject(t)})},timeout:function(){o(function(e){var t=new Error("timeout of "+e.timeout+"ms exceeded");return t.config=e,t.code="ECONNABORTED",Promise.reject(t)})}}}}),e.exports=i,e.exports.default=i},function(e,t,n){"use strict";function r(e,t){return{status:e[0],data:i.isSimpleObject(e[1])?JSON.parse(JSON.stringify(e[1])):e[1],headers:e[2],config:t}}function o(e,t,n,o){o.baseURL&&o.url.substr(0,o.baseURL.length)===o.baseURL&&(o.url=o.url.slice(o.baseURL?o.baseURL.length:0)),o.adapter=null;var s=i.findHandler(e.handlers,o.method,o.url,o.data,o.params,o.headers);if(s)if(i.purgeIfReplyOnce(e,s),2===s.length)o.adapter=e.originalAdapter,e.axiosInstance.request(o).then(t,n);else if(s[3]instanceof Function){var u=s[3](o);u.then instanceof Function?u.then(function(s){i.settle(t,n,r(s,o),e.delayResponse)},function(t){e.delayResponse>0?setTimeout(function(){n(t)},e.delayResponse):n(t)}):i.settle(t,n,r(u,o),e.delayResponse)}else i.settle(t,n,r(s.slice(3),o),e.delayResponse);else i.settle(t,n,{status:404,config:o},e.delayResponse)}var i=n(2);e.exports=o},function(e,t,n){"use strict";function r(e,t){return g(e,t,{strict:!0})}function o(e,t){for(var n=e.length,r=0;r<n;r++){var o=e[r];if(t(o))return o}}function i(e,t,n,r,i,c){return o(e[t.toLowerCase()],function(e){return"string"==typeof e[0]?s(n,e[0])&&a(t,r,i,e[1])&&u(c,e[2]):e[0]instanceof RegExp?e[0].test(n)&&a(t,r,i,e[1])&&u(c,e[2]):void 0})}function s(e,t){return("/"===e[0]?e.substr(1):e)===("/"===t[0]?t.substr(1):t)}function u(e,t){return void 0===t||r(e,t)}function a(e,t,n,r){if(["delete","get","head","options"].indexOf(e.toLowerCase())>=0)return c(n,r?r.params:void 0);return f(t,r)}function c(e,t){return void 0===t||r(e,t)}function f(e,t){if(void 0===t)return!0;var n;try{n=JSON.parse(e)}catch(e){}return n?r(n,t):r(e,t)}function l(e,t){var n=e.replyOnceHandlers.indexOf(t);n>-1&&(e.replyOnceHandlers.splice(n,1),Object.keys(e.handlers).forEach(function(r){(n=e.handlers[r].indexOf(t))>-1&&e.handlers[r].splice(n,1)}))}function p(e,t,n,r){return r>0?void setTimeout(function(){p(e,t,n)},r):n.config&&n.config.validateStatus?void(n.config.validateStatus(n.status)?e(n):t(d("Request failed with status code "+n.status,n.config,n))):void(n.status>=200&&n.status<300?e(n):t(n))}function d(e,t,n){if(!v)return n;var r=new Error(e);return r.config=t,r.response=n,r}function h(e){return null!==e&&void 0!==e&&"[object Object]"===e.toString()}var y=n(3),g=n(4),v=!!y.create().defaults.headers;e.exports={find:o,findHandler:i,isSimpleObject:h,purgeIfReplyOnce:l,settle:p}},function(t,n){t.exports=e},function(e,t,n){function r(e){return null===e||void 0===e}function o(e){return!(!e||"object"!=typeof e||"number"!=typeof e.length)&&("function"==typeof e.copy&&"function"==typeof e.slice&&!(e.length>0&&"number"!=typeof e[0]))}function i(e,t,n){var i,f;if(r(e)||r(t))return!1;if(e.prototype!==t.prototype)return!1;if(a(e))return!!a(t)&&(e=s.call(e),t=s.call(t),c(e,t,n));if(o(e)){if(!o(t))return!1;if(e.length!==t.length)return!1;for(i=0;i<e.length;i++)if(e[i]!==t[i])return!1;return!0}try{var l=u(e),p=u(t)}catch(e){return!1}if(l.length!=p.length)return!1;for(l.sort(),p.sort(),i=l.length-1;i>=0;i--)if(l[i]!=p[i])return!1;for(i=l.length-1;i>=0;i--)if(f=l[i],!c(e[f],t[f],n))return!1;return typeof e==typeof t}var s=Array.prototype.slice,u=n(5),a=n(6),c=e.exports=function(e,t,n){return n||(n={}),e===t||(e instanceof Date&&t instanceof Date?e.getTime()===t.getTime():!e||!t||"object"!=typeof e&&"object"!=typeof t?n.strict?e===t:e==t:i(e,t,n))}},function(e,t){function n(e){var t=[];for(var n in e)t.push(n);return t}t=e.exports="function"==typeof Object.keys?Object.keys:n,t.shim=n},function(e,t){function n(e){return"[object Arguments]"==Object.prototype.toString.call(e)}function r(e){return e&&"object"==typeof e&&"number"==typeof e.length&&Object.prototype.hasOwnProperty.call(e,"callee")&&!Object.prototype.propertyIsEnumerable.call(e,"callee")||!1}var o="[object Arguments]"==function(){return Object.prototype.toString.call(arguments)}();t=e.exports=o?n:r,t.supported=n,t.unsupported=r}])});
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("axios")):"function"==typeof define&&define.amd?define(["axios"],t):"object"==typeof exports?exports.AxiosMockAdapter=t(require("axios")):e.AxiosMockAdapter=t(e.axios)}("undefined"!=typeof self?self:this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";function r(){return function(e){var t=this;if(3!==arguments.length)return new Promise(function(n,r){u(t,n,r,e)});u(t,arguments[0],arguments[1],arguments[2])}.bind(this)}function o(){this.handlers=a.reduce(function(e,t){return e[t]=[],e},{}),this.replyOnceHandlers=[]}function i(e,t){o.call(this),e&&(this.axiosInstance=e,this.originalAdapter=e.defaults.adapter,this.delayResponse=t&&t.delayResponse>0?t.delayResponse:null,e.defaults.adapter=r.call(this))}function s(e,t,n){"any"===e?a.forEach(function(e){t[e].push(n)}):t[e].length?t[e].forEach(function(r,o){r[0]===n[0]&&t[e].splice(o,1,n),t[e].push(n)}):t[e].push(n)}var u=n(1),a=["get","post","head","delete","patch","put","options"];i.prototype.adapter=r,i.prototype.restore=function(){this.axiosInstance&&(this.axiosInstance.defaults.adapter=this.originalAdapter)},i.prototype.reset=o,a.concat("any").forEach(function(e){var t="on"+e.charAt(0).toUpperCase()+e.slice(1);i.prototype[t]=function(t,n,r){function o(o,u,a){var c=[t,n,r,o,u,a];return s(e,i.handlers,c),i}var i=this,t=void 0===t?/.*/:t;return{reply:o,replyOnce:function(o,u,a){var c=[t,n,r,o,u,a];return s(e,i.handlers,c),i.replyOnceHandlers.push(c),i},passThrough:function(){var r=[t,n];return s(e,i.handlers,r),i},networkError:function(){o(function(e){var t=new Error("Network Error");return t.config=e,Promise.reject(t)})},timeout:function(){o(function(e){var t=new Error("timeout of "+e.timeout+"ms exceeded");return t.config=e,t.code="ECONNABORTED",Promise.reject(t)})}}}}),e.exports=i,e.exports.default=i},function(e,t,n){"use strict";function r(e,t){return{status:e[0],data:i.isSimpleObject(e[1])?JSON.parse(JSON.stringify(e[1])):e[1],headers:e[2],config:t}}function o(e,t,n,o){o.baseURL&&o.url.substr(0,o.baseURL.length)===o.baseURL&&(o.url=o.url.slice(o.baseURL?o.baseURL.length:0)),o.adapter=null;var s=i.findHandler(e.handlers,o.method,o.url,o.data,o.params,o.headers,o.baseURL);if(s)if(i.purgeIfReplyOnce(e,s),2===s.length)o.adapter=e.originalAdapter,e.axiosInstance.request(o).then(t,n);else if(s[3]instanceof Function){var u=s[3](o);u.then instanceof Function?u.then(function(s){i.settle(t,n,r(s,o),e.delayResponse)},function(t){e.delayResponse>0?setTimeout(function(){n(t)},e.delayResponse):n(t)}):i.settle(t,n,r(u,o),e.delayResponse)}else i.settle(t,n,r(s.slice(3),o),e.delayResponse);else i.settle(t,n,{status:404,config:o},e.delayResponse)}var i=n(2);e.exports=o},function(e,t,n){"use strict";function r(e,t){return v(e,t,{strict:!0})}function o(e,t){for(var n=e.length,r=0;r<n;r++){var o=e[r];if(t(o))return o}}function i(e,t){return e?e.replace(/\/+$/,"")+"/"+t.replace(/^\/+/,""):t}function s(e,t,n,r,s,f,l){return o(e[t.toLowerCase()],function(e){return"string"==typeof e[0]?(u(n,e[0])||u(i(l,n),e[0]))&&c(t,r,s,e[1])&&a(f,e[2]):e[0]instanceof RegExp?(e[0].test(n)||e[0].test(i(l,n)))&&c(t,r,s,e[1])&&a(f,e[2]):void 0})}function u(e,t){return("/"===e[0]?e.substr(1):e)===("/"===t[0]?t.substr(1):t)}function a(e,t){return void 0===t||r(e,t)}function c(e,t,n,r){if(["delete","get","head","options"].indexOf(e.toLowerCase())>=0)return f(n,r?r.params:void 0);return l(t,r)}function f(e,t){return void 0===t||r(e,t)}function l(e,t){if(void 0===t)return!0;var n;try{n=JSON.parse(e)}catch(e){}return n?r(n,t):r(e,t)}function p(e,t){var n=e.replyOnceHandlers.indexOf(t);n>-1&&(e.replyOnceHandlers.splice(n,1),Object.keys(e.handlers).forEach(function(r){(n=e.handlers[r].indexOf(t))>-1&&e.handlers[r].splice(n,1)}))}function d(e,t,n,r){return r>0?void setTimeout(function(){d(e,t,n)},r):n.config&&n.config.validateStatus?void(n.config.validateStatus(n.status)?e(n):t(h("Request failed with status code "+n.status,n.config,n))):void(n.status>=200&&n.status<300?e(n):t(n))}function h(e,t,n){if(!b)return n;var r=new Error(e);return r.config=t,r.response=n,r}function y(e){return null!==e&&void 0!==e&&"[object Object]"===e.toString()}var g=n(3),v=n(4),b=!!g.create().defaults.headers;e.exports={find:o,findHandler:s,isSimpleObject:y,purgeIfReplyOnce:p,settle:d}},function(t,n){t.exports=e},function(e,t,n){function r(e){return null===e||void 0===e}function o(e){return!(!e||"object"!=typeof e||"number"!=typeof e.length)&&("function"==typeof e.copy&&"function"==typeof e.slice&&!(e.length>0&&"number"!=typeof e[0]))}function i(e,t,n){var i,f;if(r(e)||r(t))return!1;if(e.prototype!==t.prototype)return!1;if(a(e))return!!a(t)&&(e=s.call(e),t=s.call(t),c(e,t,n));if(o(e)){if(!o(t))return!1;if(e.length!==t.length)return!1;for(i=0;i<e.length;i++)if(e[i]!==t[i])return!1;return!0}try{var l=u(e),p=u(t)}catch(e){return!1}if(l.length!=p.length)return!1;for(l.sort(),p.sort(),i=l.length-1;i>=0;i--)if(l[i]!=p[i])return!1;for(i=l.length-1;i>=0;i--)if(f=l[i],!c(e[f],t[f],n))return!1;return typeof e==typeof t}var s=Array.prototype.slice,u=n(5),a=n(6),c=e.exports=function(e,t,n){return n||(n={}),e===t||(e instanceof Date&&t instanceof Date?e.getTime()===t.getTime():!e||!t||"object"!=typeof e&&"object"!=typeof t?n.strict?e===t:e==t:i(e,t,n))}},function(e,t){function n(e){var t=[];for(var n in e)t.push(n);return t}t=e.exports="function"==typeof Object.keys?Object.keys:n,t.shim=n},function(e,t){function n(e){return"[object Arguments]"==Object.prototype.toString.call(e)}function r(e){return e&&"object"==typeof e&&"number"==typeof e.length&&Object.prototype.hasOwnProperty.call(e,"callee")&&!Object.prototype.propertyIsEnumerable.call(e,"callee")||!1}var o="[object Arguments]"==function(){return Object.prototype.toString.call(arguments)}();t=e.exports=o?n:r,t.supported=n,t.unsupported=r}])});
{
"name": "axios-mock-adapter",
"version": "1.12.0",
"version": "1.13.0",
"description": "Axios adapter that allows to easily mock requests",

@@ -46,3 +46,3 @@ "main": "src/index.js",

"cross-env": "^5.1.0",
"eslint": "^4.3.0",
"eslint": "^4.17.0",
"istanbul": "^0.4.5",

@@ -49,0 +49,0 @@ "mocha": "^5.0.0",

@@ -20,3 +20,11 @@ 'use strict';

var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data, config.params, config.headers);
var handler = utils.findHandler(
mockAdapter.handlers,
config.method,
config.url,
config.data,
config.params,
config.headers,
config.baseURL
);

@@ -26,11 +34,14 @@ if (handler) {

if (handler.length === 2) { // passThrough handler
if (handler.length === 2) {
// passThrough handler
// tell axios to use the original adapter instead of our mock, fixes #35
config.adapter = mockAdapter.originalAdapter;
mockAdapter
.axiosInstance
.request(config)
.then(resolve, reject);
mockAdapter.axiosInstance.request(config).then(resolve, reject);
} else if (!(handler[3] instanceof Function)) {
utils.settle(resolve, reject, makeResponse(handler.slice(3), config), mockAdapter.delayResponse);
utils.settle(
resolve,
reject,
makeResponse(handler.slice(3), config),
mockAdapter.delayResponse
);
} else {

@@ -58,7 +69,13 @@ var result = handler[3](config);

}
} else { // handler not found
utils.settle(resolve, reject, {
status: 404,
config: config
}, mockAdapter.delayResponse);
} else {
// handler not found
utils.settle(
resolve,
reject,
{
status: 404,
config: config
},
mockAdapter.delayResponse
);
}

@@ -65,0 +82,0 @@ }

@@ -36,5 +36,3 @@ 'use strict';

this.originalAdapter = axiosInstance.defaults.adapter;
this.delayResponse = options && options.delayResponse > 0
? options.delayResponse
: null;
this.delayResponse = options && options.delayResponse > 0 ? options.delayResponse : null;
axiosInstance.defaults.adapter = adapter.call(this);

@@ -58,3 +56,3 @@ }

var _this = this;
var matcher = matcher === undefined ? /.*/ : matcher;
var matcher = matcher === undefined ? /.*/ : matcher;

@@ -99,3 +97,2 @@ function reply(code, response, headers) {

}
};

@@ -111,3 +108,12 @@ };

} else {
handlers[method].push(handler);
if (handlers[method].length) {
handlers[method].forEach(function(item, index) {
if (item[0] === handler[0]) {
handlers[method].splice(index, 1, handler);
}
handlers[method].push(handler);
});
} else {
handlers[method].push(handler);
}
}

@@ -114,0 +120,0 @@ }

@@ -21,8 +21,16 @@ 'use strict';

function findHandler(handlers, method, url, body, parameters, headers) {
function combineUrls(baseURL, url) {
if (baseURL) {
return baseURL.replace(/\/+$/, '') + '/' + url.replace(/^\/+/, '');
}
return url;
}
function findHandler(handlers, method, url, body, parameters, headers, baseURL) {
return find(handlers[method.toLowerCase()], function(handler) {
if (typeof handler[0] === 'string') {
return isUrlMatching(url, handler[0]) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
return (isUrlMatching(url, handler[0]) || isUrlMatching(combineUrls(baseURL, url), handler[0])) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
} else if (handler[0] instanceof RegExp) {
return handler[0].test(url) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
return (handler[0].test(url) || handler[0].test(combineUrls(baseURL, url))) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
}

@@ -29,0 +37,0 @@ });

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