Skip to content

Commit

Permalink
refactor: support empty response body
Browse files Browse the repository at this point in the history
  • Loading branch information
langjt committed Oct 2, 2019
1 parent 12e2613 commit 9b9f7d3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const zlib = require('zlib');
const concatStream = require('concat-stream');
const BufferHelper = require('bufferhelper');
const isString = function (obj) {
return Object.prototype.toString.call(obj) === '[object String]';
}

/**
* Modify the response of json
Expand Down Expand Up @@ -71,7 +74,6 @@ function handleCompressed(res, _write, _end, unzip, zip, callback) {
body = JSON.parse(data.toString());
} catch (e) {
body = data.toString();
console.log('JSON.parse error:', e);
}

// Custom modified logic
Expand All @@ -80,8 +82,12 @@ function handleCompressed(res, _write, _end, unzip, zip, callback) {
}

let finish = _body => {
// empty response body
if (!_body) {
_body = '';
}
// Converts the JSON to buffer.
let body = new Buffer(JSON.stringify(_body));
let body = new Buffer(isString(_body) ? _body : JSON.stringify(_body));

// Call the response method and recover the content-encoding.
zip.on('data', chunk => _write.call(res, chunk));
Expand Down Expand Up @@ -115,7 +121,6 @@ function handleUncompressed(res, _write, _end, callback) {
body = JSON.parse(buffer.toBuffer().toString());
} catch (e) {
body = buffer.toBuffer().toString();
console.log('JSON.parse error:', e);
}

// Custom modified logic
Expand All @@ -124,8 +129,12 @@ function handleUncompressed(res, _write, _end, callback) {
}

let finish = _body => {
// empty response body
if (!_body) {
_body = '';
}
// Converts the JSON to buffer.
let body = new Buffer(JSON.stringify(_body));
let body = new Buffer(isString(_body) ? _body : JSON.stringify(_body));

// Call the response method
_write.call(res, body);
Expand Down
8 changes: 6 additions & 2 deletions test/brotli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const modifyResponse = require('../');
const SERVER_PORT = 5000;
const TARGET_SERVER_PORT = 5001;

const isObject = function (obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

describe('modifyResponse--brotli', function() {
if (typeof zlib.createBrotliCompress !== 'function') {
return console.log('Brotli not available. Skipping "modifyResponse--brotli" test.');
Expand Down Expand Up @@ -74,7 +78,7 @@ describe('modifyResponse--brotli', function() {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand Down Expand Up @@ -112,7 +116,7 @@ describe('modifyResponse--brotli', function() {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand Down
18 changes: 11 additions & 7 deletions test/deflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const modifyResponse = require('../');
const SERVER_PORT = 5002;
const TARGET_SERVER_PORT = 5003;

const isObject = function (obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

describe('modifyResponse--deflate', () => {
let proxy, server, targetServer;
beforeEach(() => {
Expand All @@ -28,7 +32,7 @@ describe('modifyResponse--deflate', () => {

// Create your target server
targetServer = http
.createServer(function(req, res) {
.createServer(function (req, res) {
// Create deflated content
let deflate = zlib.Deflate();
let _write = res.write;
Expand Down Expand Up @@ -69,7 +73,7 @@ describe('modifyResponse--deflate', () => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand All @@ -86,10 +90,10 @@ describe('modifyResponse--deflate', () => {
res.pipe(inflate);

inflate
.on('data', function(chunk) {
.on('data', function (chunk) {
body += chunk;
})
.on('end', function() {
.on('end', function () {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
Expand All @@ -106,7 +110,7 @@ describe('modifyResponse--deflate', () => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand All @@ -123,10 +127,10 @@ describe('modifyResponse--deflate', () => {
res.pipe(inflate);

inflate
.on('data', function(chunk) {
.on('data', function (chunk) {
body += chunk;
})
.on('end', function() {
.on('end', function () {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
Expand Down
9 changes: 6 additions & 3 deletions test/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ const modifyResponse = require('../');

const SERVER_PORT = 5000;
const TARGET_SERVER_PORT = 5001;
const isObject = function (obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

describe('modifyResponse--gzip', function() {
describe('modifyResponse--gzip', function () {
let proxy, server, targetServer;
beforeEach(() => {
// Create a proxy server
Expand Down Expand Up @@ -70,7 +73,7 @@ describe('modifyResponse--gzip', function() {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand Down Expand Up @@ -108,7 +111,7 @@ describe('modifyResponse--gzip', function() {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand Down
11 changes: 7 additions & 4 deletions test/uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ const modifyResponse = require('../');

const SERVER_PORT = 5004;
const TARGET_SERVER_PORT = 5005;
const isObject = function (obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

describe('modifyResponse--uncompressed', function() {
describe('modifyResponse--uncompressed', function () {
let proxy, server, targetServer;
beforeEach(() => {
// Create a proxy server
Expand Down Expand Up @@ -51,9 +54,9 @@ describe('modifyResponse--uncompressed', function() {
describe('callback returns data', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function(proxyRes, req, res) {
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand Down Expand Up @@ -83,7 +86,7 @@ describe('modifyResponse--uncompressed', function() {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes, body => {
if (body) {
if (isObject(body)) {
// modify some information
body.age = 2;
delete body.version;
Expand Down

0 comments on commit 9b9f7d3

Please sign in to comment.