mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Added unit tests; Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
+2
-2
@@ -10,6 +10,7 @@ var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
|
|||||||
var transitionalDefaults = require('../defaults/transitional');
|
var transitionalDefaults = require('../defaults/transitional');
|
||||||
var AxiosError = require('../core/AxiosError');
|
var AxiosError = require('../core/AxiosError');
|
||||||
var CanceledError = require('../cancel/CanceledError');
|
var CanceledError = require('../cancel/CanceledError');
|
||||||
|
var parseProtocol = require('../helpers/parseProtocol');
|
||||||
|
|
||||||
module.exports = function xhrAdapter(config) {
|
module.exports = function xhrAdapter(config) {
|
||||||
return new Promise(function dispatchXhrRequest(resolve, reject) {
|
return new Promise(function dispatchXhrRequest(resolve, reject) {
|
||||||
@@ -207,8 +208,7 @@ module.exports = function xhrAdapter(config) {
|
|||||||
requestData = null;
|
requestData = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var tokens = fullPath.split(':', 2);
|
var protocol = parseProtocol(fullPath);
|
||||||
var protocol = tokens.length > 1 && tokens[0];
|
|
||||||
|
|
||||||
if (protocol && [ 'http', 'https', 'file' ].indexOf(protocol) === -1) {
|
if (protocol && [ 'http', 'https', 'file' ].indexOf(protocol) === -1) {
|
||||||
reject(new AxiosError('Unsupported protocol ' + protocol + ':', AxiosError.ERR_BAD_REQUEST, config));
|
reject(new AxiosError('Unsupported protocol ' + protocol + ':', AxiosError.ERR_BAD_REQUEST, config));
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = function parseProtocol(url) {
|
||||||
|
var match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
|
||||||
|
return match && match[1] || '';
|
||||||
|
};
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
var assert = require('assert');
|
||||||
|
var utils = require('../../../lib/utils');
|
||||||
|
var parseProtocol = require('../../../lib/helpers/parseProtocol');
|
||||||
|
|
||||||
|
describe('helpers::parseProtocol', function () {
|
||||||
|
it('should parse protocol part if it exists', function () {
|
||||||
|
utils.forEach({
|
||||||
|
'http://username:password@example.com/': 'http',
|
||||||
|
'ftp:google.com': 'ftp',
|
||||||
|
'sms:+15105550101?body=hello%20there': 'sms',
|
||||||
|
'tel:0123456789' : 'tel',
|
||||||
|
'//google.com': '',
|
||||||
|
'google.com': '',
|
||||||
|
'admin://etc/default/grub' : 'admin',
|
||||||
|
'stratum+tcp://server:port': 'stratum+tcp',
|
||||||
|
'/api/resource:customVerb': '',
|
||||||
|
'https://stackoverflow.com/questions/': 'https',
|
||||||
|
'mailto:jsmith@example.com' : 'mailto',
|
||||||
|
'chrome-extension://1234/<pageName>.html' : 'chrome-extension'
|
||||||
|
}, (expectedProtocol, url) => {
|
||||||
|
assert.strictEqual(parseProtocol(url), expectedProtocol);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user