diff --git a/lib/transport-utils.js b/lib/transport-utils.js index c7cd3f3..675ed3d 100644 --- a/lib/transport-utils.js +++ b/lib/transport-utils.js @@ -99,7 +99,6 @@ internals.Utils.prototype.handle_response = function (seneca, data, client_optio return false } - var actinfo = { id: data.id, accept: data.accept, @@ -234,14 +233,34 @@ internals.Utils.prototype.handle_request = function (seneca, data, listen_option input.id$ = data.id - this.requestAct(seneca, input, output, respond) + this.requestAct(seneca, input, output, respond, listen_options.inward, listen_options.outward) } -internals.Utils.prototype.requestAct = function (seneca, input, output, respond) { +internals.Utils.prototype.requestAct = function (seneca, input, output, respond, inward, outward) { var self = this try { + if (inward) { + inward({ seneca }, { msg: input }) + } seneca.act(input, function (err, out) { + if (outward) { + try { + var outward_data = { + err: err, + msg: input, + res: out + } + outward({ seneca }, outward_data) + err = outward_data.err + out = outward_data.res + } + catch (e) { + // outward failed, keep a trace of the original err if there was one + e.act_error = err + err = e + } + } self.update_output(input, output, err, out) respond(output) }) diff --git a/test/http.test.js b/test/http.test.js index 6669786..7707238 100644 --- a/test/http.test.js +++ b/test/http.test.js @@ -68,7 +68,6 @@ describe('Specific http', function () { }) }) - it('http-query', function (fin) { CreateInstance({errhandler: fin}) .add('a:1', function (args, done) { @@ -193,6 +192,106 @@ describe('Specific http', function () { }) }) }) + + it('apply inward/outward listen options on HTTP remote act call', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'http', port: '18997', + inward: (context, data) => { + data.msg.bar += 1 + data.msg.inward = 'INPUT UPGRADED' + }, + outward: (context, data) => { + data.res.BAR += 10 + data.res.inward = data.msg.inward + data.res.outward = 'OUTPUT UPGRADED' + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18997'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 13) + Assert.equal(out.inward, 'INPUT UPGRADED') + Assert.equal(out.outward, 'OUTPUT UPGRADED') + fin() + }) + }) + }) + + it('reject HTTP remote act call in inward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'http', port: '18996', + inward: (context, data) => { + var e = new Error('HTTP inward rejected!') + e.error_code = 'inward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18996'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'inward_rejected') + if (err) return fin() + fin(new Error('Inward does not reject remote call')) + }) + }) + }) + + it('reject HTTP remote act call in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'http', port: '18995', + outward: (context, data) => { + var e = new Error('HTTP outward rejected!') + e.error_code = 'outward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18995'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'outward_rejected') + if (err) return fin() + fin(new Error('Outward does not reject remote call')) + }) + }) + }) + + it('catch HTTP remote act call error in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(new Error('Catchable failure'), {BAR: args.bar}) + }) + .listen({type: 'http', port: '18994', + outward: (context, data) => { + delete data.err + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'http', port: '18994'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 2) + fin() + }) + }) + }) }) describe('Specific https', function () { diff --git a/test/misc.test.js b/test/misc.test.js index 6f3e132..a9535cc 100644 --- a/test/misc.test.js +++ b/test/misc.test.js @@ -44,7 +44,6 @@ describe('Miscellaneous', function () { counters.own++ } - var a = CreateInstance({ log: {map: [ {level: 'debug', regex: /\{a:1\}/, handler: log_a}, @@ -66,7 +65,6 @@ describe('Miscellaneous', function () { .listen({type: type, port: 40406}) .client({type: type, port: 40405}) - a.ready(function () { b.ready(function () { a.act('a:1', function (err, out) { @@ -193,7 +191,6 @@ describe('Miscellaneous', function () { .listen({type: type, port: 40407}) .client({type: type, port: 40405}) - a.ready(function () { b.ready(function () { c.ready(function () { @@ -375,6 +372,7 @@ describe('Miscellaneous', function () { }) }) }) + it('listen-http-pin (#97)', function (fin) { CreateInstance() .add('foo:1', function (args, done) { diff --git a/test/tcp.test.js b/test/tcp.test.js index f4806d2..5fcf1cf 100644 --- a/test/tcp.test.js +++ b/test/tcp.test.js @@ -65,7 +65,6 @@ describe('Specific tcp', function () { var settings = {tcp: {port: 0, host: 'localhost'}} - var transportUtil = new TransportUtil({ callmap: {}, seneca: seneca, @@ -215,5 +214,105 @@ describe('Specific tcp', function () { setTimeout(finish, 2000) }) + + it('apply inward/outward listen options on TCP remote act call', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19997', + inward: (context, data) => { + data.msg.bar += 1 + data.msg.inward = 'INPUT UPGRADED' + }, + outward: (context, data) => { + data.res.BAR += 10 + data.res.inward = data.msg.inward + data.res.outward = 'OUTPUT UPGRADED' + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19997'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 13) + Assert.equal(out.inward, 'INPUT UPGRADED') + Assert.equal(out.outward, 'OUTPUT UPGRADED') + fin() + }) + }) + }) + + it('reject TCP remote act call in inward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19996', + inward: (context, data) => { + var e = new Error('TCP inward rejected!') + e.error_code = 'inward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19996'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'inward_rejected') + if (err) return fin() + fin(new Error('Inward does not reject remote call')) + }) + }) + }) + + it('reject TCP remote act call in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(null, { BAR: args.bar }) + }) + .listen({type: 'tcp', port: '19995', + outward: (context, data) => { + var e = new Error('TCP outward rejected!') + e.error_code = 'outward_rejected' + throw e + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19995'}) + + siClient.act('foo:1,bar:2', function (err, out) { + Assert.equal(err.error_code, 'outward_rejected') + if (err) return fin() + fin(new Error('Outward does not reject remote call')) + }) + }) + }) + + it('catch TCP remote act call error in outward listen option', function (fin) { + CreateInstance() + .add('foo:1', function (args, done) { + done(new Error('Catchable failure'), {BAR: args.bar}) + }) + .listen({type: 'tcp', port: '19994', + outward: (context, data) => { + delete data.err + } + }) + .ready(function () { + var siClient = CreateInstance() + .client({type: 'tcp', port: '19994'}) + + siClient.act('foo:1,bar:2', function (err, out) { + if (err) return fin(err) + Assert.equal(out.BAR, 2) + fin() + }) + }) + }) }) })