diff --git a/index.js b/index.js index 895311c..a874b60 100644 --- a/index.js +++ b/index.js @@ -60,5 +60,6 @@ var sepiaUtil = require('./src/util'); module.exports.filter = sepiaUtil.addFilter; module.exports.fixtureDir = sepiaUtil.setFixtureDir; module.exports.configure = sepiaUtil.configure; +module.exports.disableGzip = sepiaUtil.disableGzip; module.exports.withSepiaServer = withSepiaServer; module.exports.shutdown = shutdown; diff --git a/package.json b/package.json index cec7fd8..59de83d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "sepia", "author": "Avik Das ", - "version": "2.0.1", + "version": "2.0.4", "description": "A VCR-like module that records HTTP interactions and plays them back for speed and reliability", "keywords": [ "http", diff --git a/src/cache.js b/src/cache.js index b9f9522..ad7a6b5 100644 --- a/src/cache.js +++ b/src/cache.js @@ -52,6 +52,23 @@ module.exports.configure = function(mode) { protocolModule.request = function(options, callback) { var reqUrl = sepiaUtil.urlFromHttpRequestOptions(options, protocol); + var forceLive = sepiaUtil.shouldForceLive(reqUrl); + + if (forceLive) { + // Just pass thru live requests + // websocket connections fail if we proxy + return oldRequest.apply(this, arguments); + } + + // Remove any gzip headers + if (sepiaUtil.gzipDisabled()) { + if (options.headers && options.headers['accept-encoding']) { + var gzipHeaders = ['gzip,deflate']; + options.headers['accept-encoding'] = options.headers['accept-encoding'] + .filter((h) => !gzipHeaders.includes(h)); + } + } + var reqBody = []; var debug = sepiaUtil.shouldFindMatchingFixtures(); @@ -81,8 +98,6 @@ module.exports.configure = function(mode) { options.headers = sepiaUtil.removeInternalHeaders(options.headers); - var forceLive = sepiaUtil.shouldForceLive(reqUrl); - // Only called if either the fixture with the constructed filename // exists, or we're playing back passed in data. function playback(resHeaders, resBody) { @@ -181,10 +196,12 @@ module.exports.configure = function(mode) { var timeLength = Date.now() - startTime; headers.url = reqUrl; headers.time = timeLength; - headers.request = { - method: options.method, - headers: options.headers - }; + // Do not include the request in the header file + // it's not used and there is a separate debug option for this + // headers.request = { + // method: options.method, + // headers: sepiaUtil.parseHeaderNames(options.headers) + // }; fs.writeFileSync(filename + '.headers', JSON.stringify(headers, null, 2)); diff --git a/src/util.js b/src/util.js index 34ac2bc..69ea328 100644 --- a/src/util.js +++ b/src/util.js @@ -50,6 +50,9 @@ function reset() { // These test options are set via an HTTP request to the embedded HTTP server // provided by sepia. The options are reset each time any of them are set. globalOptions.testOptions = {}; + + // Strip out gzip headers so plain text is provided + globalOptions.doNotGzip = false; } // automatically reset the state of the module when 'required'. @@ -91,6 +94,10 @@ function configure(options) { if (options.debug != null) { globalOptions.debug = options.debug; } + + if (options.doNotGzip != null) { + globalOptions.doNotGzip = options.doNotGzip; + } } // This is a commonly-used option, and thus, it should have its own function to @@ -99,6 +106,14 @@ function setFixtureDir(fixtureDir) { globalOptions.filenamePrefix = fixtureDir; } +function disableGzip() { + globalOptions.doNotGzip = true; +} + +function gzipDisabled() { + return globalOptions.doNotGzip; +} + function setTestOptions(options) { globalOptions.testOptions = {}; globalOptions.testOptions.testName = options.testName; @@ -431,6 +446,8 @@ module.exports.reset = reset; module.exports.configure = configure; module.exports.setFixtureDir = setFixtureDir; module.exports.setTestOptions = setTestOptions; +module.exports.disableGzip = disableGzip; +module.exports.gzipDisabled = gzipDisabled; module.exports.addFilter = addFilter; module.exports.constructFilename = constructFilename; module.exports.urlFromHttpRequestOptions = urlFromHttpRequestOptions;