mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Returned error treated when requesting uncommon URL (#3544)
* Fixing error returned when requesting illegal URL * Adding unit tests http.js * Fixing error message axios invalid request from browser * Adding unit tests requests.spec.js * Adding at utils.js a new method: getProtocol Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
committed by
GitHub
parent
412d3bd607
commit
195c8e5ff5
@@ -481,4 +481,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();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1038,6 +1038,86 @@ 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);
|
||||
})
|
||||
});
|
||||
|
||||
it('should supply a user-agent if one is not specified', function (done) {
|
||||
server = http.createServer(function (req, res) {
|
||||
assert.equal(req.headers["user-agent"], 'axios/' + pkg.version);
|
||||
@@ -1097,6 +1177,5 @@ describe('supports http with nodejs', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user