2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-23 20:40:40 +03:00

test: use mocha instead of nodeunit (#1655)

This commit is contained in:
Justin Beckwith
2018-07-05 22:41:13 -07:00
committed by GitHub
parent 0c4bf3cdbd
commit 98080381fa
4 changed files with 197 additions and 188 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ Please update the docs accordingly so that there are no discrepencies between th
### Developing ### Developing
- `grunt test` run the jasmine and nodeunit tests - `grunt test` run the jasmine and mocha tests
- `grunt build` run webpack and bundle the source - `grunt build` run webpack and bundle the source
- `grunt version` prepare the code for release - `grunt version` prepare the code for release
- `grunt watch:test` watch for changes and run `test` - `grunt watch:test` watch for changes and run `test`
+8 -3
View File
@@ -65,8 +65,13 @@ module.exports = function(grunt) {
} }
}, },
nodeunit: { mochaTest: {
all: ['test/unit/**/*.js'] test: {
src: ['test/unit/**/*.js']
},
options: {
timeout: 30000,
},
}, },
watch: { watch: {
@@ -96,7 +101,7 @@ module.exports = function(grunt) {
grunt.file.write('bower.json', JSON.stringify(bower, null, 2)); grunt.file.write('bower.json', JSON.stringify(bower, null, 2));
}); });
grunt.registerTask('test', 'Run the jasmine and nodeunit tests', ['eslint', 'nodeunit', 'karma:single', 'ts']); grunt.registerTask('test', 'Run the jasmine and mocha tests', ['eslint', 'mochaTest', 'karma:single', 'ts']);
grunt.registerTask('build', 'Run webpack and bundle the source', ['clean', 'webpack']); grunt.registerTask('build', 'Run webpack and bundle the source', ['clean', 'webpack']);
grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower']); grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower']);
}; };
+5 -4
View File
@@ -39,10 +39,10 @@
"grunt-banner": "^0.6.0", "grunt-banner": "^0.6.0",
"grunt-cli": "^1.2.0", "grunt-cli": "^1.2.0",
"grunt-contrib-clean": "^1.1.0", "grunt-contrib-clean": "^1.1.0",
"grunt-contrib-nodeunit": "^1.0.0",
"grunt-contrib-watch": "^1.0.0", "grunt-contrib-watch": "^1.0.0",
"grunt-eslint": "^20.1.0", "grunt-eslint": "^20.1.0",
"grunt-karma": "^2.0.0", "grunt-karma": "^2.0.0",
"grunt-mocha-test": "^0.13.3",
"grunt-ts": "^6.0.0-beta.19", "grunt-ts": "^6.0.0-beta.19",
"grunt-webpack": "^1.0.18", "grunt-webpack": "^1.0.18",
"istanbul-instrumenter-loader": "^1.0.0", "istanbul-instrumenter-loader": "^1.0.0",
@@ -61,11 +61,12 @@
"karma-webpack": "^1.7.0", "karma-webpack": "^1.7.0",
"load-grunt-tasks": "^3.5.2", "load-grunt-tasks": "^3.5.2",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"mocha": "^5.2.0",
"sinon": "^4.5.0", "sinon": "^4.5.0",
"webpack": "^1.13.1", "typescript": "^2.8.1",
"webpack-dev-server": "^1.14.1",
"url-search-params": "^0.10.0", "url-search-params": "^0.10.0",
"typescript": "^2.8.1" "webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
}, },
"browser": { "browser": {
"./lib/adapters/http.js": "./lib/adapters/xhr.js" "./lib/adapters/http.js": "./lib/adapters/xhr.js"
+110 -107
View File
@@ -3,26 +3,28 @@ var http = require('http');
var net = require('net'); var net = require('net');
var url = require('url'); var url = require('url');
var zlib = require('zlib'); var zlib = require('zlib');
var assert = require('assert');
var fs = require('fs'); var fs = require('fs');
var server, proxy; var server, proxy;
module.exports = { describe('supports http with nodejs', function () {
tearDown: function (callback) {
afterEach(function () {
if (server) {
server.close(); server.close();
server = null; server = null;
}
if (proxy) { if (proxy) {
proxy.close() proxy.close()
proxy = null; proxy = null;
} }
if (process.env.http_proxy) { if (process.env.http_proxy) {
delete process.env.http_proxy; delete process.env.http_proxy;
} }
});
callback(); it('should respect the timeout property', function (done) {
},
testTimeout: function (test) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
setTimeout(function () { setTimeout(function () {
res.end(); res.end();
@@ -41,16 +43,16 @@ module.exports = {
}); });
setTimeout(function () { setTimeout(function () {
test.equal(success, false, 'request should not succeed'); assert.equal(success, false, 'request should not succeed');
test.equal(failure, true, 'request should fail'); assert.equal(failure, true, 'request should fail');
test.equal(error.code, 'ECONNABORTED'); assert.equal(error.code, 'ECONNABORTED');
test.equal(error.message, 'timeout of 250ms exceeded'); assert.equal(error.message, 'timeout of 250ms exceeded');
test.done(); done();
}, 300); }, 300);
}); });
}, });
testJSON: function (test) { it('should allow passing JSON', function (done) {
var data = { var data = {
firstName: 'Fred', firstName: 'Fred',
lastName: 'Flintstone', lastName: 'Flintstone',
@@ -62,13 +64,13 @@ module.exports = {
res.end(JSON.stringify(data)); res.end(JSON.stringify(data));
}).listen(4444, function () { }).listen(4444, function () {
axios.get('http://localhost:4444/').then(function (res) { axios.get('http://localhost:4444/').then(function (res) {
test.deepEqual(res.data, data); assert.deepEqual(res.data, data);
test.done(); done();
});
}); });
}); });
},
testRedirect: function (test) { it('should redirect', function (done) {
var str = 'test response'; var str = 'test response';
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
@@ -83,14 +85,14 @@ module.exports = {
} }
}).listen(4444, function () { }).listen(4444, function () {
axios.get('http://localhost:4444/one').then(function (res) { axios.get('http://localhost:4444/one').then(function (res) {
test.equal(res.data, str); assert.equal(res.data, str);
test.equal(res.request.path, '/two'); assert.equal(res.request.path, '/two');
test.done(); done();
});
}); });
}); });
},
testNoRedirect: function (test) { it('should not redirect', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.setHeader('Location', '/foo'); res.setHeader('Location', '/foo');
res.statusCode = 302; res.statusCode = 302;
@@ -102,14 +104,14 @@ module.exports = {
return true; return true;
} }
}).then(function (res) { }).then(function (res) {
test.equal(res.status, 302); assert.equal(res.status, 302);
test.equal(res.headers['location'], '/foo'); assert.equal(res.headers['location'], '/foo');
test.done(); done();
});
}); });
}); });
},
testMaxRedirects: function (test) { it('should support max redirects', function (done) {
var i = 1; var i = 1;
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.setHeader('Location', '/' + i); res.setHeader('Location', '/' + i);
@@ -120,12 +122,12 @@ module.exports = {
axios.get('http://localhost:4444/', { axios.get('http://localhost:4444/', {
maxRedirects: 3 maxRedirects: 3
}).catch(function (error) { }).catch(function (error) {
test.done(); done();
});
}); });
}); });
},
testTransparentGunzip: function (test) { it('should support transparent gunzip', function (done) {
var data = { var data = {
firstName: 'Fred', firstName: 'Fred',
lastName: 'Flintstone', lastName: 'Flintstone',
@@ -140,27 +142,27 @@ module.exports = {
res.end(zipped); res.end(zipped);
}).listen(4444, function () { }).listen(4444, function () {
axios.get('http://localhost:4444/').then(function (res) { axios.get('http://localhost:4444/').then(function (res) {
test.deepEqual(res.data, data); assert.deepEqual(res.data, data);
test.done(); done();
}); });
}); });
}); });
}, });
testGunzipErrorHandling: function (test) { it('should support gunzip error handling', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'application/json;charset=utf-8'); res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.setHeader('Content-Encoding', 'gzip'); res.setHeader('Content-Encoding', 'gzip');
res.end('invalid response'); res.end('invalid response');
}).listen(4444, function () { }).listen(4444, function () {
axios.get('http://localhost:4444/').catch(function (error) { axios.get('http://localhost:4444/').catch(function (error) {
test.done(); done();
});
}); });
}); });
},
testUTF8: function (test) { it('should support UTF8', function (done) {
var str = Array(100000).join('ж'); var str = Array(100000).join('ж');
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
@@ -168,13 +170,13 @@ module.exports = {
res.end(str); res.end(str);
}).listen(4444, function () { }).listen(4444, function () {
axios.get('http://localhost:4444/').then(function (res) { axios.get('http://localhost:4444/').then(function (res) {
test.equal(res.data, str); assert.equal(res.data, str);
test.done(); done();
});
}); });
}); });
},
testBasicAuth: function (test) { it('should support basic auth', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.end(req.headers.authorization); res.end(req.headers.authorization);
}).listen(4444, function () { }).listen(4444, function () {
@@ -182,13 +184,13 @@ module.exports = {
var headers = { Authorization: 'Bearer 1234' }; var headers = { Authorization: 'Bearer 1234' };
axios.get('http://' + user + '@localhost:4444/', { headers: headers }).then(function (res) { axios.get('http://' + user + '@localhost:4444/', { headers: headers }).then(function (res) {
var base64 = Buffer.from(user + ':', 'utf8').toString('base64'); var base64 = Buffer.from(user + ':', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64); assert.equal(res.data, 'Basic ' + base64);
test.done(); done();
});
}); });
}); });
},
testBasicAuthWithHeader: function (test) { it('should support basic auth with a header', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.end(req.headers.authorization); res.end(req.headers.authorization);
}).listen(4444, function () { }).listen(4444, function () {
@@ -196,13 +198,13 @@ module.exports = {
var headers = { Authorization: 'Bearer 1234' }; var headers = { Authorization: 'Bearer 1234' };
axios.get('http://localhost:4444/', { auth: auth, headers: headers }).then(function (res) { axios.get('http://localhost:4444/', { auth: auth, headers: headers }).then(function (res) {
var base64 = Buffer.from('foo:bar', 'utf8').toString('base64'); var base64 = Buffer.from('foo:bar', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64); assert.equal(res.data, 'Basic ' + base64);
test.done(); done();
});
}); });
}); });
},
testMaxContentLength: function(test) { it('should support max content length', function (done) {
var str = Array(100000).join('ж'); var str = Array(100000).join('ж');
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
@@ -221,15 +223,15 @@ module.exports = {
}); });
setTimeout(function () { setTimeout(function () {
test.equal(success, false, 'request should not succeed'); assert.equal(success, false, 'request should not succeed');
test.equal(failure, true, 'request should fail'); assert.equal(failure, true, 'request should fail');
test.equal(error.message, 'maxContentLength size of 2000 exceeded'); assert.equal(error.message, 'maxContentLength size of 2000 exceeded');
test.done(); done();
}, 100); }, 100);
}); });
}, });
testSocket: function (test) { it.skip('should support sockets', function (done) {
server = net.createServer(function (socket) { server = net.createServer(function (socket) {
socket.on('data', function () { socket.on('data', function () {
socket.end('HTTP/1.1 200 OK\r\n\r\n'); socket.end('HTTP/1.1 200 OK\r\n\r\n');
@@ -240,18 +242,18 @@ module.exports = {
url: '/' url: '/'
}) })
.then(function (resp) { .then(function (resp) {
test.equal(resp.status, 200); assert.equal(resp.status, 200);
test.equal(resp.statusText, 'OK'); assert.equal(resp.statusText, 'OK');
test.done(); done();
}) })
.catch(function (error) { .catch(function (error) {
test.ifError(error); assert.ifError(error);
test.done(); done();
});
}); });
}); });
},
testStream: function(test) { it('should support streams', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
req.pipe(res); req.pipe(res);
}).listen(4444, function () { }).listen(4444, function () {
@@ -265,33 +267,32 @@ module.exports = {
string += chunk.toString('utf8'); string += chunk.toString('utf8');
}); });
stream.on('end', function () { stream.on('end', function () {
test.equal(string, fs.readFileSync(__filename, 'utf8')); assert.equal(string, fs.readFileSync(__filename, 'utf8'));
test.done(); done();
});
}); });
}); });
}); });
},
testFailedStream: function(test) { it('should pass errors for a failed stream', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
req.pipe(res); req.pipe(res);
}).listen(4444, function () { }).listen(4444, function () {
axios.post('http://localhost:4444/', axios.post('http://localhost:4444/',
fs.createReadStream('/does/not/exist') fs.createReadStream('/does/not/exist')
).then(function (res) { ).then(function (res) {
test.fail(); assert.fail();
}).catch(function (err) { }).catch(function (err) {
test.equal(err.message, 'ENOENT: no such file or directory, open \'/does/not/exist\''); assert.equal(err.message, 'ENOENT: no such file or directory, open \'/does/not/exist\'');
test.done(); done();
});
}); });
}); });
},
testBuffer: function(test) { it('should support buffers', function (done) {
var buf = Buffer.from(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes) var buf = Buffer.alloc(1024, 'x'); // Unsafe buffer < Buffer.poolSize (8192 bytes)
buf.fill('x');
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
test.equal(req.headers['content-length'], buf.length.toString()); assert.equal(req.headers['content-length'], buf.length.toString());
req.pipe(res); req.pipe(res);
}).listen(4444, function () { }).listen(4444, function () {
axios.post('http://localhost:4444/', axios.post('http://localhost:4444/',
@@ -304,14 +305,14 @@ module.exports = {
string += chunk.toString('utf8'); string += chunk.toString('utf8');
}); });
stream.on('end', function () { stream.on('end', function () {
test.equal(string, buf.toString()); assert.equal(string, buf.toString());
test.done(); done();
});
}); });
}); });
}); });
},
testHTTPProxy: function(test) { it('should support HTTP proxies', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8'); res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('12345'); res.end('12345');
@@ -342,14 +343,14 @@ module.exports = {
port: 4000 port: 4000
} }
}).then(function (res) { }).then(function (res) {
test.equal(res.data, '123456789', 'should pass through proxy'); assert.equal(res.data, '123456789', 'should pass through proxy');
test.done(); done();
});
}); });
}); });
}); });
},
testHTTPProxyDisabled: function(test) { it('should not pass through disabled proxy', function (done) {
// set the env variable // set the env variable
process.env.http_proxy = 'http://does-not-exists.example.com:4242/'; process.env.http_proxy = 'http://does-not-exists.example.com:4242/';
@@ -360,13 +361,13 @@ module.exports = {
axios.get('http://localhost:4444/', { axios.get('http://localhost:4444/', {
proxy: false proxy: false
}).then(function (res) { }).then(function (res) {
test.equal(res.data, '123456789', 'should not pass through proxy'); assert.equal(res.data, '123456789', 'should not pass through proxy');
test.done(); done();
});
}); });
}); });
},
testHTTPProxyEnv: function(test) { it('should support proxy set via env var', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8'); res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('4567'); res.end('4567');
@@ -395,14 +396,14 @@ module.exports = {
process.env.http_proxy = 'http://localhost:4000/'; process.env.http_proxy = 'http://localhost:4000/';
axios.get('http://localhost:4444/').then(function (res) { axios.get('http://localhost:4444/').then(function (res) {
test.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy'); assert.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy');
test.done(); done();
});
}); });
}); });
}); });
},
testHTTPProxyAuth: function(test) { it('should support HTTP proxy auth', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.end(); res.end();
}).listen(4444, function () { }).listen(4444, function () {
@@ -438,14 +439,14 @@ module.exports = {
} }
}).then(function (res) { }).then(function (res) {
var base64 = Buffer.from('user:pass', 'utf8').toString('base64'); var base64 = Buffer.from('user:pass', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy'); assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');
test.done(); done();
});
}); });
}); });
}); });
},
testHTTPProxyAuthFromEnv: function(test) { it('should support proxy auth from env', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.end(); res.end();
}).listen(4444, function () { }).listen(4444, function () {
@@ -474,14 +475,14 @@ module.exports = {
axios.get('http://localhost:4444/').then(function (res) { axios.get('http://localhost:4444/').then(function (res) {
var base64 = Buffer.from('user:pass', 'utf8').toString('base64'); var base64 = Buffer.from('user:pass', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy set by process.env.http_proxy'); assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy set by process.env.http_proxy');
test.done(); done();
});
}); });
}); });
}); });
},
testHTTPProxyAuthWithHeader: function (test) { it('should support proxy auth with header', function (done) {
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
res.end(); res.end();
}).listen(4444, function () { }).listen(4444, function () {
@@ -520,14 +521,14 @@ module.exports = {
} }
}).then(function (res) { }).then(function (res) {
var base64 = Buffer.from('user:pass', 'utf8').toString('base64'); var base64 = Buffer.from('user:pass', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy'); assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');
test.done(); done();
});
}); });
}); });
}); });
},
testCancel: function(test) { it('should support cancel', function (done) {
var source = axios.CancelToken.source(); var source = axios.CancelToken.source();
server = http.createServer(function (req, res) { server = http.createServer(function (req, res) {
// call cancel() when the request has been sent, but a response has not been received // call cancel() when the request has been sent, but a response has not been received
@@ -536,10 +537,12 @@ module.exports = {
axios.get('http://localhost:4444/', { axios.get('http://localhost:4444/', {
cancelToken: source.token cancelToken: source.token
}).catch(function (thrown) { }).catch(function (thrown) {
test.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a Cancel obejct'); assert.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a Cancel obejct');
test.equal(thrown.message, 'Operation has been canceled.'); assert.equal(thrown.message, 'Operation has been canceled.');
test.done(); done();
}); });
}); });
} });
}; });