From f57ebe10df65f00c8294114b51a5e540a509f36a Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 26 Jan 2015 21:58:52 +0800 Subject: [PATCH] basic streaming body --- index.js | 2 +- test/server.js | 34 ++++++++++++++++++++++++++++++++++ test/test.js | 40 ++++++++++++++++++++++------------------ 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index a9d9a75..f8a14fd 100644 --- a/index.js +++ b/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) { diff --git a/test/server.js b/test/server.js index e69de29..f162acc 100644 --- a/test/server.js +++ b/test/server.js @@ -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'); + } + +} diff --git a/test/test.js b/test/test.js index 24eba7f..e58a214 100644 --- a/test/test.js +++ b/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); }); }); + });