2014-05-26 16:30:17 +00:00
|
|
|
var network = require('../../../src/network.js');
|
2014-05-23 20:53:50 +00:00
|
|
|
var expect = require('chai').expect;
|
2014-05-19 20:47:24 +00:00
|
|
|
|
2014-05-26 16:30:17 +00:00
|
|
|
describe('Network module', function() {
|
2014-05-23 20:53:50 +00:00
|
|
|
var uri;
|
2014-05-21 17:07:31 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
if (typeof XMLHttpRequest === 'undefined') {
|
|
|
|
// Node context
|
|
|
|
uri = {
|
|
|
|
valid: 'http://localhost:1234/package.json',
|
|
|
|
invalid: 'booyah!',
|
|
|
|
notFound: 'http://localhost:1234/this-isnt-real'
|
2014-05-19 20:47:24 +00:00
|
|
|
}
|
2014-05-23 20:53:50 +00:00
|
|
|
} else {
|
|
|
|
// Browser context
|
|
|
|
uri = {
|
|
|
|
valid: '../package.json',
|
|
|
|
invalid: 'asdf://booyah!',
|
|
|
|
notFound: 'this-isnt-real'
|
|
|
|
};
|
|
|
|
}
|
2014-05-19 20:47:24 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should get an error when a non-existent path is specified', function(done) {
|
|
|
|
network.download(uri.notFound, function(error, data) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.eql(404);
|
|
|
|
expect(data).to.be.eql(null);
|
2014-05-19 20:47:24 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2014-05-19 20:47:24 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
if (typeof XMLHttpRequest === 'undefined') {
|
|
|
|
it('in nodejs, should get an error when an invalid URI is specified', function(done) {
|
|
|
|
network.download(uri.invalid, function(error, data) {
|
2014-05-21 17:07:31 +00:00
|
|
|
expect(error).to.exist;
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(error.code).to.eql(null);
|
|
|
|
expect(error.message).to.exist;
|
2014-05-21 17:07:31 +00:00
|
|
|
expect(data).to.be.eql(null);
|
2014-05-19 20:47:24 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
} else {
|
|
|
|
it('in a browser, should throw an error when an invalid URI is specified', function(done) {
|
|
|
|
expect(function(){
|
|
|
|
network.download(uri.invalid, function() {});
|
|
|
|
}).to.throwError;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2014-05-19 20:47:24 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should download a resource from the server', function(done) {
|
|
|
|
network.download(uri.valid, function(error, data) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(data).to.exist;
|
|
|
|
expect(data).to.have.length.above(0);
|
|
|
|
done();
|
2014-05-19 20:47:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|