Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

basic csv gut server with a test

  • Loading branch information...
commit bdce17f8b5244743b66d58b57ea7c9cc9a75f07e 0 parents
@maxogden authored
43 csv.js
@@ -0,0 +1,43 @@
+var vm = require('vm'),
+ _ = require('underscore'),
+ async = require('async'),
+ csv = require('csv'),
+ request = require('request').defaults({json: true}),
+ http = require('http'),
+ crypto = require('crypto')
+ ;
+
+module.exports = http.createServer(function (req, res) {
+ var headers, dataset, rows = [];
+
+ csv()
+ .fromStream(req)
+ .on('data',function(data, index) {
+ if (!headers) {
+ headers = data;
+ return;
+ }
+ var row = {}
+ _(_.zip(headers, data)).each(function(tuple) {
+ row[_.first(tuple)] = _.last(tuple)
+ })
+ rows.push(row);
+ })
+ .on('end', function(count) {
+ res.statusCode = 202;
+ res.end();
+ headers = _.map(headers, function(head) { return {name: head} })
+ request({uri: req.headers['x-callback'], method: "POST", body: {headers: headers, rows: rows}}, function(e,r,b) {
+ if (e) console.log('upload error on ' + dataset + ': ' + e);
+ });
+ })
+ .on('error',function(error){
+ console.log("csv error!", error.message);
+ });
+})
+
+// sandbox = {
+// animal: 'cat',
+// count: 2
+// };
+// vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm');
5 package.json
@@ -0,0 +1,5 @@
+{
+ "name": "gut",
+ "scripts": { "test": "node test/csv.js" }, // TODO dynamically run ALL the tests!
+ "version": "0.0.1"
+}
1  test/.bin/node-http-proxy
75 test/csv.js
@@ -0,0 +1,75 @@
+/**
+ * integration tests for csv.js
+ * run via 'npm test'. no output means everything passed!
+ */
+
+var it = require('it-is')
+ , _ = require('underscore')
+ , request = require('request')
+ , http = require('http')
+ , asyncTester = require('./test-helper')
+ , csvPipe = require('../csv')
+ ;
+
+var receiver = function(callback) {
+ return http.createServer(function (req, res) {
+ var incoming = "";
+ req
+ .on('data', function(data) { incoming += data })
+ .on('end', function() {
+ callback(incoming);
+ res.statusCode = 200;
+ res.end();
+ })
+ .on('error',function(error) {
+ throw new Error("receiver error!", error);
+ });
+ })
+}
+
+function waitForData(callback) {
+ var start = new Date();
+ if( (new Date() - start) > 5000 ) callback(true, "took too long")
+ if (receivedData) {
+ callback(false, false, receivedData)
+ } else {
+ setTimeout(function() { waitForData(callback) }, 100)
+ }
+}
+
+var csvReceiver, csvData = "name,appearance\nchewbacca,hairy", jsonData = {name: "chewbacca", appearance: "hairy"}, receivedData;
+
+asyncTester.run([
+ {
+ description: "parse csv",
+ setup: [
+ function(next) {
+ csvPipe.listen(8000, 'localhost')
+ csvReceiver = receiver(function(data) { receivedData = data })
+ csvReceiver.listen(9000, 'localhost')
+ next()
+ }
+ ],
+ requests: {
+ send: function(cb) {
+ request({
+ uri: 'http://localhost:8000',
+ headers: {'content-type': 'text/csv', 'x-callback': 'http://localhost:9000'},
+ body: csvData
+ }, cb)
+ },
+ received: function(cb) { waitForData(cb) }
+ },
+ asserts: function(err, results, done) {
+ done(null, "parse csv");
+ },
+ cleanup: [
+ function(next) {
+ csvReceiver.close()
+ csvPipe.close()
+ receivedData = false
+ next()
+ }
+ ]
+ }
+])
22 test/test-helper.js
@@ -0,0 +1,22 @@
+var _ = require('underscore')
+ , async = require('async')
+ ;
+
+exports.run = function(testObjects) {
+ var tests = _.map(testObjects, function(test) {
+ return function(done) {
+ async.series(test.setup, function(err, results) {
+ if(err) throw new Error("setup error: " + err);
+ async.series(test.requests, function(err, results) {
+ async.series(test.cleanup, function(e, r) {
+ if(e) throw new Error("cleanup error: " + r);
+ test.asserts(err, results, done);
+ })
+ });
+ })
+ }
+ })
+ async.series(tests, function(err, results) {
+ console.log("passed:", results);
+ })
+}
Please sign in to comment.
Something went wrong with that request. Please try again.