Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bencevans committed Mar 25, 2014
1 parent b5051be commit 597251f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function(grunt) {
mochaTest: {
serverTest: {
options: {
reporter: 'dot',
reporter: 'spec',
},
src: ['test/**/*.js']
}
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var getInputStream = function(config, callback) {
//
var getOutputStream = function(config, callback) {

config.method = config.method || 'POST';
config.method = config.method || 'PUT';

var req = http.request(config);

Expand All @@ -51,7 +51,7 @@ var getOutputStream = function(config, callback) {
});

req.on('response', function(res) {
callback(null, res);
callback(null, req);
});

};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "test"
},
"scripts": {
"test": "./node-modules/.bin/grunt",
"test": "./node_modules/.bin/grunt",
"coveralls": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -- -u exports --report lcovonly -- -R spec -- test/*.js && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage"
},
"repository": {
Expand Down Expand Up @@ -35,6 +35,7 @@
"grunt-contrib-jshint": "~0.8.0",
"grunt-cli": "^0.1.13",
"iostreams": "^1.0.0",
"express": "^3.5.0"
"express": "^3.5.0",
"multiparty": "^3.2.3"
}
}
42 changes: 21 additions & 21 deletions test/iostreams-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,18 @@
var ioStream = require('iostreams')();
var express = require('express');
var http = require('http');
var fs = require('fs');
var assert = require('assert');
var crypto = require('crypto');
var streamHash = require('./streamHash');
var provider = require('../');
var app, server;

ioStream.use(provider);

function streamHash(stream, cb) {
var hash = crypto.createHash('md5');
stream.on('data', function(data) {
hash.update(data);
});
stream.on('end', function() {
cb(null, hash.digest('hex'));
});
}

describe('iostreams-http', function() {

this.timeout(5000);

before(function(done) {
app = express();
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/assets'))
app.all('/300', function(req, res) { res.send(300); });
app.all('/199', function(req, res) { res.send(199); });
server = http.createServer(app).listen(3000, done);
require('./server').listen(3000, done);
});

describe('getProviderByProtocol', function() {
Expand All @@ -55,13 +39,13 @@ describe('iostreams-http', function() {
});
});
it('should provide an error given a non 2xx status code (more than 299)', function(done) {
ioStream.getInputStream('http://localhost:3000/300', function(err, stream) {
ioStream.getInputStream('http://localhost:3000/300', function(err) {
assert.equal(err.message, 'Non 2xx statusCode: 300');
done();
});
});
it('should provide an error given a non 2xx status code (less than 200)', function(done) {
ioStream.getInputStream('http://localhost:3000/199', function(err, stream) {
ioStream.getInputStream('http://localhost:3000/199', function(err) {
assert.equal(err.message, 'Non 2xx statusCode: 199');
done();
});
Expand All @@ -72,6 +56,22 @@ describe('iostreams-http', function() {
describe('getOutputStream', function() {

it('should provide a stream given a correct http string');
// it('should provide a stream given a correct http string', function(done) {
// ioStream.getOutputStream('http://localhost:3000/get_hash', function(err, stream) {
// assert.ifError(err);
// var kitten = fs.createReadStream(__dirname + '/assets/kitten2.jpg');
// var buf = '';
// console.log(stream.res)
// stream.on('data', function(data) {
// console.log('send:', data)
// buf += data
// });
// stream.on('end', function() {
// assert.equal(buf, 'b5bbe413bbdc2536205bb798fe19ea27');
// })
// kitten.pipe(stream);
// });
// });
it('should provide an error given a http string with no http at path');
it('should provide an error given a correct object input');
it('should provide an error given a http object with no http at path');
Expand Down
43 changes: 43 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

var http = require('http');
var fs = require('fs');
var streamHash = require('./streamHash');


var app = function(req, res) {

if(req.url === '/get_hash') {
streamHash(req, function(err, hash) {
if(err) {
res.statusCode = 500;
res.end(err.stack);
} else {
res.statusCode = 200;
res.end(hash);
}
});
req.resume();
} else if(req.url === '/199') {
res.statusCode = 199;
res.end('199');
} else if(req.url === '/300') {
res.statusCode = 300;
res.end('300');
} else {

var path = __dirname + '/assets' + req.url;

fs.exists(path, function(exists) {
if(exists) {
fs.createReadStream(path).pipe(res);
} else {
res.statusCode = 404;
res.end('Unknown Path');
}
});

}

}

module.exports = http.createServer(app);
16 changes: 16 additions & 0 deletions test/streamHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

var crypto = require('crypto');

module.exports = function streamHash(stream, cb) {

var hash = crypto.createHash('md5');

stream.on('data', function(data) {
hash.update(data);
});

stream.on('end', function() {
cb(null, hash.digest('hex'));
});

}

0 comments on commit 597251f

Please sign in to comment.