Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returned error treated when requesting uncommon URL #3544

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/adapters/http.js
Expand Up @@ -91,6 +91,14 @@ module.exports = function httpAdapter(config) {
var parsed = url.parse(fullPath);
var protocol = parsed.protocol || 'http:';

if (parsed.path === null) {
return reject(createError('Malformed URL ' + fullPath, config));
}

if (!utils.supportedProtocols.includes(protocol)) {
return reject(createError('Unsupported protocol ' + protocol, config));
}

if (!auth && parsed.auth) {
var urlAuth = parsed.auth.split(':');
var urlUsername = urlAuth[0] || '';
Expand Down
14 changes: 14 additions & 0 deletions lib/adapters/xhr.js
Expand Up @@ -8,6 +8,7 @@ var buildFullPath = require('../core/buildFullPath');
var parseHeaders = require('./../helpers/parseHeaders');
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
var createError = require('../core/createError');
var url = require('url');

module.exports = function xhrAdapter(config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
Expand All @@ -28,6 +29,9 @@ module.exports = function xhrAdapter(config) {
}

var fullPath = buildFullPath(config.baseURL, config.url);
var parsed = url.parse(fullPath);
var protocol = parsed.protocol || 'http:';
joaoGabriel55 marked this conversation as resolved.
Show resolved Hide resolved

request.open(config.method.toUpperCase(), buildURL(fullPath, config.params, config.paramsSerializer), true);

// Set the request timeout in MS
Expand Down Expand Up @@ -173,6 +177,16 @@ module.exports = function xhrAdapter(config) {
requestData = null;
}

if (parsed.path === null) {
reject(createError('Malformed URL ' + fullPath, config));
return;
}

if (!utils.supportedProtocols.includes(protocol)) {
reject(createError('Unsupported protocol ' + protocol, config));
return;
}

// Send the request
request.send(requestData);
});
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Expand Up @@ -6,6 +6,11 @@ var bind = require('./helpers/bind');

var toString = Object.prototype.toString;

/**
* Array with axios supported protocols.
*/
var supportedProtocols = [ 'http:', 'https:', 'file:' ];
joaoGabriel55 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Determine if a value is an Array
*
Expand Down Expand Up @@ -324,6 +329,7 @@ function stripBOM(content) {
}

module.exports = {
supportedProtocols: supportedProtocols,
isArray: isArray,
isArrayBuffer: isArrayBuffer,
isBuffer: isBuffer,
Expand Down
49 changes: 49 additions & 0 deletions test/specs/requests.spec.js
Expand Up @@ -436,4 +436,53 @@ describe('requests', function () {
done();
});
});

it('should support HTTP protocol', function (done) {
var response;

axios.get('/foo')
.then(function (res) {
response = res
})

getAjaxRequest().then(function (request) {
expect(request.method).toBe('GET');
request.respondWith({
status: 200
});
done();
});
});

it('should support HTTPS protocol', function (done) {
var response;
axios.get('https://www.google.com')
.then(function (res) {
response = res
})

getAjaxRequest().then(function (request) {
expect(request.method).toBe('GET');
request.respondWith({
status: 200
});
done();
});
});

it('should return malformed url error message', function (done) {
axios.get('tel:484-695-3408')
.catch(function (error) {
expect(error.message).toEqual('Malformed URL tel:484-695-3408')
done();
})
});

it('should return unsupported protocol error message', function (done) {
axios.get('ftp:google.com')
.catch(function (error) {
expect(error.message).toEqual('Unsupported protocol ftp:')
done();
})
});
});
80 changes: 80 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -879,5 +879,85 @@ describe('supports http with nodejs', function () {
});
});
});

it('should support HTTP protocol', function (done) {
server = http.createServer(function (req, res) {
setTimeout(function () {
res.end();
}, 1000);
}).listen(4444, function () {
axios.get('http://localhost:4444')
.then(function (res) {
assert.equal(res.request.agent.protocol, 'http:');
done();
})
})
});

it('should support HTTPS protocol', function (done) {
server = http.createServer(function (req, res) {
setTimeout(function () {
res.end();
}, 1000);
}).listen(4444, function () {
axios.get('https://www.google.com')
.then(function (res) {
assert.equal(res.request.agent.protocol, 'https:');
done();
})
})
});

it('should return malformed URL', function (done) {
var success = false, failure = false;
var error;

server = http.createServer(function (req, res) {
setTimeout(function () {
res.end();
}, 1000);
}).listen(4444, function () {
axios.get('tel:484-695-3408')
.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, 'Malformed URL tel:484-695-3408');
done();
}, 300);
})
});

it('should return unsupported protocol', function (done) {
var success = false, failure = false;
var error;

server = http.createServer(function (req, res) {
setTimeout(function () {
res.end();
}, 1000);
}).listen(4444, function () {
axios.get('ftp:google.com')
.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, 'Unsupported protocol ftp:');
done();
}, 300);
})
});
});