-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 96e2093
Showing
5 changed files
with
137 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "sepia", | ||
"author": "Avik Das <[email protected]>", | ||
"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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters