node-fetch/test/test.js

67 lines
1.7 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 http = require('http');
var https = require('https');
var bluebird = require('bluebird');
var then = require('promise');
// 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:28:23 -08:00
var url, opts, local;
2015-01-26 02:15:07 -08:00
describe('Fetch', function() {
2015-01-26 05:28:23 -08:00
before(function(done) {
// TODO: create a server instance for testing
local = http.createServer(function(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.write('hello world');
res.end();
});
local.listen(30001, done);
});
after(function(done) {
local.close(done);
2015-01-26 02:15:07 -08:00
});
it('should return a promise', function() {
url = 'http://example.com/';
expect(fetch(url)).to.be.an.instanceof(fetch.Promise);
});
2015-01-26 05:28:23 -08:00
it('should 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;
});
it('should reject with error if url is relative', function() {
url = 'some/path';
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);
});
it('should resolve with result', function() {
2015-01-26 05:28:23 -08:00
url = 'http://127.0.0.1:30001/';
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 02:15:07 -08:00
});
});
});