Permalink
Browse files

Fix #10: Support for node 0.10.x-style APIs

When sepia was tested with the then-request module, it turned out that
sepia was not compatible with that module due not conforming to certain
updated node behavior in the standard library.

Of course, normally, this type of behavior is transparent to users of
node, but sepia has to emulate that behavior, hence the breakage.

Unfortunately, the updated behavior is not compatible with 0.8.x, which
is an existing use case. I'll deprecate it eventually, but not yet. To
support both 0.8.x and 0.10.x, I added some conditional code.
  • Loading branch information...
1 parent dfa1f69 commit 96e2093462d8229da271b5862008aa625d0fe048 Avik Das committed Mar 9, 2015
Showing with 137 additions and 21 deletions.
  1. +4 −2 README.md
  2. +6 −1 examples/run-all-examples.sh
  3. +110 −0 examples/thenRequest.js
  4. +2 −1 package.json
  5. +15 −17 src/cache.js
View
@@ -6,8 +6,10 @@ created to isolate a server from its remote downstream dependencies, for speed
and fault-tolerence.
Sepia should work with any HTTP library in node.js that uses `http#request` and
-`https#request`, though in practice, it has only been tested against [the
-`request` module](https://github.com/mikeal/request).
+`https#request`. In practice, it has been extensively tested against [the
+`request` module](https://github.com/mikeal/request), and there is a test to
+ensure it works with [the `then-request`
+module](https://github.com/then/then-request).
Sepia was developed and is in use at LinkedIn since early 2013. There, it is
used to improve the speed and reliability of the integration test suite for the
@@ -31,11 +31,16 @@ start_test 'basic HTTP request'
VCR_MODE=record node examples/http
VCR_MODE=playback node examples/http
-start_test 'basic HTTP request'
+start_test 'request module'
rm -r fixtures/
VCR_MODE=record node examples/request
VCR_MODE=playback node examples/request
+start_test 'then-request module'
+rm -r fixtures/
+VCR_MODE=record node examples/thenRequest
+VCR_MODE=playback node examples/thenRequest
+
start_test 'cache mode'
rm -r fixtures/
VCR_MODE=cache node examples/cache
@@ -0,0 +1,110 @@
+// Copyright 2015 LinkedIn Corp.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * -- THEN REQUEST -------------------------------------------------------------
+ *
+ * rm -r fixtures
+ * VCR_MODE=record node examples/thenRequest
+ * VCR_MODE=playback node examples/thenRequest
+ *
+ * Tests the ability to use the then-request module, which works with sepia
+ * because it uses http(s).request internally.
+ */
+
+var http = require('http');
+var request = require('then-request');
+var _ = require('lodash');
+var step = require('step');
+require('should');
+var common = require('./common');
+
+common.ensureNonCacheMode('request.js');
+
+require('..');
+
+// -- TEST SERVER --------------------------------------------------------------
+
+// 1. Reverses the value of the x-to-reverse header
+// 2. Uppercases the request body
+
+var httpServer = http.createServer(function(req, res) {
+ var headers = { 'Content-Type': 'text/plain' };
+ var toReverse = req.headers['x-to-reverse'];
+ if (toReverse) {
+ headers['x-reversed'] = toReverse.split('').reverse().join('');
+ }
+
+ req.setEncoding('utf-8');
+ var body = '';
+ req.on('data', function(chunk) {
+ body += chunk;
+ });
+
+ req.on('end', function() {
+ headers['Content-Type'] = 'application/json';
+ var resBody = JSON.stringify({ data: body.toUpperCase() });
+
+ // simulate server latency
+ setTimeout(function() {
+ res.writeHead(200, headers);
+ res.end(resBody);
+ }, 500);
+ });
+}).listen(1337, '0.0.0.0');
+
+// -- HTTP REQUEST -------------------------------------------------------------
+
+function makeHttpRequest(next) {
+ var start = Date.now();
+
+ request('POST', 'http://localhost:1337/upper', {
+ method: 'POST',
+ headers: {
+ 'x-to-reverse': 'hi'
+ },
+ qs: {
+ page: 13
+ },
+ body: 'goodbye-world'
+ }).done(function(res) {
+ var time = Date.now() - start;
+
+ var filteredHeaders = _.pick(res.headers, 'x-reversed');
+ var body = res.getBody().toString();
+ console.log('FROM THEN-REQUEST');
+ console.log(' status :', res.statusCode);
+ console.log(' headers:', JSON.stringify(filteredHeaders));
+ console.log(' body :', body);
+ console.log(' time :', time);
+
+ common.verify(function() {
+ filteredHeaders.should.eql({ 'x-reversed': 'ih' });
+ JSON.parse(body).should.eql({ data: 'GOODBYE-WORLD' });
+ common.shouldBeDynamicallyTimed(time);
+ });
+
+ console.log();
+
+ next();
+ });
+}
+
+// -- RUN EVERYTHING -----------------------------------------------------------
+
+step(
+ function() { setTimeout(this, 100); }, // let the server start up
+ function() { makeHttpRequest(this); },
+ _.bind(httpServer.close, httpServer)
+);
View
@@ -1,7 +1,7 @@
{
"name": "sepia",
"author": "Avik Das <avik.das@berkeley.edu>",
- "version": "2.0.0",
+ "version": "2.0.1",
"description": "A VCR-like module that records HTTP interactions and plays them back for speed and reliability",
"keywords": [
"http",
@@ -28,6 +28,7 @@
},
"devDependencies": {
"request": "2.x",
+ "then-request": "2.x",
"lodash": "2.x",
"step": "0.x",
"jshint": "2.x",
View
@@ -15,6 +15,7 @@
var fs = require('fs');
var sepiaUtil = require('./util');
var EventEmitter = require('events').EventEmitter;
+var http = require('http');
var playbackHits = true;
var recordMisses = true;
@@ -92,6 +93,9 @@ module.exports.configure = function(mode) {
var socket = new EventEmitter();
socket.setTimeout = socket.setEncoding = function() {};
+ // Needed for node 0.8.x
+ socket.destroy = socket.pause = socket.resume = function() {};
+
req.socket = socket;
req.emit('socket', socket);
@@ -106,24 +110,10 @@ module.exports.configure = function(mode) {
return;
}
- var res = new EventEmitter();
+ var res = new http.IncomingMessage(socket);
res.headers = resHeaders.headers || {};
res.statusCode = resHeaders.statusCode;
- // Flesh out the response because the request module expects these
- // properties to be present.
- res.connection = {
- listeners: function() { return []; },
- once: function() {},
- setMaxListeners: function() {},
- client: {
- authorized: true
- }
- };
-
- res.setEncoding = function() {};
- res.abort = res.pause = res.resume = function() {};
-
if (callback) {
callback(res);
}
@@ -133,8 +123,16 @@ module.exports.configure = function(mode) {
}
req.emit('response', res);
- res.emit('data', resBody);
- res.emit('end');
+
+ if (res.push) {
+ // node 0.10.x
+ res.push(resBody);
+ res.push(null);
+ } else {
+ // node 0.8.x
+ res.emit('data', resBody);
+ res.emit('end');
+ }
}
// If the file exists and we allow playback (e.g. we are not in

0 comments on commit 96e2093

Please sign in to comment.