diff options
Diffstat (limited to 'axios/test')
48 files changed, 4453 insertions, 0 deletions
diff --git a/axios/test/manual/basic.html b/axios/test/manual/basic.html new file mode 100644 index 0000000..35fa649 --- /dev/null +++ b/axios/test/manual/basic.html @@ -0,0 +1,21 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> +</head> +<body> + +An alert should be shown with the <code>{"name":"axios"}</code> + +<script src="promise.js"></script> +<script src="../../dist/axios.js"></script> +<script> + axios.get('./fixture.json').then(function(response) { + console.log(response); + alert(JSON.stringify(response.data)); + alert('response headers:\n\n' + JSON.stringify(response.headers)); + }, function(err) { console.log(err) }); +</script> + +</body> +</html>
\ No newline at end of file diff --git a/axios/test/manual/cors.html b/axios/test/manual/cors.html new file mode 100644 index 0000000..da6e946 --- /dev/null +++ b/axios/test/manual/cors.html @@ -0,0 +1,20 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> +</head> +<body> + +An alert should be shown with <code>{"status":"ok"}</code> + +<script src="promise.js"></script> +<script src="../../dist/axios.js"></script> +<script> + axios.get('http://cors-test.appspot.com/test').then(function(response) { + alert(JSON.stringify(response.data)); + alert('response headers:\n\n' + JSON.stringify(response.headers)); + }, function(err) { console.log(err) }); +</script> + +</body> +</html>
\ No newline at end of file diff --git a/axios/test/manual/fixture.json b/axios/test/manual/fixture.json new file mode 100644 index 0000000..5580765 --- /dev/null +++ b/axios/test/manual/fixture.json @@ -0,0 +1,3 @@ +{ + "name": "axios" +}
\ No newline at end of file diff --git a/axios/test/manual/promise.js b/axios/test/manual/promise.js new file mode 100644 index 0000000..2d48351 --- /dev/null +++ b/axios/test/manual/promise.js @@ -0,0 +1,9 @@ +/*! + * @overview es6-promise - a tiny implementation of Promises/A+. + * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) + * @license Licensed under MIT license + * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE + * @version 3.0.2 + */ + +(function(){"use strict";function lib$es6$promise$utils$$objectOrFunction(x){return typeof x==="function"||typeof x==="object"&&x!==null}function lib$es6$promise$utils$$isFunction(x){return typeof x==="function"}function lib$es6$promise$utils$$isMaybeThenable(x){return typeof x==="object"&&x!==null}var lib$es6$promise$utils$$_isArray;if(!Array.isArray){lib$es6$promise$utils$$_isArray=function(x){return Object.prototype.toString.call(x)==="[object Array]"}}else{lib$es6$promise$utils$$_isArray=Array.isArray}var lib$es6$promise$utils$$isArray=lib$es6$promise$utils$$_isArray;var lib$es6$promise$asap$$len=0;var lib$es6$promise$asap$$toString={}.toString;var lib$es6$promise$asap$$vertxNext;var lib$es6$promise$asap$$customSchedulerFn;var lib$es6$promise$asap$$asap=function asap(callback,arg){lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len]=callback;lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len+1]=arg;lib$es6$promise$asap$$len+=2;if(lib$es6$promise$asap$$len===2){if(lib$es6$promise$asap$$customSchedulerFn){lib$es6$promise$asap$$customSchedulerFn(lib$es6$promise$asap$$flush)}else{lib$es6$promise$asap$$scheduleFlush()}}};function lib$es6$promise$asap$$setScheduler(scheduleFn){lib$es6$promise$asap$$customSchedulerFn=scheduleFn}function lib$es6$promise$asap$$setAsap(asapFn){lib$es6$promise$asap$$asap=asapFn}var lib$es6$promise$asap$$browserWindow=typeof window!=="undefined"?window:undefined;var lib$es6$promise$asap$$browserGlobal=lib$es6$promise$asap$$browserWindow||{};var lib$es6$promise$asap$$BrowserMutationObserver=lib$es6$promise$asap$$browserGlobal.MutationObserver||lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;var lib$es6$promise$asap$$isNode=typeof process!=="undefined"&&{}.toString.call(process)==="[object process]";var lib$es6$promise$asap$$isWorker=typeof Uint8ClampedArray!=="undefined"&&typeof importScripts!=="undefined"&&typeof MessageChannel!=="undefined";function lib$es6$promise$asap$$useNextTick(){return function(){process.nextTick(lib$es6$promise$asap$$flush)}}function lib$es6$promise$asap$$useVertxTimer(){return function(){lib$es6$promise$asap$$vertxNext(lib$es6$promise$asap$$flush)}}function lib$es6$promise$asap$$useMutationObserver(){var iterations=0;var observer=new lib$es6$promise$asap$$BrowserMutationObserver(lib$es6$promise$asap$$flush);var node=document.createTextNode("");observer.observe(node,{characterData:true});return function(){node.data=iterations=++iterations%2}}function lib$es6$promise$asap$$useMessageChannel(){var channel=new MessageChannel;channel.port1.onmessage=lib$es6$promise$asap$$flush;return function(){channel.port2.postMessage(0)}}function lib$es6$promise$asap$$useSetTimeout(){return function(){setTimeout(lib$es6$promise$asap$$flush,1)}}var lib$es6$promise$asap$$queue=new Array(1e3);function lib$es6$promise$asap$$flush(){for(var i=0;i<lib$es6$promise$asap$$len;i+=2){var callback=lib$es6$promise$asap$$queue[i];var arg=lib$es6$promise$asap$$queue[i+1];callback(arg);lib$es6$promise$asap$$queue[i]=undefined;lib$es6$promise$asap$$queue[i+1]=undefined}lib$es6$promise$asap$$len=0}function lib$es6$promise$asap$$attemptVertx(){try{var r=require;var vertx=r("vertx");lib$es6$promise$asap$$vertxNext=vertx.runOnLoop||vertx.runOnContext;return lib$es6$promise$asap$$useVertxTimer()}catch(e){return lib$es6$promise$asap$$useSetTimeout()}}var lib$es6$promise$asap$$scheduleFlush;if(lib$es6$promise$asap$$isNode){lib$es6$promise$asap$$scheduleFlush=lib$es6$promise$asap$$useNextTick()}else if(lib$es6$promise$asap$$BrowserMutationObserver){lib$es6$promise$asap$$scheduleFlush=lib$es6$promise$asap$$useMutationObserver()}else if(lib$es6$promise$asap$$isWorker){lib$es6$promise$asap$$scheduleFlush=lib$es6$promise$asap$$useMessageChannel()}else if(lib$es6$promise$asap$$browserWindow===undefined&&typeof require==="function"){lib$es6$promise$asap$$scheduleFlush=lib$es6$promise$asap$$attemptVertx()}else{lib$es6$promise$asap$$scheduleFlush=lib$es6$promise$asap$$useSetTimeout()}function lib$es6$promise$$internal$$noop(){}var lib$es6$promise$$internal$$PENDING=void 0;var lib$es6$promise$$internal$$FULFILLED=1;var lib$es6$promise$$internal$$REJECTED=2;var lib$es6$promise$$internal$$GET_THEN_ERROR=new lib$es6$promise$$internal$$ErrorObject;function lib$es6$promise$$internal$$selfFulfillment(){return new TypeError("You cannot resolve a promise with itself")}function lib$es6$promise$$internal$$cannotReturnOwn(){return new TypeError("A promises callback cannot return that same promise.")}function lib$es6$promise$$internal$$getThen(promise){try{return promise.then}catch(error){lib$es6$promise$$internal$$GET_THEN_ERROR.error=error;return lib$es6$promise$$internal$$GET_THEN_ERROR}}function lib$es6$promise$$internal$$tryThen(then,value,fulfillmentHandler,rejectionHandler){try{then.call(value,fulfillmentHandler,rejectionHandler)}catch(e){return e}}function lib$es6$promise$$internal$$handleForeignThenable(promise,thenable,then){lib$es6$promise$asap$$asap(function(promise){var sealed=false;var error=lib$es6$promise$$internal$$tryThen(then,thenable,function(value){if(sealed){return}sealed=true;if(thenable!==value){lib$es6$promise$$internal$$resolve(promise,value)}else{lib$es6$promise$$internal$$fulfill(promise,value)}},function(reason){if(sealed){return}sealed=true;lib$es6$promise$$internal$$reject(promise,reason)},"Settle: "+(promise._label||" unknown promise"));if(!sealed&&error){sealed=true;lib$es6$promise$$internal$$reject(promise,error)}},promise)}function lib$es6$promise$$internal$$handleOwnThenable(promise,thenable){if(thenable._state===lib$es6$promise$$internal$$FULFILLED){lib$es6$promise$$internal$$fulfill(promise,thenable._result)}else if(thenable._state===lib$es6$promise$$internal$$REJECTED){lib$es6$promise$$internal$$reject(promise,thenable._result)}else{lib$es6$promise$$internal$$subscribe(thenable,undefined,function(value){lib$es6$promise$$internal$$resolve(promise,value)},function(reason){lib$es6$promise$$internal$$reject(promise,reason)})}}function lib$es6$promise$$internal$$handleMaybeThenable(promise,maybeThenable){if(maybeThenable.constructor===promise.constructor){lib$es6$promise$$internal$$handleOwnThenable(promise,maybeThenable)}else{var then=lib$es6$promise$$internal$$getThen(maybeThenable);if(then===lib$es6$promise$$internal$$GET_THEN_ERROR){lib$es6$promise$$internal$$reject(promise,lib$es6$promise$$internal$$GET_THEN_ERROR.error)}else if(then===undefined){lib$es6$promise$$internal$$fulfill(promise,maybeThenable)}else if(lib$es6$promise$utils$$isFunction(then)){lib$es6$promise$$internal$$handleForeignThenable(promise,maybeThenable,then)}else{lib$es6$promise$$internal$$fulfill(promise,maybeThenable)}}}function lib$es6$promise$$internal$$resolve(promise,value){if(promise===value){lib$es6$promise$$internal$$reject(promise,lib$es6$promise$$internal$$selfFulfillment())}else if(lib$es6$promise$utils$$objectOrFunction(value)){lib$es6$promise$$internal$$handleMaybeThenable(promise,value)}else{lib$es6$promise$$internal$$fulfill(promise,value)}}function lib$es6$promise$$internal$$publishRejection(promise){if(promise._onerror){promise._onerror(promise._result)}lib$es6$promise$$internal$$publish(promise)}function lib$es6$promise$$internal$$fulfill(promise,value){if(promise._state!==lib$es6$promise$$internal$$PENDING){return}promise._result=value;promise._state=lib$es6$promise$$internal$$FULFILLED;if(promise._subscribers.length!==0){lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish,promise)}}function lib$es6$promise$$internal$$reject(promise,reason){if(promise._state!==lib$es6$promise$$internal$$PENDING){return}promise._state=lib$es6$promise$$internal$$REJECTED;promise._result=reason;lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection,promise)}function lib$es6$promise$$internal$$subscribe(parent,child,onFulfillment,onRejection){var subscribers=parent._subscribers;var length=subscribers.length;parent._onerror=null;subscribers[length]=child;subscribers[length+lib$es6$promise$$internal$$FULFILLED]=onFulfillment;subscribers[length+lib$es6$promise$$internal$$REJECTED]=onRejection;if(length===0&&parent._state){lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish,parent)}}function lib$es6$promise$$internal$$publish(promise){var subscribers=promise._subscribers;var settled=promise._state;if(subscribers.length===0){return}var child,callback,detail=promise._result;for(var i=0;i<subscribers.length;i+=3){child=subscribers[i];callback=subscribers[i+settled];if(child){lib$es6$promise$$internal$$invokeCallback(settled,child,callback,detail)}else{callback(detail)}}promise._subscribers.length=0}function lib$es6$promise$$internal$$ErrorObject(){this.error=null}var lib$es6$promise$$internal$$TRY_CATCH_ERROR=new lib$es6$promise$$internal$$ErrorObject;function lib$es6$promise$$internal$$tryCatch(callback,detail){try{return callback(detail)}catch(e){lib$es6$promise$$internal$$TRY_CATCH_ERROR.error=e;return lib$es6$promise$$internal$$TRY_CATCH_ERROR}}function lib$es6$promise$$internal$$invokeCallback(settled,promise,callback,detail){var hasCallback=lib$es6$promise$utils$$isFunction(callback),value,error,succeeded,failed;if(hasCallback){value=lib$es6$promise$$internal$$tryCatch(callback,detail);if(value===lib$es6$promise$$internal$$TRY_CATCH_ERROR){failed=true;error=value.error;value=null}else{succeeded=true}if(promise===value){lib$es6$promise$$internal$$reject(promise,lib$es6$promise$$internal$$cannotReturnOwn());return}}else{value=detail;succeeded=true}if(promise._state!==lib$es6$promise$$internal$$PENDING){}else if(hasCallback&&succeeded){lib$es6$promise$$internal$$resolve(promise,value)}else if(failed){lib$es6$promise$$internal$$reject(promise,error)}else if(settled===lib$es6$promise$$internal$$FULFILLED){lib$es6$promise$$internal$$fulfill(promise,value)}else if(settled===lib$es6$promise$$internal$$REJECTED){lib$es6$promise$$internal$$reject(promise,value)}}function lib$es6$promise$$internal$$initializePromise(promise,resolver){try{resolver(function resolvePromise(value){lib$es6$promise$$internal$$resolve(promise,value)},function rejectPromise(reason){lib$es6$promise$$internal$$reject(promise,reason)})}catch(e){lib$es6$promise$$internal$$reject(promise,e)}}function lib$es6$promise$enumerator$$Enumerator(Constructor,input){var enumerator=this;enumerator._instanceConstructor=Constructor;enumerator.promise=new Constructor(lib$es6$promise$$internal$$noop);if(enumerator._validateInput(input)){enumerator._input=input;enumerator.length=input.length;enumerator._remaining=input.length;enumerator._init();if(enumerator.length===0){lib$es6$promise$$internal$$fulfill(enumerator.promise,enumerator._result)}else{enumerator.length=enumerator.length||0;enumerator._enumerate();if(enumerator._remaining===0){lib$es6$promise$$internal$$fulfill(enumerator.promise,enumerator._result)}}}else{lib$es6$promise$$internal$$reject(enumerator.promise,enumerator._validationError())}}lib$es6$promise$enumerator$$Enumerator.prototype._validateInput=function(input){return lib$es6$promise$utils$$isArray(input)};lib$es6$promise$enumerator$$Enumerator.prototype._validationError=function(){return new Error("Array Methods must be provided an Array")};lib$es6$promise$enumerator$$Enumerator.prototype._init=function(){this._result=new Array(this.length)};var lib$es6$promise$enumerator$$default=lib$es6$promise$enumerator$$Enumerator;lib$es6$promise$enumerator$$Enumerator.prototype._enumerate=function(){var enumerator=this;var length=enumerator.length;var promise=enumerator.promise;var input=enumerator._input;for(var i=0;promise._state===lib$es6$promise$$internal$$PENDING&&i<length;i++){enumerator._eachEntry(input[i],i)}};lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry=function(entry,i){var enumerator=this;var c=enumerator._instanceConstructor;if(lib$es6$promise$utils$$isMaybeThenable(entry)){if(entry.constructor===c&&entry._state!==lib$es6$promise$$internal$$PENDING){entry._onerror=null;enumerator._settledAt(entry._state,i,entry._result)}else{enumerator._willSettleAt(c.resolve(entry),i)}}else{enumerator._remaining--;enumerator._result[i]=entry}};lib$es6$promise$enumerator$$Enumerator.prototype._settledAt=function(state,i,value){var enumerator=this;var promise=enumerator.promise;if(promise._state===lib$es6$promise$$internal$$PENDING){enumerator._remaining--;if(state===lib$es6$promise$$internal$$REJECTED){lib$es6$promise$$internal$$reject(promise,value)}else{enumerator._result[i]=value}}if(enumerator._remaining===0){lib$es6$promise$$internal$$fulfill(promise,enumerator._result)}};lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt=function(promise,i){var enumerator=this;lib$es6$promise$$internal$$subscribe(promise,undefined,function(value){enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED,i,value)},function(reason){enumerator._settledAt(lib$es6$promise$$internal$$REJECTED,i,reason)})};function lib$es6$promise$promise$all$$all(entries){return new lib$es6$promise$enumerator$$default(this,entries).promise}var lib$es6$promise$promise$all$$default=lib$es6$promise$promise$all$$all;function lib$es6$promise$promise$race$$race(entries){var Constructor=this;var promise=new Constructor(lib$es6$promise$$internal$$noop);if(!lib$es6$promise$utils$$isArray(entries)){lib$es6$promise$$internal$$reject(promise,new TypeError("You must pass an array to race."));return promise}var length=entries.length;function onFulfillment(value){lib$es6$promise$$internal$$resolve(promise,value)}function onRejection(reason){lib$es6$promise$$internal$$reject(promise,reason)}for(var i=0;promise._state===lib$es6$promise$$internal$$PENDING&&i<length;i++){lib$es6$promise$$internal$$subscribe(Constructor.resolve(entries[i]),undefined,onFulfillment,onRejection)}return promise}var lib$es6$promise$promise$race$$default=lib$es6$promise$promise$race$$race;function lib$es6$promise$promise$resolve$$resolve(object){var Constructor=this;if(object&&typeof object==="object"&&object.constructor===Constructor){return object}var promise=new Constructor(lib$es6$promise$$internal$$noop);lib$es6$promise$$internal$$resolve(promise,object);return promise}var lib$es6$promise$promise$resolve$$default=lib$es6$promise$promise$resolve$$resolve;function lib$es6$promise$promise$reject$$reject(reason){var Constructor=this;var promise=new Constructor(lib$es6$promise$$internal$$noop);lib$es6$promise$$internal$$reject(promise,reason);return promise}var lib$es6$promise$promise$reject$$default=lib$es6$promise$promise$reject$$reject;var lib$es6$promise$promise$$counter=0;function lib$es6$promise$promise$$needsResolver(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function lib$es6$promise$promise$$needsNew(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}var lib$es6$promise$promise$$default=lib$es6$promise$promise$$Promise;function lib$es6$promise$promise$$Promise(resolver){this._id=lib$es6$promise$promise$$counter++;this._state=undefined;this._result=undefined;this._subscribers=[];if(lib$es6$promise$$internal$$noop!==resolver){if(!lib$es6$promise$utils$$isFunction(resolver)){lib$es6$promise$promise$$needsResolver()}if(!(this instanceof lib$es6$promise$promise$$Promise)){lib$es6$promise$promise$$needsNew()}lib$es6$promise$$internal$$initializePromise(this,resolver)}}lib$es6$promise$promise$$Promise.all=lib$es6$promise$promise$all$$default;lib$es6$promise$promise$$Promise.race=lib$es6$promise$promise$race$$default;lib$es6$promise$promise$$Promise.resolve=lib$es6$promise$promise$resolve$$default;lib$es6$promise$promise$$Promise.reject=lib$es6$promise$promise$reject$$default;lib$es6$promise$promise$$Promise._setScheduler=lib$es6$promise$asap$$setScheduler;lib$es6$promise$promise$$Promise._setAsap=lib$es6$promise$asap$$setAsap;lib$es6$promise$promise$$Promise._asap=lib$es6$promise$asap$$asap;lib$es6$promise$promise$$Promise.prototype={constructor:lib$es6$promise$promise$$Promise,then:function(onFulfillment,onRejection){var parent=this;var state=parent._state;if(state===lib$es6$promise$$internal$$FULFILLED&&!onFulfillment||state===lib$es6$promise$$internal$$REJECTED&&!onRejection){return this}var child=new this.constructor(lib$es6$promise$$internal$$noop);var result=parent._result;if(state){var callback=arguments[state-1];lib$es6$promise$asap$$asap(function(){lib$es6$promise$$internal$$invokeCallback(state,child,callback,result)})}else{lib$es6$promise$$internal$$subscribe(parent,child,onFulfillment,onRejection)}return child},"catch":function(onRejection){return this.then(null,onRejection)}};function lib$es6$promise$polyfill$$polyfill(){var local;if(typeof global!=="undefined"){local=global}else if(typeof self!=="undefined"){local=self}else{try{local=Function("return this")()}catch(e){throw new Error("polyfill failed because global object is unavailable in this environment")}}var P=local.Promise;if(P&&Object.prototype.toString.call(P.resolve())==="[object Promise]"&&!P.cast){return}local.Promise=lib$es6$promise$promise$$default}var lib$es6$promise$polyfill$$default=lib$es6$promise$polyfill$$polyfill;var lib$es6$promise$umd$$ES6Promise={Promise:lib$es6$promise$promise$$default,polyfill:lib$es6$promise$polyfill$$default};if(typeof define==="function"&&define["amd"]){define(function(){return lib$es6$promise$umd$$ES6Promise})}else if(typeof module!=="undefined"&&module["exports"]){module["exports"]=lib$es6$promise$umd$$ES6Promise}else if(typeof this!=="undefined"){this["ES6Promise"]=lib$es6$promise$umd$$ES6Promise}lib$es6$promise$polyfill$$default()}).call(this);
\ No newline at end of file diff --git a/axios/test/specs/__helpers.js b/axios/test/specs/__helpers.js new file mode 100644 index 0000000..a08b8eb --- /dev/null +++ b/axios/test/specs/__helpers.js @@ -0,0 +1,123 @@ +// Polyfill ES6 Promise +require('es6-promise').polyfill(); + +// Polyfill URLSearchParams +URLSearchParams = require('url-search-params'); + +// Import axios +axios = require('../../index'); + +// Jasmine config +jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; +jasmine.getEnv().defaultTimeoutInterval = 20000; + +// Get Ajax request using an increasing timeout to retry +getAjaxRequest = (function () { +var attempts = 0; +var MAX_ATTEMPTS = 5; +var ATTEMPT_DELAY_FACTOR = 5; + +function getAjaxRequest() { + return new Promise(function (resolve, reject) { + attempts = 0; + attemptGettingAjaxRequest(resolve, reject); + }); +} + +function attemptGettingAjaxRequest(resolve, reject) { + var delay = attempts * attempts * ATTEMPT_DELAY_FACTOR; + + if (attempts++ > MAX_ATTEMPTS) { + reject(new Error('No request was found')); + return; + } + + setTimeout(function () { + var request = jasmine.Ajax.requests.mostRecent(); + if (request) { + resolve(request); + } else { + attemptGettingAjaxRequest(resolve, reject); + } + }, delay); +} + +return getAjaxRequest; +})(); + +// Validate an invalid character error +validateInvalidCharacterError = function validateInvalidCharacterError(error) { + expect(/character/i.test(error.message)).toEqual(true); +}; + +// Setup basic auth tests +setupBasicAuthTest = function setupBasicAuthTest() { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should accept HTTP Basic auth with username/password', function (done) { + axios('/foo', { + auth: { + username: 'Aladdin', + password: 'open sesame' + } + }); + + setTimeout(function () { + var request = jasmine.Ajax.requests.mostRecent(); + + expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='); + done(); + }, 100); + }); + + it('should accept HTTP Basic auth credentials without the password parameter', function (done) { + axios('/foo', { + auth: { + username: 'Aladdin' + } + }); + + setTimeout(function () { + var request = jasmine.Ajax.requests.mostRecent(); + + expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjo='); + done(); + }, 100); + }); + + it('should accept HTTP Basic auth credentials with non-Latin1 characters in password', function (done) { + axios('/foo', { + auth: { + username: 'Aladdin', + password: 'open ßç£☃sesame' + } + }); + + setTimeout(function () { + var request = jasmine.Ajax.requests.mostRecent(); + + expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIMOfw6fCo+KYg3Nlc2FtZQ=='); + done(); + }, 100); + }); + + it('should fail to encode HTTP Basic auth credentials with non-Latin1 characters in username', function (done) { + axios('/foo', { + auth: { + username: 'Aladßç£☃din', + password: 'open sesame' + } + }).then(function(response) { + done(new Error('Should not succeed to make a HTTP Basic auth request with non-latin1 chars in credentials.')); + }).catch(function(error) { + validateInvalidCharacterError(error); + done(); + }); + }); +}; diff --git a/axios/test/specs/adapter.spec.js b/axios/test/specs/adapter.spec.js new file mode 100644 index 0000000..6a00bc9 --- /dev/null +++ b/axios/test/specs/adapter.spec.js @@ -0,0 +1,19 @@ +var axios = require('../../index'); + +describe('adapter', function () { + it('should support custom adapter', function (done) { + var called = false; + + axios('/foo', { + adapter: function (config) { + called = true; + } + }); + + setTimeout(function () { + expect(called).toBe(true); + done(); + }, 100); + }); +}); + diff --git a/axios/test/specs/api.spec.js b/axios/test/specs/api.spec.js new file mode 100644 index 0000000..eaeadfa --- /dev/null +++ b/axios/test/specs/api.spec.js @@ -0,0 +1,68 @@ +describe('static api', function () { + it('should have request method helpers', function () { + expect(typeof axios.request).toEqual('function'); + expect(typeof axios.get).toEqual('function'); + expect(typeof axios.head).toEqual('function'); + expect(typeof axios.options).toEqual('function'); + expect(typeof axios.delete).toEqual('function'); + expect(typeof axios.post).toEqual('function'); + expect(typeof axios.put).toEqual('function'); + expect(typeof axios.patch).toEqual('function'); + }); + + it('should have promise method helpers', function () { + var promise = axios(); + + expect(typeof promise.then).toEqual('function'); + expect(typeof promise.catch).toEqual('function'); + }); + + it('should have defaults', function () { + expect(typeof axios.defaults).toEqual('object'); + expect(typeof axios.defaults.headers).toEqual('object'); + }); + + it('should have interceptors', function () { + expect(typeof axios.interceptors.request).toEqual('object'); + expect(typeof axios.interceptors.response).toEqual('object'); + }); + + it('should have all/spread helpers', function () { + expect(typeof axios.all).toEqual('function'); + expect(typeof axios.spread).toEqual('function'); + }); + + it('should have factory method', function () { + expect(typeof axios.create).toEqual('function'); + }); + + it('should have Cancel, CancelToken, and isCancel properties', function () { + expect(typeof axios.Cancel).toEqual('function'); + expect(typeof axios.CancelToken).toEqual('function'); + expect(typeof axios.isCancel).toEqual('function'); + }); + + it('should have isAxiosError properties', function () { + expect(typeof axios.isAxiosError).toEqual('function'); + }); +}); + +describe('instance api', function () { + var instance = axios.create(); + + it('should have request methods', function () { + expect(typeof instance.request).toEqual('function'); + expect(typeof instance.get).toEqual('function'); + expect(typeof instance.options).toEqual('function'); + expect(typeof instance.head).toEqual('function'); + expect(typeof instance.delete).toEqual('function'); + expect(typeof instance.post).toEqual('function'); + expect(typeof instance.put).toEqual('function'); + expect(typeof instance.patch).toEqual('function'); + }); + + it('should have interceptors', function () { + expect(typeof instance.interceptors.request).toEqual('object'); + expect(typeof instance.interceptors.response).toEqual('object'); + }); +}); diff --git a/axios/test/specs/basicAuth.spec.js b/axios/test/specs/basicAuth.spec.js new file mode 100644 index 0000000..8c10117 --- /dev/null +++ b/axios/test/specs/basicAuth.spec.js @@ -0,0 +1,3 @@ +describe('basicAuth', function () { + setupBasicAuthTest(); +}); diff --git a/axios/test/specs/cancel.spec.js b/axios/test/specs/cancel.spec.js new file mode 100644 index 0000000..42b2b4e --- /dev/null +++ b/axios/test/specs/cancel.spec.js @@ -0,0 +1,89 @@ +var Cancel = axios.Cancel; +var CancelToken = axios.CancelToken; + +describe('cancel', function() { + beforeEach(function() { + jasmine.Ajax.install(); + }); + + afterEach(function() { + jasmine.Ajax.uninstall(); + }); + + describe('when called before sending request', function() { + it('rejects Promise with a Cancel object', function (done) { + var source = CancelToken.source(); + source.cancel('Operation has been canceled.'); + axios.get('/foo', { + cancelToken: source.token + }).catch(function (thrown) { + expect(thrown).toEqual(jasmine.any(Cancel)); + expect(thrown.message).toBe('Operation has been canceled.'); + done(); + }); + }); + }); + + describe('when called after request has been sent', function() { + it('rejects Promise with a Cancel object', function (done) { + var source = CancelToken.source(); + axios.get('/foo/bar', { + cancelToken: source.token + }).catch(function (thrown) { + expect(thrown).toEqual(jasmine.any(Cancel)); + expect(thrown.message).toBe('Operation has been canceled.'); + done(); + }); + + getAjaxRequest().then(function (request) { + // call cancel() when the request has been sent, but a response has not been received + source.cancel('Operation has been canceled.'); + request.respondWith({ + status: 200, + responseText: 'OK' + }); + }); + }); + + it('calls abort on request object', function (done) { + var source = CancelToken.source(); + var request; + axios.get('/foo/bar', { + cancelToken: source.token + }).catch(function() { + // jasmine-ajax sets statusText to 'abort' when request.abort() is called + expect(request.statusText).toBe('abort'); + done(); + }); + + getAjaxRequest().then(function (req) { + // call cancel() when the request has been sent, but a response has not been received + source.cancel(); + request = req; + }); + }); + }); + + describe('when called after response has been received', function() { + // https://github.com/axios/axios/issues/482 + it('does not cause unhandled rejection', function (done) { + var source = CancelToken.source(); + axios.get('/foo', { + cancelToken: source.token + }).then(function () { + window.addEventListener('unhandledrejection', function () { + done.fail('Unhandled rejection.'); + }); + source.cancel(); + setTimeout(done, 100); + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: 'OK' + }); + }); + }); + }); +}); diff --git a/axios/test/specs/cancel/Cancel.spec.js b/axios/test/specs/cancel/Cancel.spec.js new file mode 100644 index 0000000..0e0de80 --- /dev/null +++ b/axios/test/specs/cancel/Cancel.spec.js @@ -0,0 +1,15 @@ +var Cancel = require('../../../lib/cancel/Cancel'); + +describe('Cancel', function() { + describe('toString', function() { + it('returns correct result when message is not specified', function() { + var cancel = new Cancel(); + expect(cancel.toString()).toBe('Cancel'); + }); + + it('returns correct result when message is specified', function() { + var cancel = new Cancel('Operation has been canceled.'); + expect(cancel.toString()).toBe('Cancel: Operation has been canceled.'); + }); + }); +}); diff --git a/axios/test/specs/cancel/CancelToken.spec.js b/axios/test/specs/cancel/CancelToken.spec.js new file mode 100644 index 0000000..dd72327 --- /dev/null +++ b/axios/test/specs/cancel/CancelToken.spec.js @@ -0,0 +1,87 @@ +var CancelToken = require('../../../lib/cancel/CancelToken'); +var Cancel = require('../../../lib/cancel/Cancel'); + +describe('CancelToken', function() { + describe('constructor', function() { + it('throws when executor is not specified', function() { + expect(function() { + new CancelToken(); + }).toThrowError(TypeError, 'executor must be a function.'); + }); + + it('throws when executor is not a function', function() { + expect(function() { + new CancelToken(123); + }).toThrowError(TypeError, 'executor must be a function.'); + }); + }); + + describe('reason', function() { + it('returns a Cancel if cancellation has been requested', function() { + var cancel; + var token = new CancelToken(function(c) { + cancel = c; + }); + cancel('Operation has been canceled.'); + expect(token.reason).toEqual(jasmine.any(Cancel)); + expect(token.reason.message).toBe('Operation has been canceled.'); + }); + + it('returns undefined if cancellation has not been requested', function() { + var token = new CancelToken(function() {}); + expect(token.reason).toBeUndefined(); + }); + }); + + describe('promise', function() { + it('returns a Promise that resolves when cancellation is requested', function(done) { + var cancel; + var token = new CancelToken(function(c) { + cancel = c; + }); + token.promise.then(function onFulfilled(value) { + expect(value).toEqual(jasmine.any(Cancel)); + expect(value.message).toBe('Operation has been canceled.'); + done(); + }); + cancel('Operation has been canceled.'); + }); + }); + + describe('throwIfRequested', function() { + it('throws if cancellation has been requested', function() { + // Note: we cannot use expect.toThrowError here as Cancel does not inherit from Error + var cancel; + var token = new CancelToken(function(c) { + cancel = c; + }); + cancel('Operation has been canceled.'); + try { + token.throwIfRequested(); + fail('Expected throwIfRequested to throw.'); + } catch (thrown) { + if (!(thrown instanceof Cancel)) { + fail('Expected throwIfRequested to throw a Cancel, but it threw ' + thrown + '.'); + } + expect(thrown.message).toBe('Operation has been canceled.'); + } + }); + + it('does not throw if cancellation has not been requested', function() { + var token = new CancelToken(function() {}); + token.throwIfRequested(); + }); + }); + + describe('source', function() { + it('returns an object containing token and cancel function', function() { + var source = CancelToken.source(); + expect(source.token).toEqual(jasmine.any(CancelToken)); + expect(source.cancel).toEqual(jasmine.any(Function)); + expect(source.token.reason).toBeUndefined(); + source.cancel('Operation has been canceled.'); + expect(source.token.reason).toEqual(jasmine.any(Cancel)); + expect(source.token.reason.message).toBe('Operation has been canceled.'); + }); + }); +}); diff --git a/axios/test/specs/cancel/isCancel.spec.js b/axios/test/specs/cancel/isCancel.spec.js new file mode 100644 index 0000000..e6be40d --- /dev/null +++ b/axios/test/specs/cancel/isCancel.spec.js @@ -0,0 +1,12 @@ +var isCancel = require('../../../lib/cancel/isCancel'); +var Cancel = require('../../../lib/cancel/Cancel'); + +describe('isCancel', function() { + it('returns true if value is a Cancel', function() { + expect(isCancel(new Cancel())).toBe(true); + }); + + it('returns false if value is not a Cancel', function() { + expect(isCancel({ foo: 'bar' })).toBe(false); + }); +}); diff --git a/axios/test/specs/core/buildFullPath.spec.js b/axios/test/specs/core/buildFullPath.spec.js new file mode 100644 index 0000000..e7a64d3 --- /dev/null +++ b/axios/test/specs/core/buildFullPath.spec.js @@ -0,0 +1,20 @@ +var buildFullPath = require('../../../lib/core/buildFullPath'); + +describe('helpers::buildFullPath', function () { + it('should combine URLs when the requestedURL is relative', function () { + expect(buildFullPath('https://api.github.com', '/users')).toBe('https://api.github.com/users'); + }); + + it('should return the requestedURL when it is absolute', function () { + expect(buildFullPath('https://api.github.com', 'https://api.example.com/users')).toBe('https://api.example.com/users'); + }); + + it('should not combine URLs when the baseURL is not configured', function () { + expect(buildFullPath(undefined, '/users')).toBe('/users'); + }); + + it('should combine URLs when the baseURL and requestedURL are relative', function () { + expect(buildFullPath('/api', '/users')).toBe('/api/users'); + }); + +}); diff --git a/axios/test/specs/core/createError.spec.js b/axios/test/specs/core/createError.spec.js new file mode 100644 index 0000000..53f708b --- /dev/null +++ b/axios/test/specs/core/createError.spec.js @@ -0,0 +1,29 @@ +var createError = require('../../../lib/core/createError'); + +describe('core::createError', function() { + it('should create an Error with message, config, code, request, response and isAxiosError', function() { + var request = { path: '/foo' }; + var response = { status: 200, data: { foo: 'bar' } }; + var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING', request, response); + expect(error instanceof Error).toBe(true); + expect(error.message).toBe('Boom!'); + expect(error.config).toEqual({ foo: 'bar' }); + expect(error.code).toBe('ESOMETHING'); + expect(error.request).toBe(request); + expect(error.response).toBe(response); + expect(error.isAxiosError).toBe(true); + }); + it('should create an Error that can be serialized to JSON', function() { + // Attempting to serialize request and response results in + // TypeError: Converting circular structure to JSON + var request = { path: '/foo' }; + var response = { status: 200, data: { foo: 'bar' } }; + var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING', request, response); + var json = error.toJSON(); + expect(json.message).toBe('Boom!'); + expect(json.config).toEqual({ foo: 'bar' }); + expect(json.code).toBe('ESOMETHING'); + expect(json.request).toBe(undefined); + expect(json.response).toBe(undefined); + }); +}); diff --git a/axios/test/specs/core/enhanceError.spec.js b/axios/test/specs/core/enhanceError.spec.js new file mode 100644 index 0000000..f8e24d1 --- /dev/null +++ b/axios/test/specs/core/enhanceError.spec.js @@ -0,0 +1,21 @@ +var enhanceError = require('../../../lib/core/enhanceError'); + +describe('core::enhanceError', function() { + it('should add config, config, request and response to error', function() { + var error = new Error('Boom!'); + var request = { path: '/foo' }; + var response = { status: 200, data: { foo: 'bar' } }; + + enhanceError(error, { foo: 'bar' }, 'ESOMETHING', request, response); + expect(error.config).toEqual({ foo: 'bar' }); + expect(error.code).toBe('ESOMETHING'); + expect(error.request).toBe(request); + expect(error.response).toBe(response); + expect(error.isAxiosError).toBe(true); + }); + + it('should return error', function() { + var error = new Error('Boom!'); + expect(enhanceError(error, { foo: 'bar' }, 'ESOMETHING')).toBe(error); + }); +}); diff --git a/axios/test/specs/core/mergeConfig.spec.js b/axios/test/specs/core/mergeConfig.spec.js new file mode 100644 index 0000000..cc8ab51 --- /dev/null +++ b/axios/test/specs/core/mergeConfig.spec.js @@ -0,0 +1,310 @@ +var defaults = require('../../../lib/defaults'); +var mergeConfig = require('../../../lib/core/mergeConfig'); + +describe('core::mergeConfig', function() { + it('should accept undefined for second argument', function() { + expect(mergeConfig(defaults, undefined)).toEqual(defaults); + }); + + it('should accept an object for second argument', function() { + expect(mergeConfig(defaults, {})).toEqual(defaults); + }); + + it('should not leave references', function() { + var merged = mergeConfig(defaults, {}); + expect(merged).not.toBe(defaults); + expect(merged.headers).not.toBe(defaults.headers); + }); + + it('should allow setting request options', function() { + var config = { + url: '__sample url__', + method: '__sample method__', + params: '__sample params__', + data: { foo: true } + }; + var merged = mergeConfig(defaults, config); + expect(merged.url).toEqual(config.url); + expect(merged.method).toEqual(config.method); + expect(merged.params).toEqual(config.params); + expect(merged.data).toEqual(config.data); + }); + + it('should not inherit request options', function() { + var localDefaults = { + method: '__sample method__', + data: { foo: true } + }; + var merged = mergeConfig(localDefaults, {}); + expect(merged.method).toEqual(undefined); + expect(merged.data).toEqual(undefined); + }); + + ['auth', 'headers', 'params', 'proxy'].forEach(function(key) { + it('should set new config for' + key + ' without default', function() { + var a = {}, b = {}, c = {} + a[key] = undefined + b[key] = { user: 'foo', pass: 'test' } + c[key] = { user: 'foo', pass: 'test' } + + expect(mergeConfig(a, b)).toEqual(c); + }); + + it('should merge ' + key + ' with defaults', function() { + var a = {}, b = {}, c = {}; + a[key] = { user: 'foo', pass: 'bar' }; + b[key] = { pass: 'test' }; + c[key] = { user: 'foo', pass: 'test' }; + + expect(mergeConfig(a, b)).toEqual(c); + }); + + it('should overwrite default ' + key + ' with a non-object value', function() { + [false, null, 123].forEach(function(value) { + var a = {}, b = {}, c = {}; + a[key] = { user: 'foo', pass: 'test' }; + b[key] = value; + c[key] = value; + + expect(mergeConfig(a, b)).toEqual(c); + }); + }); + }); + + it('should allow setting other options', function() { + var merged = mergeConfig(defaults, { timeout: 123 }); + expect(merged.timeout).toEqual(123); + }); + + it('should allow setting custom options', function() { + var merged = mergeConfig(defaults, { foo: 'bar' }); + expect(merged.foo).toEqual('bar'); + }); + + it('should allow setting custom default options', function() { + var merged = mergeConfig({ foo: 'bar' }, {}); + expect(merged.foo).toEqual('bar'); + }); + + it('should allow merging custom objects in the config', function() { + var merged = mergeConfig({ + nestedConfig: { + propertyOnDefaultConfig: true + } + }, { + nestedConfig: { + propertyOnRequestConfig: true + } + }); + expect(merged.nestedConfig.propertyOnDefaultConfig).toEqual(true); + expect(merged.nestedConfig.propertyOnRequestConfig).toEqual(true); + }); + + describe('valueFromConfig2Keys', function() { + var config1 = {url: '/foo', method: 'post', data: {a: 3}}; + + it('should skip if config2 is undefined', function() { + expect(mergeConfig(config1, {})).toEqual({}); + }); + + it('should clone config2 if is plain object', function() { + var data = {a: 1, b: 2}; + var merged = mergeConfig(config1, {data: data}); + expect(merged.data).toEqual(data); + expect(merged.data).not.toBe(data); + }); + + it('should clone config2 if is array', function() { + var data = [1, 2, 3]; + var merged = mergeConfig(config1, {data: data}); + expect(merged.data).toEqual(data); + expect(merged.data).not.toBe(data); + }); + + it('should set as config2 in other cases', function() { + var obj = Object.create({}); + expect(mergeConfig(config1, {data: 1}).data).toBe(1); + expect(mergeConfig(config1, {data: 'str'}).data).toBe('str'); + expect(mergeConfig(config1, {data: obj}).data).toBe(obj); + expect(mergeConfig(config1, {data: null}).data).toBe(null); + }); + }); + + describe('mergeDeepPropertiesKeys', function() { + it('should skip if both config1 and config2 are undefined', function() { + expect(mergeConfig({headers: undefined}, {headers: undefined})).toEqual({}); + }); + + it('should merge if both config1 and config2 are plain object', function() { + expect(mergeConfig({headers: {a: 1, b: 1}}, {headers: {b: 2, c: 2}})) + .toEqual({headers: {a: 1, b: 2, c: 2}}); + }); + + it('should clone config2 if is plain object', function() { + var config1 = {headers: [1, 2, 3]}; + var config2 = {headers: {a: 1, b: 2}}; + var merged = mergeConfig(config1, config2); + expect(merged.headers).toEqual(config2.headers); + expect(merged.headers).not.toBe(config2.headers); + }); + + it('should clone config2 if is array', function() { + var config1 = {headers: {a: 1, b: 1}}; + var config2 = {headers: [1, 2, 3]}; + var merged = mergeConfig(config1, config2); + expect(merged.headers).toEqual(config2.headers); + expect(merged.headers).not.toBe(config2.headers); + }); + + it('should set as config2 in other cases', function() { + var config1 = {headers: {a: 1, b: 1}}; + var obj = Object.create({}); + expect(mergeConfig(config1, {headers: 1}).headers).toBe(1); + expect(mergeConfig(config1, {headers: 'str'}).headers).toBe('str'); + expect(mergeConfig(config1, {headers: obj}).headers).toBe(obj); + expect(mergeConfig(config1, {headers: null}).headers).toBe(null); + }); + + it('should clone config1 if is plain object', function() { + var config1 = {headers: {a: 1, b: 2}}; + var config2 = {}; + var merged = mergeConfig(config1, config2); + expect(merged.headers).toEqual(config1.headers); + expect(merged.headers).not.toBe(config1.headers); + }); + + it('should clone config1 if is array', function() { + var config1 = {headers: [1, 2, 3]}; + var config2 = {}; + var merged = mergeConfig(config1, config2); + expect(merged.headers).toEqual(config1.headers); + expect(merged.headers).not.toBe(config1.headers); + }); + + it('should set as config1 in other cases', function() { + var config2 = {}; + var obj = Object.create({}); + expect(mergeConfig({headers: 1}, config2).headers).toBe(1); + expect(mergeConfig({headers: 'str'}, config2).headers).toBe('str'); + expect(mergeConfig({headers: obj}, config2).headers).toBe(obj); + expect(mergeConfig({headers: null}, config2).headers).toBe(null); + }); + }); + + describe('defaultToConfig2Keys', function() { + it('should skip if both config1 and config2 are undefined', function() { + expect(mergeConfig({transformRequest: undefined}, {transformRequest: undefined})).toEqual({}); + }); + + it('should clone config2 if both config1 and config2 are plain object', function() { + var config1 = {transformRequest: {a: 1, b: 1}}; + var config2 = {transformRequest: {b: 2, c: 2}}; + var merged = mergeConfig(config1, config2); + expect(merged.transformRequest).toEqual(config2.transformRequest); + expect(merged.transformRequest).not.toBe(config2.transformRequest); + }); + + it('should clone config2 if is array', function() { + var config1 = {transformRequest: {a: 1, b: 1}}; + var config2 = {transformRequest: [1, 2, 3]}; + var merged = mergeConfig(config1, config2); + expect(merged.transformRequest).toEqual(config2.transformRequest); + expect(merged.transformRequest).not.toBe(config2.transformRequest); + }); + + it('should set as config2 in other cases', function() { + var config1 = {transformRequest: {a: 1, b: 1}}; + var obj = Object.create({}); + expect(mergeConfig(config1, {transformRequest: 1}).transformRequest).toBe(1); + expect(mergeConfig(config1, {transformRequest: 'str'}).transformRequest).toBe('str'); + expect(mergeConfig(config1, {transformRequest: obj}).transformRequest).toBe(obj); + expect(mergeConfig(config1, {transformRequest: null}).transformRequest).toBe(null); + }); + + it('should clone config1 if is plain object', function() { + var config1 = {transformRequest: {a: 1, b: 2}}; + var config2 = {}; + var merged = mergeConfig(config1, config2); + expect(merged.transformRequest).toEqual(config1.transformRequest); + expect(merged.transformRequest).not.toBe(config1.transformRequest); + }); + + it('should clone config1 if is array', function() { + var config1 = {transformRequest: [1, 2, 3]}; + var config2 = {}; + var merged = mergeConfig(config1, config2); + expect(merged.transformRequest).toEqual(config1.transformRequest); + expect(merged.transformRequest).not.toBe(config1.transformRequest); + }); + + it('should set as config1 in other cases', function() { + var config2 = {}; + var obj = Object.create({}); + expect(mergeConfig({transformRequest: 1}, config2).transformRequest).toBe(1); + expect(mergeConfig({transformRequest: 'str'}, config2).transformRequest).toBe('str'); + expect(mergeConfig({transformRequest: obj}, config2).transformRequest).toBe(obj); + expect(mergeConfig({transformRequest: null}, config2).transformRequest).toBe(null); + }); + }); + + describe('directMergeKeys', function() { + it('should merge if config2 in keys', function() { + expect(mergeConfig({}, {validateStatus: undefined})).toEqual({validateStatus: undefined}); + }); + + it('should merge if both config1 and config2 are plain object', function() { + expect(mergeConfig({validateStatus: {a: 1, b: 1}}, {validateStatus: {b: 2, c: 2}})) + .toEqual({validateStatus: {a: 1, b: 2, c: 2}}); + }); + + it('should clone config2 if is plain object', function() { + var config1 = {validateStatus: [1, 2, 3]}; + var config2 = {validateStatus: {a: 1, b: 2}}; + var merged = mergeConfig(config1, config2); + expect(merged.validateStatus).toEqual(config2.validateStatus); + expect(merged.validateStatus).not.toBe(config2.validateStatus); + }); + + it('should clone config2 if is array', function() { + var config1 = {validateStatus: {a: 1, b: 2}}; + var config2 = {validateStatus: [1, 2, 3]}; + var merged = mergeConfig(config1, config2); + expect(merged.validateStatus).toEqual(config2.validateStatus); + expect(merged.validateStatus).not.toBe(config2.validateStatus); + }); + + it('should set as config2 in other cases', function() { + var config1 = {validateStatus: {a: 1, b: 2}}; + var obj = Object.create({}); + expect(mergeConfig(config1, {validateStatus: 1}).validateStatus).toBe(1); + expect(mergeConfig(config1, {validateStatus: 'str'}).validateStatus).toBe('str'); + expect(mergeConfig(config1, {validateStatus: obj}).validateStatus).toBe(obj); + expect(mergeConfig(config1, {validateStatus: null}).validateStatus).toBe(null); + }); + + it('should clone config1 if is plain object', function() { + var config1 = {validateStatus: {a: 1, b: 2}}; + var config2 = {}; + var merged = mergeConfig(config1, config2); + expect(merged.validateStatus).toEqual(config1.validateStatus); + expect(merged.validateStatus).not.toBe(config1.validateStatus); + }); + + it('should clone config1 if is array', function() { + var config1 = {validateStatus: [1, 2, 3]}; + var config2 = {}; + var merged = mergeConfig(config1, config2); + expect(merged.validateStatus).toEqual(config1.validateStatus); + expect(merged.validateStatus).not.toBe(config1.validateStatus); + }); + + it('should set as config1 in other cases', function() { + var config2 = {}; + var obj = Object.create({}); + expect(mergeConfig({validateStatus: 1}, config2).validateStatus).toBe(1); + expect(mergeConfig({validateStatus: 'str'}, config2).validateStatus).toBe('str'); + expect(mergeConfig({validateStatus: obj}, config2).validateStatus).toBe(obj); + expect(mergeConfig({validateStatus: null}, config2).validateStatus).toBe(null); + }); + }); +}); diff --git a/axios/test/specs/core/settle.spec.js b/axios/test/specs/core/settle.spec.js new file mode 100644 index 0000000..914bb9b --- /dev/null +++ b/axios/test/specs/core/settle.spec.js @@ -0,0 +1,85 @@ +var settle = require('../../../lib/core/settle'); + +describe('core::settle', function() { + var resolve; + var reject; + + beforeEach(function() { + resolve = jasmine.createSpy('resolve'); + reject = jasmine.createSpy('reject'); + }); + + it('should resolve promise if status is not set', function() { + var response = { + config: { + validateStatus: function() { + return true; + } + } + }; + settle(resolve, reject, response); + expect(resolve).toHaveBeenCalledWith(response); + expect(reject).not.toHaveBeenCalled(); + }); + + it('should resolve promise if validateStatus is not set', function() { + var response = { + status: 500, + config: { + } + }; + settle(resolve, reject, response); + expect(resolve).toHaveBeenCalledWith(response); + expect(reject).not.toHaveBeenCalled(); + }); + + it('should resolve promise if validateStatus returns true', function() { + var response = { + status: 500, + config: { + validateStatus: function() { + return true; + } + } + }; + settle(resolve, reject, response); + expect(resolve).toHaveBeenCalledWith(response); + expect(reject).not.toHaveBeenCalled(); + }); + + it('should reject promise if validateStatus returns false', function() { + var req = { + path: '/foo' + }; + var response = { + status: 500, + config: { + validateStatus: function() { + return false; + } + }, + request: req + }; + settle(resolve, reject, response); + expect(resolve).not.toHaveBeenCalled(); + expect(reject).toHaveBeenCalled(); + var reason = reject.calls.first().args[0]; + expect(reason instanceof Error).toBe(true); + expect(reason.message).toBe('Request failed with status code 500'); + expect(reason.config).toBe(response.config); + expect(reason.request).toBe(req); + expect(reason.response).toBe(response); + }); + + it('should pass status to validateStatus', function() { + var validateStatus = jasmine.createSpy('validateStatus'); + var response = { + status: 500, + config: { + validateStatus: validateStatus + } + }; + settle(resolve, reject, response); + expect(validateStatus).toHaveBeenCalledWith(500); + }); +}); diff --git a/axios/test/specs/core/transformData.spec.js b/axios/test/specs/core/transformData.spec.js new file mode 100644 index 0000000..95479bd --- /dev/null +++ b/axios/test/specs/core/transformData.spec.js @@ -0,0 +1,30 @@ +var transformData = require('../../../lib/core/transformData'); + +describe('core::transformData', function () { + it('should support a single transformer', function () { + var data; + data = transformData(data, null, function (data) { + data = 'foo'; + return data; + }); + + expect(data).toEqual('foo'); + }); + + it('should support an array of transformers', function () { + var data = ''; + data = transformData(data, null, [function (data) { + data += 'f'; + return data; + }, function (data) { + data += 'o'; + return data; + }, function (data) { + data += 'o'; + return data; + }]); + + expect(data).toEqual('foo'); + }); +}); + diff --git a/axios/test/specs/defaults.spec.js b/axios/test/specs/defaults.spec.js new file mode 100644 index 0000000..c8ee72e --- /dev/null +++ b/axios/test/specs/defaults.spec.js @@ -0,0 +1,162 @@ +var defaults = require('../../lib/defaults'); +var utils = require('../../lib/utils'); + +describe('defaults', function () { + var XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN'; + + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + delete axios.defaults.baseURL; + delete axios.defaults.headers.get['X-CUSTOM-HEADER']; + delete axios.defaults.headers.post['X-CUSTOM-HEADER']; + document.cookie = XSRF_COOKIE_NAME + '=;expires=' + new Date(Date.now() - 86400000).toGMTString(); + }); + + it('should transform request json', function () { + expect(defaults.transformRequest[0]({foo: 'bar'})).toEqual('{"foo":"bar"}'); + }); + + it('should do nothing to request string', function () { + expect(defaults.transformRequest[0]('foo=bar')).toEqual('foo=bar'); + }); + + it('should transform response json', function () { + var data = defaults.transformResponse[0]('{"foo":"bar"}'); + + expect(typeof data).toEqual('object'); + expect(data.foo).toEqual('bar'); + }); + + it('should do nothing to response string', function () { + expect(defaults.transformResponse[0]('foo=bar')).toEqual('foo=bar'); + }); + + it('should use global defaults config', function (done) { + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + done(); + }); + }); + + it('should use modified defaults config', function (done) { + axios.defaults.baseURL = 'http://example.com/'; + + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://example.com/foo'); + done(); + }); + }); + + it('should use request config', function (done) { + axios('/foo', { + baseURL: 'http://www.example.com' + }); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://www.example.com/foo'); + done(); + }); + }); + + it('should use default config for custom instance', function (done) { + var instance = axios.create({ + xsrfCookieName: XSRF_COOKIE_NAME, + xsrfHeaderName: 'X-CUSTOM-XSRF-TOKEN' + }); + document.cookie = instance.defaults.xsrfCookieName + '=foobarbaz'; + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders[instance.defaults.xsrfHeaderName]).toEqual('foobarbaz'); + done(); + }); + }); + + it('should use GET headers', function (done) { + axios.defaults.headers.get['X-CUSTOM-HEADER'] = 'foo'; + axios.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders['X-CUSTOM-HEADER']).toBe('foo'); + done(); + }); + }); + + it('should use POST headers', function (done) { + axios.defaults.headers.post['X-CUSTOM-HEADER'] = 'foo'; + axios.post('/foo', {}); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders['X-CUSTOM-HEADER']).toBe('foo'); + done(); + }); + }); + + it('should use header config', function (done) { + var instance = axios.create({ + headers: { + common: { + 'X-COMMON-HEADER': 'commonHeaderValue' + }, + get: { + 'X-GET-HEADER': 'getHeaderValue' + }, + post: { + 'X-POST-HEADER': 'postHeaderValue' + } + } + }); + + instance.get('/foo', { + headers: { + 'X-FOO-HEADER': 'fooHeaderValue', + 'X-BAR-HEADER': 'barHeaderValue' + } + }); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders).toEqual( + utils.merge(defaults.headers.common, defaults.headers.get, { + 'X-COMMON-HEADER': 'commonHeaderValue', + 'X-GET-HEADER': 'getHeaderValue', + 'X-FOO-HEADER': 'fooHeaderValue', + 'X-BAR-HEADER': 'barHeaderValue' + }) + ); + done(); + }); + }); + + it('should be used by custom instance if set before instance created', function (done) { + axios.defaults.baseURL = 'http://example.org/'; + var instance = axios.create(); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://example.org/foo'); + done(); + }); + }); + + it('should not be used by custom instance if set after instance created', function (done) { + var instance = axios.create(); + axios.defaults.baseURL = 'http://example.org/'; + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + done(); + }); + }); +}); diff --git a/axios/test/specs/headers.spec.js b/axios/test/specs/headers.spec.js new file mode 100644 index 0000000..c9c768d --- /dev/null +++ b/axios/test/specs/headers.spec.js @@ -0,0 +1,115 @@ +function testHeaderValue(headers, key, val) { + var found = false; + + for (var k in headers) { + if (k.toLowerCase() === key.toLowerCase()) { + found = true; + expect(headers[k]).toEqual(val); + break; + } + } + + if (!found) { + if (typeof val === 'undefined') { + expect(headers.hasOwnProperty(key)).toEqual(false); + } else { + throw new Error(key + ' was not found in headers'); + } + } +} + +describe('headers', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should default common headers', function (done) { + var headers = axios.defaults.headers.common; + + axios('/foo'); + + getAjaxRequest().then(function (request) { + for (var key in headers) { + if (headers.hasOwnProperty(key)) { + expect(request.requestHeaders[key]).toEqual(headers[key]); + } + } + done(); + }); + }); + + it('should add extra headers for post', function (done) { + var headers = axios.defaults.headers.common; + + axios.post('/foo', 'fizz=buzz'); + + getAjaxRequest().then(function (request) { + for (var key in headers) { + if (headers.hasOwnProperty(key)) { + expect(request.requestHeaders[key]).toEqual(headers[key]); + } + } + done(); + }); + }); + + it('should reset headers by null or explicit undefined', function (done) { + axios.create({ + headers: { + common: { + 'x-header-a': 'a', + 'x-header-b': 'b', + 'x-header-c': 'c' + } + } + }).post('/foo', {fizz: 'buzz'}, { + headers: { + 'Content-Type': null, + 'x-header-a': null, + 'x-header-b': undefined + } + }); + + getAjaxRequest().then(function (request) { + testHeaderValue(request.requestHeaders, 'Content-Type', null); + testHeaderValue(request.requestHeaders, 'x-header-a', null); + testHeaderValue(request.requestHeaders, 'x-header-b', undefined); + testHeaderValue(request.requestHeaders, 'x-header-c', 'c'); + done(); + }); + }); + + it('should use application/json when posting an object', function (done) { + axios.post('/foo/bar', { + firstName: 'foo', + lastName: 'bar' + }); + + getAjaxRequest().then(function (request) { + testHeaderValue(request.requestHeaders, 'Content-Type', 'application/json;charset=utf-8'); + done(); + }); + }); + + it('should remove content-type if data is empty', function (done) { + axios.post('/foo'); + + getAjaxRequest().then(function (request) { + testHeaderValue(request.requestHeaders, 'Content-Type', undefined); + done(); + }); + }); + + it('should preserve content-type if data is false', function (done) { + axios.post('/foo', false); + + getAjaxRequest().then(function (request) { + testHeaderValue(request.requestHeaders, 'Content-Type', 'application/x-www-form-urlencoded'); + done(); + }); + }); +}); diff --git a/axios/test/specs/helpers/bind.spec.js b/axios/test/specs/helpers/bind.spec.js new file mode 100644 index 0000000..a02c462 --- /dev/null +++ b/axios/test/specs/helpers/bind.spec.js @@ -0,0 +1,12 @@ +var bind = require('../../../lib/helpers/bind'); + +describe('bind', function () { + it('should bind an object to a function', function () { + var o = { val: 123 }; + var f = bind(function (num) { + return this.val * num; + }, o); + + expect(f(2)).toEqual(246); + }); +}); diff --git a/axios/test/specs/helpers/buildURL.spec.js b/axios/test/specs/helpers/buildURL.spec.js new file mode 100644 index 0000000..7adf574 --- /dev/null +++ b/axios/test/specs/helpers/buildURL.spec.js @@ -0,0 +1,75 @@ +var buildURL = require('../../../lib/helpers/buildURL'); +var URLSearchParams = require('url-search-params'); + +describe('helpers::buildURL', function () { + it('should support null params', function () { + expect(buildURL('/foo')).toEqual('/foo'); + }); + + it('should support params', function () { + expect(buildURL('/foo', { + foo: 'bar' + })).toEqual('/foo?foo=bar'); + }); + + it('should support object params', function () { + expect(buildURL('/foo', { + foo: { + bar: 'baz' + } + })).toEqual('/foo?foo=' + encodeURI('{"bar":"baz"}')); + }); + + it('should support date params', function () { + var date = new Date(); + + expect(buildURL('/foo', { + date: date + })).toEqual('/foo?date=' + date.toISOString()); + }); + + it('should support array params', function () { + expect(buildURL('/foo', { + foo: ['bar', 'baz'] + })).toEqual('/foo?foo[]=bar&foo[]=baz'); + }); + + it('should support special char params', function () { + expect(buildURL('/foo', { + foo: ':$, ' + })).toEqual('/foo?foo=:$,+'); + }); + + it('should support existing params', function () { + expect(buildURL('/foo?foo=bar', { + bar: 'baz' + })).toEqual('/foo?foo=bar&bar=baz'); + }); + + it('should support "length" parameter', function () { + expect(buildURL('/foo', { + query: 'bar', + start: 0, + length: 5 + })).toEqual('/foo?query=bar&start=0&length=5'); + }); + + it('should correct discard url hash mark', function () { + expect(buildURL('/foo?foo=bar#hash', { + query: 'baz' + })).toEqual('/foo?foo=bar&query=baz'); + }); + + it('should use serializer if provided', function () { + serializer = sinon.stub(); + params = {foo: 'bar'}; + serializer.returns('foo=bar'); + expect(buildURL('/foo', params, serializer)).toEqual('/foo?foo=bar'); + expect(serializer.calledOnce).toBe(true); + expect(serializer.calledWith(params)).toBe(true); + }); + + it('should support URLSearchParams', function () { + expect(buildURL('/foo', new URLSearchParams('bar=baz'))).toEqual('/foo?bar=baz'); + }); +}); diff --git a/axios/test/specs/helpers/combineURLs.spec.js b/axios/test/specs/helpers/combineURLs.spec.js new file mode 100644 index 0000000..2a427f5 --- /dev/null +++ b/axios/test/specs/helpers/combineURLs.spec.js @@ -0,0 +1,23 @@ +var combineURLs = require('../../../lib/helpers/combineURLs'); + +describe('helpers::combineURLs', function () { + it('should combine URLs', function () { + expect(combineURLs('https://api.github.com', '/users')).toBe('https://api.github.com/users'); + }); + + it('should remove duplicate slashes', function () { + expect(combineURLs('https://api.github.com/', '/users')).toBe('https://api.github.com/users'); + }); + + it('should insert missing slash', function () { + expect(combineURLs('https://api.github.com', 'users')).toBe('https://api.github.com/users'); + }); + + it('should not insert slash when relative url missing/empty', function () { + expect(combineURLs('https://api.github.com/users', '')).toBe('https://api.github.com/users'); + }); + + it('should allow a single slash for relative url', function () { + expect(combineURLs('https://api.github.com/users', '/')).toBe('https://api.github.com/users/'); + }); +}); diff --git a/axios/test/specs/helpers/cookies.spec.js b/axios/test/specs/helpers/cookies.spec.js new file mode 100644 index 0000000..9c3880d --- /dev/null +++ b/axios/test/specs/helpers/cookies.spec.js @@ -0,0 +1,36 @@ +var cookies = require('../../../lib/helpers/cookies'); + +describe('helpers::cookies', function () { + afterEach(function () { + // Remove all the cookies + var expires = Date.now() - (60 * 60 * 24 * 7); + document.cookie.split(';').map(function (cookie) { + return cookie.split('=')[0]; + }).forEach(function (name) { + document.cookie = name + '=; expires=' + new Date(expires).toGMTString(); + }); + }); + + it('should write cookies', function () { + cookies.write('foo', 'baz'); + expect(document.cookie).toEqual('foo=baz'); + }); + + it('should read cookies', function () { + cookies.write('foo', 'abc'); + cookies.write('bar', 'def'); + expect(cookies.read('foo')).toEqual('abc'); + expect(cookies.read('bar')).toEqual('def'); + }); + + it('should remove cookies', function () { + cookies.write('foo', 'bar'); + cookies.remove('foo'); + expect(cookies.read('foo')).toEqual(null); + }); + + it('should uri encode values', function () { + cookies.write('foo', 'bar baz%'); + expect(document.cookie).toEqual('foo=bar%20baz%25'); + }); +}); diff --git a/axios/test/specs/helpers/isAbsoluteURL.spec.js b/axios/test/specs/helpers/isAbsoluteURL.spec.js new file mode 100644 index 0000000..0af4139 --- /dev/null +++ b/axios/test/specs/helpers/isAbsoluteURL.spec.js @@ -0,0 +1,23 @@ +var isAbsoluteURL = require('../../../lib/helpers/isAbsoluteURL'); + +describe('helpers::isAbsoluteURL', function () { + it('should return true if URL begins with valid scheme name', function () { + expect(isAbsoluteURL('https://api.github.com/users')).toBe(true); + expect(isAbsoluteURL('custom-scheme-v1.0://example.com/')).toBe(true); + expect(isAbsoluteURL('HTTP://example.com/')).toBe(true); + }); + + it('should return false if URL begins with invalid scheme name', function () { + expect(isAbsoluteURL('123://example.com/')).toBe(false); + expect(isAbsoluteURL('!valid://example.com/')).toBe(false); + }); + + it('should return true if URL is protocol-relative', function () { + expect(isAbsoluteURL('//example.com/')).toBe(true); + }); + + it('should return false if URL is relative', function () { + expect(isAbsoluteURL('/foo')).toBe(false); + expect(isAbsoluteURL('foo')).toBe(false); + }); +}); diff --git a/axios/test/specs/helpers/isAxiosError.spec.js b/axios/test/specs/helpers/isAxiosError.spec.js new file mode 100644 index 0000000..7aeef85 --- /dev/null +++ b/axios/test/specs/helpers/isAxiosError.spec.js @@ -0,0 +1,20 @@ +var createError = require('../../../lib/core/createError'); +var enhanceError = require('../../../lib/core/enhanceError'); +var isAxiosError = require('../../../lib/helpers/isAxiosError'); + +describe('helpers::isAxiosError', function () { + it('should return true if the error is created by core::createError', function () { + expect(isAxiosError(createError('Boom!', { foo: 'bar' }))) + .toBe(true); + }); + + it('should return true if the error is enhanced by core::enhanceError', function () { + expect(isAxiosError(enhanceError(new Error('Boom!'), { foo: 'bar' }))) + .toBe(true); + }); + + it('should return false if the error is a normal Error instance', function () { + expect(isAxiosError(new Error('Boom!'))) + .toBe(false); + }); +}); diff --git a/axios/test/specs/helpers/isURLSameOrigin.spec.js b/axios/test/specs/helpers/isURLSameOrigin.spec.js new file mode 100644 index 0000000..c26c770 --- /dev/null +++ b/axios/test/specs/helpers/isURLSameOrigin.spec.js @@ -0,0 +1,11 @@ +var isURLSameOrigin = require('../../../lib/helpers/isURLSameOrigin'); + +describe('helpers::isURLSameOrigin', function () { + it('should detect same origin', function () { + expect(isURLSameOrigin(window.location.href)).toEqual(true); + }); + + it('should detect different origin', function () { + expect(isURLSameOrigin('https://github.com/axios/axios')).toEqual(false); + }); +}); diff --git a/axios/test/specs/helpers/normalizeHeaderName.spec.js b/axios/test/specs/helpers/normalizeHeaderName.spec.js new file mode 100644 index 0000000..d8d5e82 --- /dev/null +++ b/axios/test/specs/helpers/normalizeHeaderName.spec.js @@ -0,0 +1,21 @@ +var normalizeHeaderName = require('../../../lib/helpers/normalizeHeaderName'); + +describe('helpers::normalizeHeaderName', function () { + it('should normalize matching header name', function () { + var headers = { + 'conTenT-Type': 'foo/bar', + }; + normalizeHeaderName(headers, 'Content-Type'); + expect(headers['Content-Type']).toBe('foo/bar'); + expect(headers['conTenT-Type']).toBeUndefined(); + }); + + it('should not change non-matching header name', function () { + var headers = { + 'content-type': 'foo/bar', + }; + normalizeHeaderName(headers, 'Content-Length'); + expect(headers['content-type']).toBe('foo/bar'); + expect(headers['Content-Length']).toBeUndefined(); + }); +}); diff --git a/axios/test/specs/helpers/parseHeaders.spec.js b/axios/test/specs/helpers/parseHeaders.spec.js new file mode 100644 index 0000000..254d1d6 --- /dev/null +++ b/axios/test/specs/helpers/parseHeaders.spec.js @@ -0,0 +1,45 @@ +var parseHeaders = require('../../../lib/helpers/parseHeaders'); + +describe('helpers::parseHeaders', function () { + it('should parse headers', function () { + var date = new Date(); + var parsed = parseHeaders( + 'Date: ' + date.toISOString() + '\n' + + 'Content-Type: application/json\n' + + 'Connection: keep-alive\n' + + 'Transfer-Encoding: chunked' + ); + + expect(parsed['date']).toEqual(date.toISOString()); + expect(parsed['content-type']).toEqual('application/json'); + expect(parsed['connection']).toEqual('keep-alive'); + expect(parsed['transfer-encoding']).toEqual('chunked'); + }); + + it('should use array for set-cookie', function() { + var parsedZero = parseHeaders(''); + var parsedSingle = parseHeaders( + 'Set-Cookie: key=val;' + ); + var parsedMulti = parseHeaders( + 'Set-Cookie: key=val;\n' + + 'Set-Cookie: key2=val2;\n' + ); + + expect(parsedZero['set-cookie']).toBeUndefined(); + expect(parsedSingle['set-cookie']).toEqual(['key=val;']); + expect(parsedMulti['set-cookie']).toEqual(['key=val;', 'key2=val2;']); + }); + + it('should handle duplicates', function() { + var parsed = parseHeaders( + 'Age: age-a\n' + // age is in ignore duplicates blocklist + 'Age: age-b\n' + + 'Foo: foo-a\n' + + 'Foo: foo-b\n' + ); + + expect(parsed['age']).toEqual('age-a'); + expect(parsed['foo']).toEqual('foo-a, foo-b'); + }); +}); diff --git a/axios/test/specs/helpers/spread.spec.js b/axios/test/specs/helpers/spread.spec.js new file mode 100644 index 0000000..702063b --- /dev/null +++ b/axios/test/specs/helpers/spread.spec.js @@ -0,0 +1,21 @@ +var spread = require('../../../lib/helpers/spread'); + +describe('helpers::spread', function () { + it('should spread array to arguments', function () { + var value = 0; + spread(function (a, b) { + value = a * b; + })([5, 10]); + + expect(value).toEqual(50); + }); + + it('should return callback result', function () { + var value = spread(function (a, b) { + return a * b; + })([5, 10]); + + expect(value).toEqual(50); + }); +}); + diff --git a/axios/test/specs/instance.spec.js b/axios/test/specs/instance.spec.js new file mode 100644 index 0000000..bd9e9fb --- /dev/null +++ b/axios/test/specs/instance.spec.js @@ -0,0 +1,114 @@ +describe('instance', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should have the same methods as default instance', function () { + var instance = axios.create(); + + for (var prop in axios) { + if ([ + 'Axios', + 'create', + 'Cancel', + 'CancelToken', + 'isCancel', + 'all', + 'spread', + 'isAxiosError', + 'default'].indexOf(prop) > -1) { + continue; + } + expect(typeof instance[prop]).toBe(typeof axios[prop]); + } + }); + + it('should make an http request without verb helper', function (done) { + var instance = axios.create(); + + instance('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + done(); + }); + }); + + it('should make an http request with url instead of baseURL', function (done) { + var instance = axios.create({ + url: 'https://api.example.com' + }); + + instance('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + done(); + }); + }); + + it('should make an http request', function (done) { + var instance = axios.create(); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + done(); + }); + }); + + it('should use instance options', function (done) { + var instance = axios.create({ timeout: 1000 }); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.timeout).toBe(1000); + done(); + }); + }); + + it('should have defaults.headers', function () { + var instance = axios.create({ + baseURL: 'https://api.example.com' + }); + + expect(typeof instance.defaults.headers, 'object'); + expect(typeof instance.defaults.headers.common, 'object'); + }); + + it('should have interceptors on the instance', function (done) { + axios.interceptors.request.use(function (config) { + config.foo = true; + return config; + }); + + var instance = axios.create(); + instance.interceptors.request.use(function (config) { + config.bar = true; + return config; + }); + + var response; + instance.get('/foo').then(function (res) { + response = res; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200 + }); + + setTimeout(function () { + expect(response.config.foo).toEqual(undefined); + expect(response.config.bar).toEqual(true); + done(); + }, 100); + }); + }); +}); diff --git a/axios/test/specs/interceptors.spec.js b/axios/test/specs/interceptors.spec.js new file mode 100644 index 0000000..effbcde --- /dev/null +++ b/axios/test/specs/interceptors.spec.js @@ -0,0 +1,273 @@ +describe('interceptors', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + axios.interceptors.request.handlers = []; + axios.interceptors.response.handlers = []; + }); + + it('should add a request interceptor', function (done) { + axios.interceptors.request.use(function (config) { + config.headers.test = 'added by interceptor'; + return config; + }); + + axios('/foo'); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: 'OK' + }); + + expect(request.requestHeaders.test).toBe('added by interceptor'); + done(); + }); + }); + + it('should add a request interceptor that returns a new config object', function (done) { + axios.interceptors.request.use(function () { + return { + url: '/bar', + method: 'post' + }; + }); + + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.method).toBe('POST'); + expect(request.url).toBe('/bar'); + done(); + }); + }); + + it('should add a request interceptor that returns a promise', function (done) { + axios.interceptors.request.use(function (config) { + return new Promise(function (resolve) { + // do something async + setTimeout(function () { + config.headers.async = 'promise'; + resolve(config); + }, 100); + }); + }); + + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders.async).toBe('promise'); + done(); + }); + }); + + it('should add multiple request interceptors', function (done) { + axios.interceptors.request.use(function (config) { + config.headers.test1 = '1'; + return config; + }); + axios.interceptors.request.use(function (config) { + config.headers.test2 = '2'; + return config; + }); + axios.interceptors.request.use(function (config) { + config.headers.test3 = '3'; + return config; + }); + + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders.test1).toBe('1'); + expect(request.requestHeaders.test2).toBe('2'); + expect(request.requestHeaders.test3).toBe('3'); + done(); + }); + }); + + it('should add a response interceptor', function (done) { + var response; + + axios.interceptors.response.use(function (data) { + data.data = data.data + ' - modified by interceptor'; + return data; + }); + + axios('/foo').then(function (data) { + response = data; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: 'OK' + }); + + setTimeout(function () { + expect(response.data).toBe('OK - modified by interceptor'); + done(); + }, 100); + }); + }); + + it('should add a response interceptor that returns a new data object', function (done) { + var response; + + axios.interceptors.response.use(function () { + return { + data: 'stuff' + }; + }); + + axios('/foo').then(function (data) { + response = data; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: 'OK' + }); + + setTimeout(function () { + expect(response.data).toBe('stuff'); + done(); + }, 100); + }); + }); + + it('should add a response interceptor that returns a promise', function (done) { + var response; + + axios.interceptors.response.use(function (data) { + return new Promise(function (resolve) { + // do something async + setTimeout(function () { + data.data = 'you have been promised!'; + resolve(data); + }, 10); + }); + }); + + axios('/foo').then(function (data) { + response = data; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: 'OK' + }); + + setTimeout(function () { + expect(response.data).toBe('you have been promised!'); + done(); + }, 100); + }); + }); + + it('should add multiple response interceptors', function (done) { + var response; + + axios.interceptors.response.use(function (data) { + data.data = data.data + '1'; + return data; + }); + axios.interceptors.response.use(function (data) { + data.data = data.data + '2'; + return data; + }); + axios.interceptors.response.use(function (data) { + data.data = data.data + '3'; + return data; + }); + + axios('/foo').then(function (data) { + response = data; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: 'OK' + }); + + setTimeout(function () { + expect(response.data).toBe('OK123'); + done(); + }, 100); + }); + }); + + it('should allow removing interceptors', function (done) { + var response, intercept; + + axios.interceptors.response.use(function (data) { + data.data = data.data + '1'; + return data; + }); + intercept = axios.interceptors.response.use(function (data) { + data.data = data.data + '2'; + return data; + }); + axios.interceptors.response.use(function (data) { + data.data = data.data + '3'; + return data; + }); + + axios.interceptors.response.eject(intercept); + + axios('/foo').then(function (data) { + response = data; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: 'OK' + }); + + setTimeout(function () { + expect(response.data).toBe('OK13'); + done(); + }, 100); + }); + }); + + it('should execute interceptors before transformers', function (done) { + axios.interceptors.request.use(function (config) { + config.data.baz = 'qux'; + return config; + }); + + axios.post('/foo', { + foo: 'bar' + }); + + getAjaxRequest().then(function (request) { + expect(request.params).toEqual('{"foo":"bar","baz":"qux"}'); + done(); + }); + }); + + it('should modify base URL in request interceptor', function (done) { + var instance = axios.create({ + baseURL: 'http://test.com/' + }); + + instance.interceptors.request.use(function (config) { + config.baseURL = 'http://rebase.com/'; + return config; + }); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://rebase.com/foo'); + done(); + }); + }); +}); diff --git a/axios/test/specs/options.spec.js b/axios/test/specs/options.spec.js new file mode 100644 index 0000000..762b851 --- /dev/null +++ b/axios/test/specs/options.spec.js @@ -0,0 +1,112 @@ +describe('options', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should default method to get', function (done) { + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.method).toBe('GET'); + done(); + }); + }); + + it('should accept headers', function (done) { + axios('/foo', { + headers: { + 'X-Requested-With': 'XMLHttpRequest' + } + }); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders['X-Requested-With']).toEqual('XMLHttpRequest'); + done(); + }); + }); + + it('should accept params', function (done) { + axios('/foo', { + params: { + foo: 123, + bar: 456 + } + }); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo?foo=123&bar=456'); + done(); + }); + }); + + it('should allow overriding default headers', function (done) { + axios('/foo', { + headers: { + 'Accept': 'foo/bar' + } + }); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders['Accept']).toEqual('foo/bar'); + done(); + }); + }); + + it('should accept base URL', function (done) { + var instance = axios.create({ + baseURL: 'http://test.com/' + }); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://test.com/foo'); + done(); + }); + }); + + it('should ignore base URL if request URL is absolute', function (done) { + var instance = axios.create({ + baseURL: 'http://someurl.com/' + }); + + instance.get('http://someotherurl.com/'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://someotherurl.com/'); + done(); + }); + }); + + it('should change only the baseURL of the specified instance', function() { + var instance1 = axios.create(); + var instance2 = axios.create(); + + instance1.defaults.baseURL = 'http://instance1.example.com/'; + + expect(instance2.defaults.baseURL).not.toBe('http://instance1.example.com/'); + }); + + it('should change only the headers of the specified instance', function() { + var instance1 = axios.create(); + var instance2 = axios.create(); + + instance1.defaults.headers.common.Authorization = 'faketoken'; + instance2.defaults.headers.common.Authorization = 'differentfaketoken'; + + instance1.defaults.headers.common['Content-Type'] = 'application/xml'; + instance2.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded'; + + expect(axios.defaults.headers.common.Authorization).toBe(undefined); + expect(instance1.defaults.headers.common.Authorization).toBe('faketoken'); + expect(instance2.defaults.headers.common.Authorization).toBe('differentfaketoken'); + + expect(axios.defaults.headers.common['Content-Type']).toBe(undefined); + expect(instance1.defaults.headers.common['Content-Type']).toBe('application/xml'); + expect(instance2.defaults.headers.common['Content-Type']).toBe('application/x-www-form-urlencoded'); + }); +}); diff --git a/axios/test/specs/progress.spec.js b/axios/test/specs/progress.spec.js new file mode 100644 index 0000000..287b4dd --- /dev/null +++ b/axios/test/specs/progress.spec.js @@ -0,0 +1,111 @@ +describe('progress events', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should add a download progress handler', function (done) { + var progressSpy = jasmine.createSpy('progress'); + + axios('/foo', { onDownloadProgress: progressSpy } ); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: '{"foo": "bar"}' + }); + expect(progressSpy).toHaveBeenCalled(); + done(); + }); + }); + + it('should add a upload progress handler', function (done) { + var progressSpy = jasmine.createSpy('progress'); + + axios('/foo', { onUploadProgress: progressSpy } ); + + getAjaxRequest().then(function (request) { + // Jasmine AJAX doesn't trigger upload events. Waiting for upstream fix + // expect(progressSpy).toHaveBeenCalled(); + done(); + }); + }); + + it('should add both upload and download progress handlers', function (done) { + var downloadProgressSpy = jasmine.createSpy('downloadProgress'); + var uploadProgressSpy = jasmine.createSpy('uploadProgress'); + + axios('/foo', { onDownloadProgress: downloadProgressSpy, onUploadProgress: uploadProgressSpy }); + + getAjaxRequest().then(function (request) { + // expect(uploadProgressSpy).toHaveBeenCalled(); + expect(downloadProgressSpy).not.toHaveBeenCalled(); + request.respondWith({ + status: 200, + responseText: '{"foo": "bar"}' + }); + expect(downloadProgressSpy).toHaveBeenCalled(); + done(); + }); + }); + + it('should add a download progress handler from instance config', function (done) { + var progressSpy = jasmine.createSpy('progress'); + + var instance = axios.create({ + onDownloadProgress: progressSpy, + }); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: '{"foo": "bar"}' + }); + expect(progressSpy).toHaveBeenCalled(); + done(); + }); + }); + + it('should add a upload progress handler from instance config', function (done) { + var progressSpy = jasmine.createSpy('progress'); + + var instance = axios.create({ + onUploadProgress: progressSpy, + }); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + // expect(progressSpy).toHaveBeenCalled(); + done(); + }); + }); + + it('should add upload and download progress handlers from instance config', function (done) { + var downloadProgressSpy = jasmine.createSpy('downloadProgress'); + var uploadProgressSpy = jasmine.createSpy('uploadProgress'); + + var instance = axios.create({ + onDownloadProgress: downloadProgressSpy, + onUploadProgress: uploadProgressSpy, + }); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + // expect(uploadProgressSpy).toHaveBeenCalled(); + expect(downloadProgressSpy).not.toHaveBeenCalled(); + request.respondWith({ + status: 200, + responseText: '{"foo": "bar"}' + }); + expect(downloadProgressSpy).toHaveBeenCalled(); + done(); + }); + }); +}); diff --git a/axios/test/specs/promise.spec.js b/axios/test/specs/promise.spec.js new file mode 100644 index 0000000..ffc17ce --- /dev/null +++ b/axios/test/specs/promise.spec.js @@ -0,0 +1,70 @@ +describe('promise', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should provide succinct object to then', function (done) { + var response; + + axios('/foo').then(function (r) { + response = r; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: '{"hello":"world"}' + }); + + setTimeout(function () { + expect(typeof response).toEqual('object'); + expect(response.data.hello).toEqual('world'); + expect(response.status).toEqual(200); + expect(response.headers['content-type']).toEqual('application/json'); + expect(response.config.url).toEqual('/foo'); + done(); + }, 100); + }); + }); + + it('should support all', function (done) { + var fulfilled = false; + + axios.all([true, 123]).then(function () { + fulfilled = true; + }); + + setTimeout(function () { + expect(fulfilled).toEqual(true); + done(); + }, 100); + }); + + it('should support spread', function (done) { + var sum = 0; + var fulfilled = false; + var result; + + axios + .all([123, 456]) + .then(axios.spread(function (a, b) { + sum = a + b; + fulfilled = true; + return 'hello world'; + })) + .then(function (res) { + result = res; + }); + + setTimeout(function () { + expect(fulfilled).toEqual(true); + expect(sum).toEqual(123 + 456); + expect(result).toEqual('hello world'); + done(); + }, 100); + }); +}); diff --git a/axios/test/specs/requests.spec.js b/axios/test/specs/requests.spec.js new file mode 100644 index 0000000..692dbb0 --- /dev/null +++ b/axios/test/specs/requests.spec.js @@ -0,0 +1,439 @@ +describe('requests', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should treat single string arg as url', function (done) { + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + expect(request.method).toBe('GET'); + done(); + }); + }); + + it('should treat method value as lowercase string', function (done) { + axios({ + url: '/foo', + method: 'POST' + }).then(function (response) { + expect(response.config.method).toBe('post'); + done(); + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200 + }); + }); + }); + + it('should allow string arg as url, and config arg', function (done) { + axios.post('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + expect(request.method).toBe('POST'); + done(); + }); + }); + + it('should allow data', function (done) { + axios.delete('/foo', { + data: { foo: 'bar' } + }); + + getAjaxRequest().then(function (request) { + expect(request.params).toBe(JSON.stringify({ foo: 'bar' })); + done(); + }); + }); + + it('should make an http request', function (done) { + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + done(); + }); + }); + + it('should reject on network errors', function (done) { + // disable jasmine.Ajax since we're hitting a non-existent server anyway + jasmine.Ajax.uninstall(); + + var resolveSpy = jasmine.createSpy('resolve'); + var rejectSpy = jasmine.createSpy('reject'); + + var finish = function () { + expect(resolveSpy).not.toHaveBeenCalled(); + expect(rejectSpy).toHaveBeenCalled(); + var reason = rejectSpy.calls.first().args[0]; + expect(reason instanceof Error).toBe(true); + expect(reason.config.method).toBe('get'); + expect(reason.config.url).toBe('http://thisisnotaserver/foo'); + expect(reason.request).toEqual(jasmine.any(XMLHttpRequest)); + + // re-enable jasmine.Ajax + jasmine.Ajax.install(); + + done(); + }; + + axios('http://thisisnotaserver/foo') + .then(resolveSpy, rejectSpy) + .then(finish, finish); + }); + + it('should reject on abort', function (done) { + var resolveSpy = jasmine.createSpy('resolve'); + var rejectSpy = jasmine.createSpy('reject'); + + var finish = function () { + expect(resolveSpy).not.toHaveBeenCalled(); + expect(rejectSpy).toHaveBeenCalled(); + var reason = rejectSpy.calls.first().args[0]; + expect(reason instanceof Error).toBe(true); + expect(reason.config.method).toBe('get'); + expect(reason.config.url).toBe('/foo'); + expect(reason.request).toEqual(jasmine.any(XMLHttpRequest)); + + done(); + }; + + axios('/foo') + .then(resolveSpy, rejectSpy) + .then(finish, finish); + + getAjaxRequest().then(function (request) { + request.abort(); + }); + }); + + it('should reject when validateStatus returns false', function (done) { + var resolveSpy = jasmine.createSpy('resolve'); + var rejectSpy = jasmine.createSpy('reject'); + + axios('/foo', { + validateStatus: function (status) { + return status !== 500; + } + }).then(resolveSpy) + .catch(rejectSpy) + .then(function () { + expect(resolveSpy).not.toHaveBeenCalled(); + expect(rejectSpy).toHaveBeenCalled(); + var reason = rejectSpy.calls.first().args[0]; + expect(reason instanceof Error).toBe(true); + expect(reason.message).toBe('Request failed with status code 500'); + expect(reason.config.method).toBe('get'); + expect(reason.config.url).toBe('/foo'); + expect(reason.response.status).toBe(500); + + done(); + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 500 + }); + }); + }); + + it('should resolve when validateStatus returns true', function (done) { + var resolveSpy = jasmine.createSpy('resolve'); + var rejectSpy = jasmine.createSpy('reject'); + + axios('/foo', { + validateStatus: function (status) { + return status === 500; + } + }).then(resolveSpy) + .catch(rejectSpy) + .then(function () { + expect(resolveSpy).toHaveBeenCalled(); + expect(rejectSpy).not.toHaveBeenCalled(); + done(); + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 500 + }); + }); + }); + + it('should resolve when the response status is 0 (i.e. requesting with file protocol)', function (done) { + var resolveSpy = jasmine.createSpy('resolve'); + var rejectSpy = jasmine.createSpy('reject'); + + axios('file:///xxx').then(resolveSpy) + .catch(rejectSpy) + .then(function () { + expect(resolveSpy).toHaveBeenCalled(); + expect(rejectSpy).not.toHaveBeenCalled(); + done(); + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 0, + responseURL: 'file:///xxx', + }); + }); + }); + + it('should resolve when validateStatus is null', function (done) { + var resolveSpy = jasmine.createSpy('resolve'); + var rejectSpy = jasmine.createSpy('reject'); + + axios('/foo', { + validateStatus: null + }).then(resolveSpy) + .catch(rejectSpy) + .then(function () { + expect(resolveSpy).toHaveBeenCalled(); + expect(rejectSpy).not.toHaveBeenCalled(); + done(); + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 500 + }); + }); + }); + + it('should resolve when validateStatus is undefined', function (done) { + var resolveSpy = jasmine.createSpy('resolve'); + var rejectSpy = jasmine.createSpy('reject'); + + axios('/foo', { + validateStatus: undefined + }).then(resolveSpy) + .catch(rejectSpy) + .then(function () { + expect(resolveSpy).toHaveBeenCalled(); + expect(rejectSpy).not.toHaveBeenCalled(); + done(); + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 500 + }); + }); + }); + + // https://github.com/axios/axios/issues/378 + it('should return JSON when rejecting', function (done) { + var response; + + axios('/api/account/signup', { + username: null, + password: null + }, { + method: 'post', + headers: { + 'Accept': 'application/json' + } + }) + .catch(function (error) { + response = error.response; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 400, + statusText: 'Bad Request', + responseText: '{"error": "BAD USERNAME", "code": 1}' + }); + + setTimeout(function () { + expect(typeof response.data).toEqual('object'); + expect(response.data.error).toEqual('BAD USERNAME'); + expect(response.data.code).toEqual(1); + done(); + }, 100); + }); + }); + + it('should make cross domain http request', function (done) { + var response; + + axios.post('www.someurl.com/foo').then(function(res){ + response = res; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + statusText: 'OK', + responseText: '{"foo": "bar"}', + headers: { + 'Content-Type': 'application/json' + } + }); + + setTimeout(function () { + expect(response.data.foo).toEqual('bar'); + expect(response.status).toEqual(200); + expect(response.statusText).toEqual('OK'); + expect(response.headers['content-type']).toEqual('application/json'); + done(); + }, 100); + }); + }); + + + it('should supply correct response', function (done) { + var response; + + axios.post('/foo').then(function (res) { + response = res; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + statusText: 'OK', + responseText: '{"foo": "bar"}', + headers: { + 'Content-Type': 'application/json' + } + }); + + setTimeout(function () { + expect(response.data.foo).toEqual('bar'); + expect(response.status).toEqual(200); + expect(response.statusText).toEqual('OK'); + expect(response.headers['content-type']).toEqual('application/json'); + done(); + }, 100); + }); + }); + + it('should not modify the config url with relative baseURL', function (done) { + var config; + + axios.get('/foo', { + baseURL: '/api' + }).catch(function (error) { + config = error.config; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 404, + statusText: 'NOT FOUND', + responseText: 'Resource not found' + }); + + setTimeout(function () { + expect(config.baseURL).toEqual('/api'); + expect(config.url).toEqual('/foo'); + done(); + }, 100); + }); + }); + + it('should allow overriding Content-Type header case-insensitive', function (done) { + var response; + var contentType = 'application/vnd.myapp.type+json'; + + axios.post('/foo', { prop: 'value' }, { + headers: { + 'content-type': contentType + } + }).then(function (res) { + response = res; + }); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders['Content-Type']).toEqual(contentType); + done(); + }); + }); + + it('should support binary data as array buffer', function (done) { + var input = new Int8Array(2); + input[0] = 1; + input[1] = 2; + + axios.post('/foo', input.buffer); + + getAjaxRequest().then(function (request) { + var output = new Int8Array(request.params); + expect(output.length).toEqual(2); + expect(output[0]).toEqual(1); + expect(output[1]).toEqual(2); + done(); + }); + }); + + it('should support binary data as array buffer view', function (done) { + var input = new Int8Array(2); + input[0] = 1; + input[1] = 2; + + axios.post('/foo', input); + + getAjaxRequest().then(function (request) { + var output = new Int8Array(request.params); + expect(output.length).toEqual(2); + expect(output[0]).toEqual(1); + expect(output[1]).toEqual(2); + done(); + }); + }); + + it('should support array buffer response', function (done) { + var response; + + function str2ab(str) { + var buff = new ArrayBuffer(str.length * 2); + var view = new Uint16Array(buff); + for ( var i=0, l=str.length; i<l; i++) { + view[i] = str.charCodeAt(i); + } + return buff; + } + + axios('/foo', { + responseType: 'arraybuffer' + }).then(function (data) { + response = data; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + response: str2ab('Hello world') + }); + + setTimeout(function () { + expect(response.data.byteLength).toBe(22); + done(); + }, 100); + }); + }); + + it('should support URLSearchParams', function (done) { + var params = new URLSearchParams(); + params.append('param1', 'value1'); + params.append('param2', 'value2'); + + axios.post('/foo', params); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders['Content-Type']).toBe('application/x-www-form-urlencoded;charset=utf-8'); + expect(request.params).toBe('param1=value1¶m2=value2'); + done(); + }); + }); +}); diff --git a/axios/test/specs/transform.spec.js b/axios/test/specs/transform.spec.js new file mode 100644 index 0000000..5199ecb --- /dev/null +++ b/axios/test/specs/transform.spec.js @@ -0,0 +1,94 @@ +describe('transform', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + jasmine.Ajax.uninstall(); + }); + + it('should transform JSON to string', function (done) { + var data = { + foo: 'bar' + }; + + axios.post('/foo', data); + + getAjaxRequest().then(function (request) { + expect(request.params).toEqual('{"foo":"bar"}'); + done(); + }); + }); + + it('should transform string to JSON', function (done) { + var response; + + axios('/foo').then(function (data) { + response = data; + }); + + getAjaxRequest().then(function (request) { + request.respondWith({ + status: 200, + responseText: '{"foo": "bar"}' + }); + + setTimeout(function () { + expect(typeof response.data).toEqual('object'); + expect(response.data.foo).toEqual('bar'); + done(); + }, 100); + }); + }); + + it('should override default transform', function (done) { + var data = { + foo: 'bar' + }; + + axios.post('/foo', data, { + transformRequest: function (data) { + return data; + } + }); + + getAjaxRequest().then(function (request) { + expect(typeof request.params).toEqual('object'); + done(); + }); + }); + + it('should allow an Array of transformers', function (done) { + var data = { + foo: 'bar' + }; + + axios.post('/foo', data, { + transformRequest: axios.defaults.transformRequest.concat( + function (data) { + return data.replace('bar', 'baz'); + } + ) + }); + + getAjaxRequest().then(function (request) { + expect(request.params).toEqual('{"foo":"baz"}'); + done(); + }); + }); + + it('should allowing mutating headers', function (done) { + var token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36); + + axios('/foo', { + transformRequest: function (data, headers) { + headers['X-Authorization'] = token; + } + }); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders['X-Authorization']).toEqual(token); + done(); + }); + }); +}); diff --git a/axios/test/specs/utils/extend.spec.js b/axios/test/specs/utils/extend.spec.js new file mode 100644 index 0000000..3e1bb32 --- /dev/null +++ b/axios/test/specs/utils/extend.spec.js @@ -0,0 +1,34 @@ +var extend = require('../../../lib/utils').extend; + +describe('utils::extend', function () { + it('should be mutable', function () { + var a = {}; + var b = {foo: 123}; + + extend(a, b); + + expect(a.foo).toEqual(b.foo); + }); + + it('should extend properties', function () { + var a = {foo: 123, bar: 456}; + var b = {bar: 789}; + + a = extend(a, b); + + expect(a.foo).toEqual(123); + expect(a.bar).toEqual(789); + }); + + it('should bind to thisArg', function () { + var a = {}; + var b = {getFoo: function getFoo() { return this.foo; }}; + var thisArg = { foo: 'barbaz' }; + + extend(a, b, thisArg); + + expect(typeof a.getFoo).toEqual('function'); + expect(a.getFoo()).toEqual(thisArg.foo); + }); +}); + diff --git a/axios/test/specs/utils/forEach.spec.js b/axios/test/specs/utils/forEach.spec.js new file mode 100644 index 0000000..5f28399 --- /dev/null +++ b/axios/test/specs/utils/forEach.spec.js @@ -0,0 +1,63 @@ +var forEach = require('../../../lib/utils').forEach; + +describe('utils::forEach', function () { + it('should loop over an array', function () { + var sum = 0; + + forEach([1, 2, 3, 4, 5], function (val) { + sum += val; + }); + + expect(sum).toEqual(15); + }); + + it('should loop over object keys', function () { + var keys = ''; + var vals = 0; + var obj = { + b: 1, + a: 2, + r: 3 + }; + + forEach(obj, function (v, k) { + keys += k; + vals += v; + }); + + expect(keys).toEqual('bar'); + expect(vals).toEqual(6); + }); + + it('should handle undefined gracefully', function () { + var count = 0; + + forEach(undefined, function () { + count++; + }); + + expect(count).toEqual(0); + }); + + it('should make an array out of non-array argument', function () { + var count = 0; + + forEach(function () {}, function () { + count++; + }); + + expect(count).toEqual(1); + }); + + it('should handle non object prototype gracefully', function () { + var count = 0; + var data = Object.create(null); + data.foo = 'bar' + + forEach(data, function () { + count++; + }); + + expect(count).toEqual(1); + }); +}); diff --git a/axios/test/specs/utils/isX.spec.js b/axios/test/specs/utils/isX.spec.js new file mode 100644 index 0000000..59b3168 --- /dev/null +++ b/axios/test/specs/utils/isX.spec.js @@ -0,0 +1,80 @@ +var utils = require('../../../lib/utils'); +var Stream = require('stream'); + +describe('utils::isX', function () { + it('should validate Array', function () { + expect(utils.isArray([])).toEqual(true); + expect(utils.isArray({length: 5})).toEqual(false); + }); + + it('should validate Buffer', function () { + expect(utils.isBuffer(Buffer.from('a'))).toEqual(true); + expect(utils.isBuffer(null)).toEqual(false); + expect(utils.isBuffer(undefined)).toEqual(false); + }); + + it('should validate ArrayBuffer', function () { + expect(utils.isArrayBuffer(new ArrayBuffer(2))).toEqual(true); + expect(utils.isArrayBuffer({})).toEqual(false); + }); + + it('should validate ArrayBufferView', function () { + expect(utils.isArrayBufferView(new DataView(new ArrayBuffer(2)))).toEqual(true); + }); + + it('should validate FormData', function () { + expect(utils.isFormData(new FormData())).toEqual(true); + }); + + it('should validate Blob', function () { + expect(utils.isBlob(new Blob())).toEqual(true); + }); + + it('should validate String', function () { + expect(utils.isString('')).toEqual(true); + expect(utils.isString({toString: function () { return ''; }})).toEqual(false); + }); + + it('should validate Number', function () { + expect(utils.isNumber(123)).toEqual(true); + expect(utils.isNumber('123')).toEqual(false); + }); + + it('should validate Undefined', function () { + expect(utils.isUndefined()).toEqual(true); + expect(utils.isUndefined(null)).toEqual(false); + }); + + it('should validate Object', function () { + expect(utils.isObject({})).toEqual(true); + expect(utils.isObject([])).toEqual(true); + expect(utils.isObject(null)).toEqual(false); + }); + + it('should validate plain Object', function () { + expect(utils.isPlainObject({})).toEqual(true); + expect(utils.isPlainObject([])).toEqual(false); + expect(utils.isPlainObject(null)).toEqual(false); + expect(utils.isPlainObject(Object.create({}))).toEqual(false); + }); + + it('should validate Date', function () { + expect(utils.isDate(new Date())).toEqual(true); + expect(utils.isDate(Date.now())).toEqual(false); + }); + + it('should validate Function', function () { + expect(utils.isFunction(function () {})).toEqual(true); + expect(utils.isFunction('function')).toEqual(false); + }); + + it('should validate Stream', function () { + expect(utils.isStream(new Stream.Readable())).toEqual(true); + expect(utils.isStream({ foo: 'bar' })).toEqual(false); + }); + + it('should validate URLSearchParams', function () { + expect(utils.isURLSearchParams(new URLSearchParams())).toEqual(true); + expect(utils.isURLSearchParams('foo=1&bar=2')).toEqual(false); + }); +}); diff --git a/axios/test/specs/utils/merge.spec.js b/axios/test/specs/utils/merge.spec.js new file mode 100644 index 0000000..efe844a --- /dev/null +++ b/axios/test/specs/utils/merge.spec.js @@ -0,0 +1,84 @@ +var merge = require('../../../lib/utils').merge; + +describe('utils::merge', function () { + it('should be immutable', function () { + var a = {}; + var b = {foo: 123}; + var c = {bar: 456}; + + merge(a, b, c); + + expect(typeof a.foo).toEqual('undefined'); + expect(typeof a.bar).toEqual('undefined'); + expect(typeof b.bar).toEqual('undefined'); + expect(typeof c.foo).toEqual('undefined'); + }); + + it('should merge properties', function () { + var a = {foo: 123}; + var b = {bar: 456}; + var c = {foo: 789}; + var d = merge(a, b, c); + + expect(d.foo).toEqual(789); + expect(d.bar).toEqual(456); + }); + + it('should merge recursively', function () { + var a = {foo: {bar: 123}}; + var b = {foo: {baz: 456}, bar: {qux: 789}}; + + expect(merge(a, b)).toEqual({ + foo: { + bar: 123, + baz: 456 + }, + bar: { + qux: 789 + } + }); + }); + + it('should remove all references from nested objects', function () { + var a = {foo: {bar: 123}}; + var b = {}; + var d = merge(a, b); + + expect(d).toEqual({ + foo: { + bar: 123 + } + }); + + expect(d.foo).not.toBe(a.foo); + }); + + it('handles null and undefined arguments', function () { + expect(merge(undefined, undefined)).toEqual({}); + expect(merge(undefined, {foo: 123})).toEqual({foo: 123}); + expect(merge({foo: 123}, undefined)).toEqual({foo: 123}); + + expect(merge(null, null)).toEqual({}); + expect(merge(null, {foo: 123})).toEqual({foo: 123}); + expect(merge({foo: 123}, null)).toEqual({foo: 123}); + }); + + it('should replace properties with null', function () { + expect(merge({}, {a: null})).toEqual({a: null}); + expect(merge({a: null}, {})).toEqual({a: null}); + }); + + it('should replace properties with arrays', function () { + expect(merge({}, {a: [1, 2, 3]})).toEqual({a: [1, 2, 3]}); + expect(merge({a: 2}, {a: [1, 2, 3]})).toEqual({a: [1, 2, 3]}); + expect(merge({a: {b: 2}}, {a: [1, 2, 3]})).toEqual({a: [1, 2, 3]}); + }); + + it('should replace properties with cloned arrays', function () { + var a = [1, 2, 3]; + var d = merge({}, {a: a}); + + expect(d).toEqual({a: [1, 2, 3]}); + expect(d.a).not.toBe(a); + }); +}); diff --git a/axios/test/specs/utils/trim.spec.js b/axios/test/specs/utils/trim.spec.js new file mode 100644 index 0000000..1b45e95 --- /dev/null +++ b/axios/test/specs/utils/trim.spec.js @@ -0,0 +1,12 @@ +var trim = require('../../../lib/utils').trim; + +describe('utils::trim', function () { + it('should trim spaces', function () { + expect(trim(' foo ')).toEqual('foo'); + }); + + it('should trim tabs', function () { + expect(trim('\tfoo\t')).toEqual('foo'); + }); +}); + diff --git a/axios/test/specs/xsrf.spec.js b/axios/test/specs/xsrf.spec.js new file mode 100644 index 0000000..56cc0d2 --- /dev/null +++ b/axios/test/specs/xsrf.spec.js @@ -0,0 +1,82 @@ +var cookies = require('../../lib/helpers/cookies'); + +describe('xsrf', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + afterEach(function () { + document.cookie = axios.defaults.xsrfCookieName + '=;expires=' + new Date(Date.now() - 86400000).toGMTString(); + jasmine.Ajax.uninstall(); + }); + + it('should not set xsrf header if cookie is null', function (done) { + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); + done(); + }); + }); + + it('should set xsrf header if cookie is set', function (done) { + document.cookie = axios.defaults.xsrfCookieName + '=12345'; + + axios('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual('12345'); + done(); + }); + }); + + it('should not set xsrf header if xsrfCookieName is null', function (done) { + document.cookie = axios.defaults.xsrfCookieName + '=12345'; + + axios('/foo', { + xsrfCookieName: null + }); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); + done(); + }); + }); + + it('should not read cookies at all if xsrfCookieName is null', function (done) { + spyOn(cookies, "read"); + + axios('/foo', { + xsrfCookieName: null + }); + + getAjaxRequest().then(function (request) { + expect(cookies.read).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should not set xsrf header for cross origin', function (done) { + document.cookie = axios.defaults.xsrfCookieName + '=12345'; + + axios('http://example.com/'); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); + done(); + }); + }); + + it('should set xsrf header for cross origin when using withCredentials', function (done) { + document.cookie = axios.defaults.xsrfCookieName + '=12345'; + + axios('http://example.com/', { + withCredentials: true + }); + + getAjaxRequest().then(function (request) { + expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual('12345'); + done(); + }); + }); +}); diff --git a/axios/test/typescript/axios.ts b/axios/test/typescript/axios.ts new file mode 100644 index 0000000..1a1b6a7 --- /dev/null +++ b/axios/test/typescript/axios.ts @@ -0,0 +1,369 @@ +import axios, { + AxiosRequestConfig, + AxiosResponse, + AxiosError, + AxiosInstance, + AxiosAdapter, + Cancel, + CancelToken, + CancelTokenSource, + Canceler +} from '../../'; + +const config: AxiosRequestConfig = { + url: '/user', + method: 'get', + baseURL: 'https://api.example.com/', + transformRequest: (data: any) => '{"foo":"bar"}', + transformResponse: [ + (data: any) => ({ baz: 'qux' }) + ], + headers: { 'X-FOO': 'bar' }, + params: { id: 12345 }, + paramsSerializer: (params: any) => 'id=12345', + data: { foo: 'bar' }, + timeout: 10000, + withCredentials: true, + auth: { + username: 'janedoe', + password: 's00pers3cret' + }, + responseType: 'json', + xsrfCookieName: 'XSRF-TOKEN', + xsrfHeaderName: 'X-XSRF-TOKEN', + onUploadProgress: (progressEvent: ProgressEvent) => {}, + onDownloadProgress: (progressEvent: ProgressEvent) => {}, + maxContentLength: 2000, + maxBodyLength: 2000, + validateStatus: (status: number) => status >= 200 && status < 300, + maxRedirects: 5, + proxy: { + host: '127.0.0.1', + port: 9000 + }, + cancelToken: new axios.CancelToken((cancel: Canceler) => {}) +}; + +const nullValidateStatusConfig: AxiosRequestConfig = { + validateStatus: null +}; + +const undefinedValidateStatusConfig: AxiosRequestConfig = { + validateStatus: undefined +}; + +const handleResponse = (response: AxiosResponse) => { + console.log(response.data); + console.log(response.status); + console.log(response.statusText); + console.log(response.headers); + console.log(response.config); +}; + +const handleError = (error: AxiosError) => { + if (error.response) { + console.log(error.response.data); + console.log(error.response.status); + console.log(error.response.headers); + } else { + console.log(error.message); + } +}; + +axios(config) + .then(handleResponse) + .catch(handleError); + +axios.get('/user?id=12345') + .then(handleResponse) + .catch(handleError); + +axios.get('/user', { params: { id: 12345 } }) + .then(handleResponse) + .catch(handleError); + +axios.head('/user') + .then(handleResponse) + .catch(handleError); + +axios.options('/user') + .then(handleResponse) + .catch(handleError); + +axios.delete('/user') + .then(handleResponse) + .catch(handleError); + +axios.post('/user', { foo: 'bar' }) + .then(handleResponse) + .catch(handleError); + +axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) + .then(handleResponse) + .catch(handleError); + +axios.put('/user', { foo: 'bar' }) + .then(handleResponse) + .catch(handleError); + +axios.patch('/user', { foo: 'bar' }) + .then(handleResponse) + .catch(handleError); + +// Typed methods +interface User { + id: number; + name: string; +} + +// with default AxiosResponse<T> result + +const handleUserResponse = (response: AxiosResponse<User>) => { + console.log(response.data.id); + console.log(response.data.name); + console.log(response.status); + console.log(response.statusText); + console.log(response.headers); + console.log(response.config); +}; + +axios.get<User>('/user?id=12345') + .then(handleUserResponse) + .catch(handleError); + +axios.get<User>('/user', { params: { id: 12345 } }) + .then(handleUserResponse) + .catch(handleError); + +axios.head<User>('/user') + .then(handleUserResponse) + .catch(handleError); + +axios.options<User>('/user') + .then(handleUserResponse) + .catch(handleError); + +axios.delete<User>('/user') + .then(handleUserResponse) + .catch(handleError); + +axios.post<User>('/user', { foo: 'bar' }) + .then(handleUserResponse) + .catch(handleError); + +axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) + .then(handleUserResponse) + .catch(handleError); + +axios.put<User>('/user', { foo: 'bar' }) + .then(handleUserResponse) + .catch(handleError); + +axios.patch<User>('/user', { foo: 'bar' }) + .then(handleUserResponse) + .catch(handleError); + +// (Typed methods) with custom response type + +const handleStringResponse = (response: string) => { + console.log(response) +} + +axios.get<User, string>('/user?id=12345') + .then(handleStringResponse) + .catch(handleError); + +axios.get<User, string>('/user', { params: { id: 12345 } }) + .then(handleStringResponse) + .catch(handleError); + +axios.head<User, string>('/user') + .then(handleStringResponse) + .catch(handleError); + +axios.options<User, string>('/user') + .then(handleStringResponse) + .catch(handleError); + +axios.delete<User, string>('/user') + .then(handleStringResponse) + .catch(handleError); + +axios.post<User, string>('/user', { foo: 'bar' }) + .then(handleStringResponse) + .catch(handleError); + +axios.post<User, string>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) + .then(handleStringResponse) + .catch(handleError); + +axios.put<User, string>('/user', { foo: 'bar' }) + .then(handleStringResponse) + .catch(handleError); + +axios.patch<User, string>('/user', { foo: 'bar' }) + .then(handleStringResponse) + .catch(handleError); + +axios.request<User, string>({ + method: 'get', + url: '/user?id=12345' +}) + .then(handleStringResponse) + .catch(handleError); + +// Instances + +const instance1: AxiosInstance = axios.create(); +const instance2: AxiosInstance = axios.create(config); + +instance1(config) + .then(handleResponse) + .catch(handleError); + +instance1.request(config) + .then(handleResponse) + .catch(handleError); + +instance1.get('/user?id=12345') + .then(handleResponse) + .catch(handleError); + +instance1.options('/user') + .then(handleResponse) + .catch(handleError); + +instance1.get('/user', { params: { id: 12345 } }) + .then(handleResponse) + .catch(handleError); + +instance1.post('/user', { foo: 'bar' }) + .then(handleResponse) + .catch(handleError); + +instance1.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) + .then(handleResponse) + .catch(handleError); + +// Defaults + +axios.defaults.baseURL = 'https://api.example.com/'; +axios.defaults.headers.common['Authorization'] = 'token'; +axios.defaults.headers.post['X-FOO'] = 'bar'; +axios.defaults.timeout = 2500; + +instance1.defaults.baseURL = 'https://api.example.com/'; +instance1.defaults.headers.common['Authorization'] = 'token'; +instance1.defaults.headers.post['X-FOO'] = 'bar'; +instance1.defaults.timeout = 2500; + +// Interceptors + +const requestInterceptorId: number = axios.interceptors.request.use( + (config: AxiosRequestConfig) => config, + (error: any) => Promise.reject(error) +); + +axios.interceptors.request.eject(requestInterceptorId); + +axios.interceptors.request.use( + (config: AxiosRequestConfig) => Promise.resolve(config), + (error: any) => Promise.reject(error) +); + +axios.interceptors.request.use((config: AxiosRequestConfig) => config); +axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config)); + +const responseInterceptorId: number = axios.interceptors.response.use( + (response: AxiosResponse) => response, + (error: any) => Promise.reject(error) +); + +axios.interceptors.response.eject(responseInterceptorId); + +axios.interceptors.response.use( + (response: AxiosResponse) => Promise.resolve(response), + (error: any) => Promise.reject(error) +); + +axios.interceptors.response.use((response: AxiosResponse) => response); +axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(response)); + +// Adapters + +const adapter: AxiosAdapter = (config: AxiosRequestConfig) => { + const response: AxiosResponse = { + data: { foo: 'bar' }, + status: 200, + statusText: 'OK', + headers: { 'X-FOO': 'bar' }, + config + }; + return Promise.resolve(response); +}; + +axios.defaults.adapter = adapter; + +// axios.all + +const promises = [ + Promise.resolve(1), + Promise.resolve(2) +]; + +const promise: Promise<number[]> = axios.all(promises); + +// axios.spread + +const fn1 = (a: number, b: number, c: number) => `${a}-${b}-${c}`; +const fn2: (arr: number[]) => string = axios.spread(fn1); + +// Promises + +axios.get('/user') + .then((response: AxiosResponse) => 'foo') + .then((value: string) => {}); + +axios.get('/user') + .then((response: AxiosResponse) => Promise.resolve('foo')) + .then((value: string) => {}); + +axios.get('/user') + .then((response: AxiosResponse) => 'foo', (error: any) => 'bar') + .then((value: string) => {}); + +axios.get('/user') + .then((response: AxiosResponse) => 'foo', (error: any) => 123) + .then((value: string | number) => {}); + +axios.get('/user') + .catch((error: any) => 'foo') + .then((value: string) => {}); + +axios.get('/user') + .catch((error: any) => Promise.resolve('foo')) + .then((value: string) => {}); + +// Cancellation + +const source: CancelTokenSource = axios.CancelToken.source(); + +axios.get('/user', { + cancelToken: source.token +}).catch((thrown: AxiosError | Cancel) => { + if (axios.isCancel(thrown)) { + const cancel: Cancel = thrown; + console.log(cancel.message); + } +}); + +source.cancel('Operation has been canceled.'); + +// AxiosError + +axios.get('/user') + .catch((error) => { + if (axios.isAxiosError(error)) { + const axiosError: AxiosError = error; + } + }); diff --git a/axios/test/unit/adapters/cert.pem b/axios/test/unit/adapters/cert.pem new file mode 100644 index 0000000..42dcc29 --- /dev/null +++ b/axios/test/unit/adapters/cert.pem @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICpDCCAYwCCQDbqELLwgbPdDANBgkqhkiG9w0BAQUFADAUMRIwEAYDVQQDDAls +b2NhbGhvc3QwHhcNMjAwNjI2MjIxMTQ3WhcNNDcxMTExMjIxMTQ3WjAUMRIwEAYD +VQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQD6 +Ogt99/dZ0UgbCuVV1RZ9n28Ov3DzrJCkjperQoXomIq3Fr4RUI1a2rwe3mtl3UzE +1IVZVvWPGdEsEQHwXfAsP/jFGTwI3HDyOhcqzFQSKsjvqJWYkOOb+2r3SBrFlRZW +09k/3lC+hx2XtuuG68u4Xgn3AlUvm2vplgCN7eiYcGeNwVuf2eHdOqTRTqiYCZLi +T8GtdYMDXOrwsGZs/jUKd9U0ar/lqwMhmw07yzlVDM2MWM2tyq/asQ7Sf7vuoMFu +oAtDJ3E+bK1k/7SNhdyP4RonhyUCkWG+mzoKDS1qgXroTiQSDUksAvOCTcj8BNIT +ee+Lcn9FaTKNJiKiU9q/AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAFi5ZpaUj+mU +dsgOka+j2/njgNXux3cOjhm7z/N7LeTuDENAOrYa5b+j5JX/YM7RKHrkbXHsQbfs +GB3ufH6QhSiCd/AdsXp/TbCE/8gdq8ykkjwVP1bvBle9oPH7x1aO/WP/odsepYUv +o9aOZW4iNQVmwamU62ezglf3QD7HPeE4LnZueaFtuzRoC+aWT9v0MIeUPJLe3WDQ +FEySwUuthMDJEv92/TeK0YOiunmseCu2mvdiDj6E3C9xa5q2DWgl+msu7+bPgvYO +GuWaoNeQQGk7ebBO3Hk3IyaGx6Cbd8ty+YaZW7dUT+m7KCs1VkxdcDMjZJVWiJy4 +4HcEcKboG4Y= +-----END CERTIFICATE----- diff --git a/axios/test/unit/adapters/http.js b/axios/test/unit/adapters/http.js new file mode 100644 index 0000000..497eb28 --- /dev/null +++ b/axios/test/unit/adapters/http.js @@ -0,0 +1,883 @@ +var axios = require('../../../index'); +var http = require('http'); +var https = require('https'); +var net = require('net'); +var url = require('url'); +var zlib = require('zlib'); +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); +var server, proxy; + +describe('supports http with nodejs', function () { + + afterEach(function () { + if (server) { + server.close(); + server = null; + } + if (proxy) { + proxy.close() + proxy = null; + } + if (process.env.http_proxy) { + delete process.env.http_proxy; + } + if (process.env.no_proxy) { + delete process.env.no_proxy; + } + }); + + it('should respect the timeout property', function (done) { + + server = http.createServer(function (req, res) { + setTimeout(function () { + res.end(); + }, 1000); + }).listen(4444, function () { + var success = false, failure = false; + var error; + + axios.get('http://localhost:4444/', { + timeout: 250 + }).then(function (res) { + success = true; + }).catch(function (err) { + error = err; + failure = true; + }); + + setTimeout(function () { + assert.equal(success, false, 'request should not succeed'); + assert.equal(failure, true, 'request should fail'); + assert.equal(error.code, 'ECONNABORTED'); + assert.equal(error.message, 'timeout of 250ms exceeded'); + done(); + }, 300); + }); + }); + + it('should allow passing JSON', function (done) { + var data = { + firstName: 'Fred', + lastName: 'Flintstone', + emailAddr: 'fred@example.com' + }; + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'application/json;charset=utf-8'); + res.end(JSON.stringify(data)); + }).listen(4444, function () { + axios.get('http://localhost:4444/').then(function (res) { + assert.deepEqual(res.data, data); + done(); + }); + }); + }); + + it('should allow passing JSON with BOM', function (done) { + var data = { + firstName: 'Fred', + lastName: 'Flintstone', + emailAddr: 'fred@example.com' + }; + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'application/json;charset=utf-8'); + var bomBuffer = Buffer.from([0xEF, 0xBB, 0xBF]) + var jsonBuffer = Buffer.from(JSON.stringify(data)); + res.end(Buffer.concat([bomBuffer, jsonBuffer])); + }).listen(4444, function () { + axios.get('http://localhost:4444/').then(function (res) { + assert.deepEqual(res.data, data); + done(); + }); + }); + }); + + it('should redirect', function (done) { + var str = 'test response'; + + server = http.createServer(function (req, res) { + var parsed = url.parse(req.url); + + if (parsed.pathname === '/one') { + res.setHeader('Location', '/two'); + res.statusCode = 302; + res.end(); + } else { + res.end(str); + } + }).listen(4444, function () { + axios.get('http://localhost:4444/one').then(function (res) { + assert.equal(res.data, str); + assert.equal(res.request.path, '/two'); + done(); + }); + }); + }); + + it('should not redirect', function (done) { + server = http.createServer(function (req, res) { + res.setHeader('Location', '/foo'); + res.statusCode = 302; + res.end(); + }).listen(4444, function () { + axios.get('http://localhost:4444/', { + maxRedirects: 0, + validateStatus: function () { + return true; + } + }).then(function (res) { + assert.equal(res.status, 302); + assert.equal(res.headers['location'], '/foo'); + done(); + }); + }); + }); + + it('should support max redirects', function (done) { + var i = 1; + server = http.createServer(function (req, res) { + res.setHeader('Location', '/' + i); + res.statusCode = 302; + res.end(); + i++; + }).listen(4444, function () { + axios.get('http://localhost:4444/', { + maxRedirects: 3 + }).catch(function (error) { + done(); + }); + }); + }); + + it('should preserve the HTTP verb on redirect', function (done) { + server = http.createServer(function (req, res) { + if (req.method.toLowerCase() !== "head") { + res.statusCode = 400; + res.end(); + return; + } + + var parsed = url.parse(req.url); + if (parsed.pathname === '/one') { + res.setHeader('Location', '/two'); + res.statusCode = 302; + res.end(); + } else { + res.end(); + } + }).listen(4444, function () { + axios.head('http://localhost:4444/one').then(function (res) { + assert.equal(res.status, 200); + done(); + }).catch(function (err) { + done(err); + }); + }); + }); + + it('should support transparent gunzip', function (done) { + var data = { + firstName: 'Fred', + lastName: 'Flintstone', + emailAddr: 'fred@example.com' + }; + + zlib.gzip(JSON.stringify(data), function (err, zipped) { + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'application/json;charset=utf-8'); + res.setHeader('Content-Encoding', 'gzip'); + res.end(zipped); + }).listen(4444, function () { + axios.get('http://localhost:4444/').then(function (res) { + assert.deepEqual(res.data, data); + done(); + }); + }); + + }); + }); + + it('should support gunzip error handling', function (done) { + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'application/json;charset=utf-8'); + res.setHeader('Content-Encoding', 'gzip'); + res.end('invalid response'); + }).listen(4444, function () { + axios.get('http://localhost:4444/').catch(function (error) { + done(); + }); + }); + }); + + it('should support disabling automatic decompression of response data', function(done) { + var data = 'Test data'; + + zlib.gzip(data, function(err, zipped) { + server = http.createServer(function(req, res) { + res.setHeader('Content-Type', 'text/html;charset=utf-8'); + res.setHeader('Content-Encoding', 'gzip'); + res.end(zipped); + }).listen(4444, function() { + axios.get('http://localhost:4444/', { + decompress: false, + responseType: 'arraybuffer' + + }).then(function(res) { + assert.equal(res.data.toString('base64'), zipped.toString('base64')); + done(); + }); + }); + }); + }); + + it('should support UTF8', function (done) { + var str = Array(100000).join('ж'); + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end(str); + }).listen(4444, function () { + axios.get('http://localhost:4444/').then(function (res) { + assert.equal(res.data, str); + done(); + }); + }); + }); + + it('should support basic auth', function (done) { + server = http.createServer(function (req, res) { + res.end(req.headers.authorization); + }).listen(4444, function () { + var user = 'foo'; + var headers = { Authorization: 'Bearer 1234' }; + axios.get('http://' + user + '@localhost:4444/', { headers: headers }).then(function (res) { + var base64 = Buffer.from(user + ':', 'utf8').toString('base64'); + assert.equal(res.data, 'Basic ' + base64); + done(); + }); + }); + }); + + it('should support basic auth with a header', function (done) { + server = http.createServer(function (req, res) { + res.end(req.headers.authorization); + }).listen(4444, function () { + var auth = { username: 'foo', password: 'bar' }; + var headers = { Authorization: 'Bearer 1234' }; + axios.get('http://localhost:4444/', { auth: auth, headers: headers }).then(function (res) { + var base64 = Buffer.from('foo:bar', 'utf8').toString('base64'); + assert.equal(res.data, 'Basic ' + base64); + done(); + }); + }); + }); + + it('should support max content length', function (done) { + var str = Array(100000).join('ж'); + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end(str); + }).listen(4444, function () { + var success = false, failure = false, error; + + axios.get('http://localhost:4444/', { + maxContentLength: 2000 + }).then(function (res) { + success = true; + }).catch(function (err) { + error = err; + failure = true; + }); + + setTimeout(function () { + assert.equal(success, false, 'request should not succeed'); + assert.equal(failure, true, 'request should fail'); + assert.equal(error.message, 'maxContentLength size of 2000 exceeded'); + done(); + }, 100); + }); + }); + + it('should support max content length for redirected', function (done) { + var str = Array(100000).join('ж'); + + server = http.createServer(function (req, res) { + var parsed = url.parse(req.url); + + if (parsed.pathname === '/two') { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end(str); + } else { + res.setHeader('Location', '/two'); + res.statusCode = 302; + res.end(); + } + }).listen(4444, function () { + var success = false, failure = false, error; + + axios.get('http://localhost:4444/one', { + maxContentLength: 2000 + }).then(function (res) { + success = true; + }).catch(function (err) { + error = err; + failure = true; + }); + + setTimeout(function () { + assert.equal(success, false, 'request should not succeed'); + assert.equal(failure, true, 'request should fail'); + assert.equal(error.message, 'maxContentLength size of 2000 exceeded'); + done(); + }, 100); + }); + }); + + it('should support max body length', function (done) { + var data = Array(100000).join('ж'); + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end(); + }).listen(4444, function () { + var success = false, failure = false, error; + + axios.post('http://localhost:4444/', { + data: data + }, { + maxBodyLength: 2000 + }).then(function (res) { + success = true; + }).catch(function (err) { + error = err; + failure = true; + }); + + + setTimeout(function () { + assert.equal(success, false, 'request should not succeed'); + assert.equal(failure, true, 'request should fail'); + assert.equal(error.code, 'ERR_FR_MAX_BODY_LENGTH_EXCEEDED'); + assert.equal(error.message, 'Request body larger than maxBodyLength limit'); + done(); + }, 100); + }); + }); + + it('should support sockets', function (done) { + // Different sockets for win32 vs darwin/linux + var socketName = './test.sock'; + + if (process.platform === 'win32') { + socketName = '\\\\.\\pipe\\libuv-test'; + } + + server = net.createServer(function (socket) { + socket.on('data', function () { + socket.end('HTTP/1.1 200 OK\r\n\r\n'); + }); + }).listen(socketName, function () { + axios({ + socketPath: socketName, + url: '/' + }) + .then(function (resp) { + assert.equal(resp.status, 200); + assert.equal(resp.statusText, 'OK'); + done(); + }) + .catch(function (error) { + assert.ifError(error); + done(); + }); + }); + }); + + it('should support streams', function (done) { + server = http.createServer(function (req, res) { + req.pipe(res); + }).listen(4444, function () { + axios.post('http://localhost:4444/', + fs.createReadStream(__filename), { + responseType: 'stream' + }).then(function (res) { + var stream = res.data; + var string = ''; + stream.on('data', function (chunk) { + string += chunk.toString('utf8'); + }); + stream.on('end', function () { + assert.equal(string, fs.readFileSync(__filename, 'utf8')); + done(); + }); + }); + }); + }); + + it('should pass errors for a failed stream', function (done) { + var notExitPath = path.join(__dirname, 'does_not_exist'); + + server = http.createServer(function (req, res) { + req.pipe(res); + }).listen(4444, function () { + axios.post('http://localhost:4444/', + fs.createReadStream(notExitPath) + ).then(function (res) { + assert.fail(); + }).catch(function (err) { + assert.equal(err.message, `ENOENT: no such file or directory, open \'${notExitPath}\'`); + done(); + }); + }); + }); + + it('should support buffers', function (done) { + var buf = Buffer.alloc(1024, 'x'); // Unsafe buffer < Buffer.poolSize (8192 bytes) + server = http.createServer(function (req, res) { + assert.equal(req.headers['content-length'], buf.length.toString()); + req.pipe(res); + }).listen(4444, function () { + axios.post('http://localhost:4444/', + buf, { + responseType: 'stream' + }).then(function (res) { + var stream = res.data; + var string = ''; + stream.on('data', function (chunk) { + string += chunk.toString('utf8'); + }); + stream.on('end', function () { + assert.equal(string, buf.toString()); + done(); + }); + }); + }); + }); + + it('should support HTTP proxies', function (done) { + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('12345'); + }).listen(4444, function () { + proxy = http.createServer(function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path + }; + + http.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(body + '6789'); + }); + }); + + }).listen(4000, function () { + axios.get('http://localhost:4444/', { + proxy: { + host: 'localhost', + port: 4000 + } + }).then(function (res) { + assert.equal(res.data, '123456789', 'should pass through proxy'); + done(); + }); + }); + }); + }); + + it('should support HTTPS proxies', function (done) { + var options = { + key: fs.readFileSync(path.join(__dirname, 'key.pem')), + cert: fs.readFileSync(path.join(__dirname, 'cert.pem')) + }; + + server = https.createServer(options, function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('12345'); + }).listen(4444, function () { + proxy = https.createServer(options, function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path, + protocol: parsed.protocol, + rejectUnauthorized: false + }; + + https.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(body + '6789'); + }); + }); + }).listen(4000, function () { + axios.get('https://localhost:4444/', { + proxy: { + host: 'localhost', + port: 4000, + protocol: 'https' + }, + httpsAgent: new https.Agent({ + rejectUnauthorized: false + }) + }).then(function (res) { + assert.equal(res.data, '123456789', 'should pass through proxy'); + done(); + }).catch(function (err) { + assert.fail(err); + done() + }); + }); + }); + }); + + it('should not pass through disabled proxy', function (done) { + // set the env variable + process.env.http_proxy = 'http://does-not-exists.example.com:4242/'; + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('123456789'); + }).listen(4444, function () { + axios.get('http://localhost:4444/', { + proxy: false + }).then(function (res) { + assert.equal(res.data, '123456789', 'should not pass through proxy'); + done(); + }); + }); + }); + + it('should support proxy set via env var', function (done) { + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('4567'); + }).listen(4444, function () { + proxy = http.createServer(function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path + }; + + http.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(body + '1234'); + }); + }); + + }).listen(4000, function () { + // set the env variable + process.env.http_proxy = 'http://localhost:4000/'; + + axios.get('http://localhost:4444/').then(function (res) { + assert.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy'); + done(); + }); + }); + }); + }); + + it('should support HTTPS proxy set via env var', function (done) { + var options = { + key: fs.readFileSync(path.join(__dirname, 'key.pem')), + cert: fs.readFileSync(path.join(__dirname, 'cert.pem')) + }; + + server = https.createServer(options, function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('12345'); + }).listen(4444, function () { + proxy = https.createServer(options, function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path, + protocol: parsed.protocol, + rejectUnauthorized: false + }; + + https.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(body + '6789'); + }); + }); + }).listen(4000, function () { + process.env.https_proxy = 'https://localhost:4000/'; + + axios.get('https://localhost:4444/', { + httpsAgent: new https.Agent({ + rejectUnauthorized: false + }) + }).then(function (res) { + assert.equal(res.data, '123456789', 'should pass through proxy'); + done(); + }).catch(function (err) { + assert.fail(err); + done() + }).finally(function () { + process.env.https_proxy = '' + }); + }); + }); + }); + + it('should not use proxy for domains in no_proxy', function (done) { + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('4567'); + }).listen(4444, function () { + proxy = http.createServer(function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path + }; + + http.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(body + '1234'); + }); + }); + + }).listen(4000, function () { + // set the env variable + process.env.http_proxy = 'http://localhost:4000/'; + process.env.no_proxy = 'foo.com, localhost,bar.net , , quix.co'; + + axios.get('http://localhost:4444/').then(function (res) { + assert.equal(res.data, '4567', 'should not use proxy for domains in no_proxy'); + done(); + }); + }); + }); + }); + + it('should use proxy for domains not in no_proxy', function (done) { + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('4567'); + }).listen(4444, function () { + proxy = http.createServer(function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path + }; + + http.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(body + '1234'); + }); + }); + + }).listen(4000, function () { + // set the env variable + process.env.http_proxy = 'http://localhost:4000/'; + process.env.no_proxy = 'foo.com, ,bar.net , quix.co'; + + axios.get('http://localhost:4444/').then(function (res) { + assert.equal(res.data, '45671234', 'should use proxy for domains not in no_proxy'); + done(); + }); + }); + }); + }); + + it('should support HTTP proxy auth', function (done) { + server = http.createServer(function (req, res) { + res.end(); + }).listen(4444, function () { + proxy = http.createServer(function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path + }; + var proxyAuth = request.headers['proxy-authorization']; + + http.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(proxyAuth); + }); + }); + + }).listen(4000, function () { + axios.get('http://localhost:4444/', { + proxy: { + host: 'localhost', + port: 4000, + auth: { + username: 'user', + password: 'pass' + } + } + }).then(function (res) { + var base64 = Buffer.from('user:pass', 'utf8').toString('base64'); + assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy'); + done(); + }); + }); + }); + }); + + it('should support proxy auth from env', function (done) { + server = http.createServer(function (req, res) { + res.end(); + }).listen(4444, function () { + proxy = http.createServer(function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path + }; + var proxyAuth = request.headers['proxy-authorization']; + + http.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(proxyAuth); + }); + }); + + }).listen(4000, function () { + process.env.http_proxy = 'http://user:pass@localhost:4000/'; + + axios.get('http://localhost:4444/').then(function (res) { + var base64 = Buffer.from('user:pass', 'utf8').toString('base64'); + assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy set by process.env.http_proxy'); + done(); + }); + }); + }); + }); + + it('should support proxy auth with header', function (done) { + server = http.createServer(function (req, res) { + res.end(); + }).listen(4444, function () { + proxy = http.createServer(function (request, response) { + var parsed = url.parse(request.url); + var opts = { + host: parsed.hostname, + port: parsed.port, + path: parsed.path + }; + var proxyAuth = request.headers['proxy-authorization']; + + http.get(opts, function (res) { + var body = ''; + res.on('data', function (data) { + body += data; + }); + res.on('end', function () { + response.setHeader('Content-Type', 'text/html; charset=UTF-8'); + response.end(proxyAuth); + }); + }); + + }).listen(4000, function () { + axios.get('http://localhost:4444/', { + proxy: { + host: 'localhost', + port: 4000, + auth: { + username: 'user', + password: 'pass' + } + }, + headers: { + 'Proxy-Authorization': 'Basic abc123' + } + }).then(function (res) { + var base64 = Buffer.from('user:pass', 'utf8').toString('base64'); + assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy'); + done(); + }); + }); + }); + }); + + it('should support cancel', function (done) { + var source = axios.CancelToken.source(); + server = http.createServer(function (req, res) { + // call cancel() when the request has been sent, but a response has not been received + source.cancel('Operation has been canceled.'); + }).listen(4444, function () { + axios.get('http://localhost:4444/', { + cancelToken: source.token + }).catch(function (thrown) { + assert.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a Cancel obejct'); + assert.equal(thrown.message, 'Operation has been canceled.'); + done(); + }); + }); + }); + + it('should combine baseURL and url', function (done) { + server = http.createServer(function (req, res) { + res.end(); + }).listen(4444, function () { + axios.get('/foo', { + baseURL: 'http://localhost:4444/', + }).then(function (res) { + assert.equal(res.config.baseURL, 'http://localhost:4444/'); + assert.equal(res.config.url, '/foo'); + done(); + }); + }); + }); +}); + diff --git a/axios/test/unit/adapters/key.pem b/axios/test/unit/adapters/key.pem new file mode 100644 index 0000000..9ce05bf --- /dev/null +++ b/axios/test/unit/adapters/key.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEA+joLfff3WdFIGwrlVdUWfZ9vDr9w86yQpI6Xq0KF6JiKtxa+ +EVCNWtq8Ht5rZd1MxNSFWVb1jxnRLBEB8F3wLD/4xRk8CNxw8joXKsxUEirI76iV +mJDjm/tq90gaxZUWVtPZP95Qvocdl7brhuvLuF4J9wJVL5tr6ZYAje3omHBnjcFb +n9nh3Tqk0U6omAmS4k/BrXWDA1zq8LBmbP41CnfVNGq/5asDIZsNO8s5VQzNjFjN +rcqv2rEO0n+77qDBbqALQydxPmytZP+0jYXcj+EaJ4clApFhvps6Cg0taoF66E4k +Eg1JLALzgk3I/ATSE3nvi3J/RWkyjSYiolPavwIDAQABAoIBAEbMi5ndwjfAlkVI +hPEPNKjgpnymwB/CEL7utY04akkQeBcrsSWXBBfT0exuBDczMVhzxTMs/pe5t0xf +l4vaGG18wDeMV0cukCqJMyrh21u0jVv5+DHNtQjaTz6eQSzsbQCuOkbu8SuncUEO ++X8YUnDc8rbYCyBIOnVCAvAlg201uW0G5G9NEwJOu6cAKMKkogdHqv+FRX96C5hm +gtbGEzpGV2vVClgMwMcX49ucluZvqLvit/yehNVd0VOtW/kuLup4R6q0abHRapDd +95rJAhPvar4mzP+UgJrGQ9hozqhizDthBjnsmGeMBUiBCkay7OXIZpvLoCpQkti1 +WIWuikkCgYEA/oZqq71RT1nPuI7rlcjx3AeWe2EUQtKhQMJBiPx5eLLP6gII8+v2 +pD1qlmJM2eyIK0lzuskLIulTAA5Z+ejORDbvmn/DdT0CSvdrUFrcvnrRQnt2M5M2 +9VDRp6nvPE0H4kRZJrtITyLn0dv5ABf2L32i4dPCMePjKjSUygJSHrsCgYEA+61A +cIqch/lrQTk8hG7Y6p0EJzSInFVaKuZoMYpLhlDQcVvSDIQbGgRAN6BKTdxeQ+tK +hSxBSm2mze11aHig8GBGgdBFLaJOZRo6G+2fl+s1t1FCHfsaFhHwheZJONHMpKKd +Qm/7L/V35QV9YG0lPZ01TM6d5lXuKsmUNvBJTc0CgYASYajAgGqn3WeX/5JZ/eoh +ptaiUG+DJ+0HXUAYYYtwQRGs57q3yvnEAL963tyH/IIVBjf6bFyGh+07ms26s6p5 +2LHTKZj3FZHd0iKI6hb5FquYLoxpyx7z9oM9pZMmerWwDJmXp3zgYjf1uvovnItm +AJ/LyVxD+B5GxQdd028U0wKBgG4OllZglxDzJk7wa6FyI9N89Fr8oxzSSkrmVPwN +APfskSpxP8qPXpai8z4gDz47NtG2q/DOqIKWrtHwnF4iGibjwxFzdTz+dA/MR0r9 +P8QcbHIMy7/2lbK/B5JWYQDC5h28qs5pz8tqKZLyMqCfOiDWhX9f/zbBrxPw8KqR +q0ylAoGAL/0kemA/Tmxpwmp0S0oCqnA4gbCgS7qnApxB09xTewc/tuvraXc3Mzea +EvqDXLXK0R7O4E3vo0Mr23SodRVlFPevsmUUJLPJMJcxdfnSJgX+qE/UC8Ux+UMi +eYufYRDYSslfL2rt9D7abnnbqSfsHymJKukWpElIgJTklQUru4k= +-----END RSA PRIVATE KEY----- diff --git a/axios/test/unit/regression/SNYK-JS-AXIOS-1038255.js b/axios/test/unit/regression/SNYK-JS-AXIOS-1038255.js new file mode 100644 index 0000000..52c7498 --- /dev/null +++ b/axios/test/unit/regression/SNYK-JS-AXIOS-1038255.js @@ -0,0 +1,61 @@ +// https://snyk.io/vuln/SNYK-JS-AXIOS-1038255 +// https://github.com/axios/axios/issues/3407 +// https://github.com/axios/axios/issues/3369 + +const axios = require('../../../index'); +const http = require('http'); +const assert = require('assert'); + +const PROXY_PORT = 4777; +const EVIL_PORT = 4666; + + +describe('Server-Side Request Forgery (SSRF)', () => { + let fail = false; + let proxy; + let server; + let location; + beforeEach(() => { + server = http.createServer(function (req, res) { + fail = true; + res.end('rm -rf /'); + }).listen(EVIL_PORT); + proxy = http.createServer(function (req, res) { + if (req.url === 'http://localhost:' + EVIL_PORT + '/') { + return res.end(JSON.stringify({ + msg: 'Protected', + headers: req.headers, + })); + } + res.writeHead(302, { location }) + res.end() + }).listen(PROXY_PORT); + }); + afterEach(() => { + server.close(); + proxy.close(); + }); + it('obeys proxy settings when following redirects', async () => { + location = 'http://localhost:' + EVIL_PORT; + let response = await axios({ + method: "get", + url: "http://www.google.com/", + proxy: { + host: "localhost", + port: PROXY_PORT, + auth: { + username: 'sam', + password: 'password', + } + }, + }); + + assert.strictEqual(fail, false); + assert.strictEqual(response.data.msg, 'Protected'); + assert.strictEqual(response.data.headers.host, 'localhost:' + EVIL_PORT); + assert.strictEqual(response.data.headers['proxy-authorization'], 'Basic ' + Buffer.from('sam:password').toString('base64')); + + return response; + + }); +});
\ No newline at end of file |