Navigation Menu

Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Added ability for requests to already be buffered
Browse files Browse the repository at this point in the history
Decided to use less documentation in favor of confusing documentation

Wrote up README example but not satisfied with it

Added assertion as promised

Added callback logic with buffered body

Added raw body parser

Added body parsing

Added new tests for buffered body

Moving back to req.body

Deciding to "expose" forwardRequestMessage

Created forwardBufferedRequest to use, yey

Moving to options for buffer

Exposing Message to avoid future complications

Fixed up timing complications

Fixed missing property

Starting to allow existing body
  • Loading branch information
twolfson committed May 16, 2014
1 parent d9372ad commit 495c188
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -82,6 +82,9 @@ eightTrack({
});
```

If you need to buffer the data before passing it off to `eight-track` that is supported as well.
The requirement is that you record the data as a `Buffer` or `String` to `req.body`.

#### `normalizeFn` libraries
- `multipart/form-data` - Ignore randomly generated boundaries and consolidate similar `multipart/form-data` requests
- Website: https://github.com/twolfson/eight-track-normalize-multipart
Expand Down
10 changes: 10 additions & 0 deletions lib/eight-track.js
Expand Up @@ -160,9 +160,18 @@ EightTrack.prototype = {
return callback(null, connInfo.response, connInfo.response.body);
}

// Create marker for request loading before we get to `loadIncomingBody` listener
var localReqLoaded = false;
localReqMsg.on('loaded', function updateLoadedState () {
localReqLoaded = true;
});

var that = this;
async.waterfall([
function loadIncomingBody (cb) {
if (localReqLoaded) {
return process.nextTick(cb);
}
localReqMsg.on('loaded', cb);
},
function findSavedConnection (cb) {
Expand Down Expand Up @@ -241,6 +250,7 @@ function middlewareCreator(options) {

// Expose class on top of middlewareCreator
middlewareCreator.EightTrack = EightTrack;
middlewareCreator.Message = Message;

// Expose our middleware constructor
module.exports = middlewareCreator;
11 changes: 10 additions & 1 deletion lib/message.js
Expand Up @@ -8,13 +8,22 @@ var concat = require('concat-stream');
// Create connection
function Message(connection) {
// Inherit from event emitter
var that = this;
EventEmitter.call(this);

// Save the connection
this.connection = connection;

// If there is a body already, don't buffer
if (connection.body) {
that.body = connection.body;
async.setImmediate(function () {
that.emit('loaded');
});
return;
}

// Buffer the content of the connecftion
var that = this;
connection.pipe(concat(function handleBuffer (buff) {
// Save the body and emit a `loaded` event
// DEV: The delay is so `async.waterfall` still operates when we enter it
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -47,7 +47,8 @@
"express": "~3.4.7",
"grunt-cli": "~0.1.11",
"request-mocha": "~0.2.0",
"pem": "~1.4.0"
"pem": "~1.4.0",
"raw-body": "~1.1.5"
},
"keywords": [
"http",
Expand All @@ -58,4 +59,4 @@
"middleware",
"8-track"
]
}
}
70 changes: 70 additions & 0 deletions test/buffered-body_test.js
@@ -0,0 +1,70 @@
var expect = require('chai').expect;
var express = require('express');
var rawBody = require('raw-body');
var eightTrack = require('../');
var httpUtils = require('./utils/http');
var serverUtils = require('./utils/server');

// TODO: This should be a separate repo
function connectRawBody(req, res, next) {
rawBody(req, function handleBody (err, buff) {
if (err) {
next(err);
}
req.body = buff;
next();
});
}

describe('A server that asserts content before talking to eight-track', function () {
serverUtils.run(1337, [
express.urlencoded(),
function (req, res) {
res.send(req.body);
}
]);
serverUtils.run(1338, [
connectRawBody,
function assertInfo (req, res, next) {
expect(req.body.toString()).to.equal('hello=world');
next();
},
eightTrack({
fixtureDir: __dirname + '/actual-files/buffered-body',
url: 'http://localhost:1337'
})
]);
serverUtils._cleanupEightTrack(__dirname + '/actual-files/buffered-body');

describe('when requested', function () {
httpUtils.save({
form: {
hello: 'world'
},
url: 'http://localhost:1338/'
});

it('replies with a our header', function () {
expect(this.err).to.equal(null);
expect(this.res.statusCode).to.equal(200);
expect(JSON.parse(this.body)).to.have.property('hello', 'world');
});

describe('when requested again', function () {
httpUtils.save({
form: {
hello: 'world'
},
url: 'http://localhost:1338/'
});

it('has the same header', function () {
expect(JSON.parse(this.body)).to.have.property('hello', 'world');
});

it('does not double request', function () {
expect(this.requests[1337]).to.have.property('length', 1);
});
});
});
});
16 changes: 12 additions & 4 deletions test/utils/server.js
Expand Up @@ -24,7 +24,12 @@ exports._run = function (listenFn, port, middlewares) {
});

// Use our middlewares and start listening
app.use(middlewares);
if (!Array.isArray(middlewares)) {
middlewares = [middlewares];
}
middlewares.forEach(function (middleware) {
app.use(middleware);
});
_app = listenFn(app, port);
});
after(function deleteServer (done) {
Expand Down Expand Up @@ -63,9 +68,12 @@ exports.runHttps = function (port, middlewares) {
};

// Start an eight-track server
exports.runEightServer = function (port, options) {
exports.run(port, eightTrack(options));
exports._cleanupEightTrack = function (fixtureDir) {
after(function cleanupEightTrack (done) {
rimraf(options.fixtureDir, done);
rimraf(fixtureDir, done);
});
};
exports.runEightServer = function (port, options) {
exports.run(port, eightTrack(options));
exports._cleanupEightTrack(options.fixtureDir);
};

0 comments on commit 495c188

Please sign in to comment.