2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-05 16:42:32 +03:00

Merge branch 'master' into cancel

This commit is contained in:
Nick Uraltsev
2016-09-15 21:09:29 -07:00
19 changed files with 357 additions and 49 deletions
+111
View File
@@ -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();
});
});
});
+12
View File
@@ -48,4 +48,16 @@ describe('utils::forEach', function () {
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);
});
});
+7 -2
View File
@@ -22,10 +22,15 @@ const config: AxiosRequestConfig = {
responseType: 'json',
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
progress: (progressEvent: any) => {},
onUploadProgress: (progressEvent: any) => {},
onDownloadProgress: (progressEvent: any) => {},
maxContentLength: 2000,
validateStatus: (status: number) => status >= 200 && status < 300,
maxRedirects: 5
maxRedirects: 5,
proxy: {
host: '127.0.0.1',
port: 9000
}
};
const handleResponse = (response: AxiosResponse) => {
+84 -1
View File
@@ -3,12 +3,21 @@ var http = require('http');
var url = require('url');
var zlib = require('zlib');
var fs = require('fs');
var server;
var server, proxy;
module.exports = {
tearDown: function (callback) {
server.close();
server = null;
if (proxy) {
proxy.close()
proxy = null;
}
if (process.env.http_proxy) {
delete process.env.http_proxy;
}
callback();
},
@@ -237,5 +246,79 @@ module.exports = {
});
});
});
},
testProxy: function(test) {
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) {
test.equal(res.data, '123456789', 'should pass through proxy');
test.done();
});
});
});
},
testHTTPProxyEnv: function(test) {
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) {
test.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy');
test.done();
});
});
});
}
};