node-fetch/test/test.js

87 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-01-26 02:15:07 -08:00
// test tools
var chai = require('chai');
var cap = require('chai-as-promised');
chai.use(cap);
var expect = chai.expect;
var bluebird = require('bluebird');
var then = require('promise');
2015-01-26 05:58:52 -08:00
var stream = require('stream');
var TestServer = require('./server');
2015-01-26 02:15:07 -08:00
// test subjects
var fetch = require('../index.js');
// test with native promise on node 0.11, and bluebird for node 0.10
fetch.Promise = fetch.Promise || bluebird;
2015-01-26 05:58:52 -08:00
var url, opts, local, base;
2015-01-26 02:15:07 -08:00
describe('Fetch', function() {
2015-01-26 05:28:23 -08:00
before(function(done) {
2015-01-26 05:58:52 -08:00
local = new TestServer();
base = 'http://' + local.hostname + ':' + local.port;
local.start(done);
2015-01-26 05:28:23 -08:00
});
after(function(done) {
2015-01-26 05:58:52 -08:00
local.stop(done);
2015-01-26 02:15:07 -08:00
});
it('should return a promise', function() {
url = 'http://example.com/';
2015-01-26 05:58:52 -08:00
var p = fetch(url);
expect(p).to.be.an.instanceof(fetch.Promise);
expect(p).to.have.property('then');
2015-01-26 02:15:07 -08:00
});
2015-01-26 05:58:52 -08:00
it('should allow custom promise', function() {
2015-01-26 02:15:07 -08:00
url = 'http://example.com/';
var old = fetch.Promise;
fetch.Promise = then;
expect(fetch(url)).to.be.an.instanceof(then);
fetch.Promise = old;
});
2015-01-26 09:46:32 -08:00
it('should throw error when no promise implementation found', function() {
url = 'http://example.com/';
var old = fetch.Promise;
fetch.Promise = undefined;
expect(function() {
fetch(url)
}).to.throw(Error);
fetch.Promise = old;
});
2015-01-26 05:58:52 -08:00
it('should reject with error if url is protocol relative', function() {
url = '//example.com/';
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);
});
it('should reject with error if url is relative path', function() {
url = '/some/path';
2015-01-26 02:15:07 -08:00
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);
});
it('should reject with error if protocol is unsupported', function() {
url = 'ftp://example.com/';
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);
});
2015-01-26 09:46:32 -08:00
it('should reject with error on network failure', function() {
url = 'http://localhost:50000/';
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);
});
2015-01-26 05:58:52 -08:00
it('should resolve status code, headers, body correctly', function() {
url = base + '/hello';
2015-01-26 02:15:07 -08:00
return fetch(url).then(function(res) {
2015-01-26 05:28:23 -08:00
expect(res.status).to.equal(200);
expect(res.headers).to.include({ 'content-type': 'text/plain' });
2015-01-26 05:58:52 -08:00
expect(res.body).to.be.an.instanceof(stream.Transform);
2015-01-26 09:46:32 -08:00
expect(res.url).to.equal(url);
2015-01-26 02:15:07 -08:00
});
});
2015-01-26 05:58:52 -08:00
2015-01-26 02:15:07 -08:00
});