Skip to content

Commit

Permalink
Support decorating responses from proxies
Browse files Browse the repository at this point in the history
Closes #47
  • Loading branch information
bbyars committed Jan 1, 2015
1 parent bf5917b commit 6947af8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
27 changes: 27 additions & 0 deletions functionalTest/api/http/httpProxyStubTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,31 @@ describe('http proxy stubs', function () {
return api.del('/imposters');
});
});

promiseIt('should support decorating response from origin server', function () {
var originServerPort = port + 1,
originServerStub = { responses: [{ is: { body: 'origin server' } }] },
originServerRequest = { protocol: 'http', port: originServerPort, stubs: [originServerStub], name: this.name + ' origin' },
decorator = function (request, response) {
response.headers['X-Test'] = 'decorated';
},
proxyResponse = {
proxy: { to: 'http://localhost:' + originServerPort },
_behaviors: { decorate: decorator.toString() }
},
proxyStub = { responses: [proxyResponse] },
proxyRequest = { protocol: 'http', port: port, stubs: [proxyStub], name: this.name + ' proxy' };

return api.post('/imposters', originServerRequest).then(function () {
return api.post('/imposters', proxyRequest);
}).then(function (response) {
assert.strictEqual(response.statusCode, 201, JSON.stringify(response.body, null, 2));
return client.get('/', port);
}).then(function (response) {
assert.strictEqual(response.body, 'origin server');
assert.strictEqual(response.headers['x-test'], 'decorated', JSON.stringify(response.headers, null, 2));
}).finally(function () {
return api.del('/imposters' );
});
});
});
25 changes: 25 additions & 0 deletions functionalTest/api/tcp/tcpStubTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,30 @@ describe('tcp imposter', function () {
return api.del('/imposters');
});
});

promiseIt('should support decorating response from origin server', function () {
var originServerPort = port + 1,
originServerStub = { responses: [{ is: { data: 'ORIGIN' } }] },
originServerRequest = { protocol: 'tcp', port: originServerPort, stubs: [originServerStub], name: this.name + ' ORIGIN' },
decorator = function (request, response) {
response.data = response.data + ' DECORATED';
},
proxyResponse = {
proxy: { to: { host: 'localhost', port: originServerPort } },
_behaviors: { decorate: decorator.toString() }
},
proxyStub = { responses: [proxyResponse] },
proxyRequest = { protocol: 'tcp', port: port, stubs: [proxyStub], name: this.name + ' PROXY' };

return api.post('/imposters', originServerRequest).then(function () {
return api.post('/imposters', proxyRequest);
}).then(function () {
return tcp.send('request', port);
}).then(function (response) {
assert.strictEqual(response.toString(), 'ORIGIN DECORATED');
}).finally(function () {
return api.del('/imposters');
});
});
});
});
3 changes: 2 additions & 1 deletion src/models/dryRunValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function create (options) {
}

function dryRun (stub, encoding, logger) {
var dryRunProxy = { to: function () { return Q({}); } },
// Need a well-formed proxy response in case a behavior decorator expcets certain fields to exist
var dryRunProxy = { to: function () { return Q(options.testProxyResponse); } },
dryRunLogger = {
debug: combinators.noop,
info: combinators.noop,
Expand Down
5 changes: 5 additions & 0 deletions src/models/http/baseHttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ function setup (protocolName, createNodeServer) {
return DryRunValidator.create({
StubRepository: StubRepository,
testRequest: HttpRequest.createTestRequest(),
testProxyResponse: {
statusCode: 200,
headers: {},
body: ''
},
allowInjection: allowInjection
});
}
Expand Down
1 change: 1 addition & 0 deletions src/models/tcp/tcpValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
return DryRunValidator.create({
StubRepository: StubRepository,
testRequest: TcpRequest.createTestRequest(),
testProxyResponse: { data: '' },
allowInjection: allowInjection,
additionalValidation: validateMode
});
Expand Down

0 comments on commit 6947af8

Please sign in to comment.