basic streaming body
This commit is contained in:
parent
78853fc544
commit
f57ebe10df
2
index.js
2
index.js
|
@ -27,7 +27,7 @@ function Fetch(url, opts) {
|
|||
return new Fetch(url, opts);
|
||||
|
||||
if (!Fetch.Promise) {
|
||||
throw new Error('native promise missing, set Fetch.Promise to your favorite substitute');
|
||||
throw new Error('native promise missing, set Fetch.Promise to your favorite alternative');
|
||||
}
|
||||
|
||||
return new Fetch.Promise(function(resolve, reject) {
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
|
||||
var http = require('http');
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports = TestServer;
|
||||
|
||||
function TestServer() {
|
||||
this.server = http.createServer(this.router);
|
||||
this.port = 30001;
|
||||
this.hostname = '127.0.0.1';
|
||||
this.server.on('error', function(err) {
|
||||
console.log(err.stack);
|
||||
});
|
||||
}
|
||||
|
||||
TestServer.prototype.start = function(cb) {
|
||||
this.server.listen(this.port, this.hostname, cb);
|
||||
}
|
||||
|
||||
TestServer.prototype.stop = function(cb) {
|
||||
this.server.close(cb);
|
||||
}
|
||||
|
||||
TestServer.prototype.router = function(req, res) {
|
||||
|
||||
var p = parse(req.url).pathname;
|
||||
|
||||
if (p === '/hello') {
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
res.end('world');
|
||||
}
|
||||
|
||||
}
|
40
test/test.js
40
test/test.js
|
@ -4,41 +4,38 @@ 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');
|
||||
var stream = require('stream');
|
||||
var TestServer = require('./server');
|
||||
|
||||
// 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;
|
||||
|
||||
var url, opts, local;
|
||||
var url, opts, local, base;
|
||||
|
||||
describe('Fetch', function() {
|
||||
|
||||
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);
|
||||
local = new TestServer();
|
||||
base = 'http://' + local.hostname + ':' + local.port;
|
||||
local.start(done);
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
local.close(done);
|
||||
local.stop(done);
|
||||
});
|
||||
|
||||
it('should return a promise', function() {
|
||||
url = 'http://example.com/';
|
||||
expect(fetch(url)).to.be.an.instanceof(fetch.Promise);
|
||||
var p = fetch(url);
|
||||
expect(p).to.be.an.instanceof(fetch.Promise);
|
||||
expect(p).to.have.property('then');
|
||||
});
|
||||
|
||||
it('should custom promise', function() {
|
||||
it('should allow custom promise', function() {
|
||||
url = 'http://example.com/';
|
||||
var old = fetch.Promise;
|
||||
fetch.Promise = then;
|
||||
|
@ -46,8 +43,13 @@ describe('Fetch', function() {
|
|||
fetch.Promise = old;
|
||||
});
|
||||
|
||||
it('should reject with error if url is relative', function() {
|
||||
url = 'some/path';
|
||||
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';
|
||||
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);
|
||||
});
|
||||
|
||||
|
@ -56,11 +58,13 @@ describe('Fetch', function() {
|
|||
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);
|
||||
});
|
||||
|
||||
it('should resolve with result', function() {
|
||||
url = 'http://127.0.0.1:30001/';
|
||||
it('should resolve status code, headers, body correctly', function() {
|
||||
url = base + '/hello';
|
||||
return fetch(url).then(function(res) {
|
||||
expect(res.status).to.equal(200);
|
||||
expect(res.headers).to.include({ 'content-type': 'text/plain' });
|
||||
expect(res.body).to.be.an.instanceof(stream.Transform);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue