From f348df9867d126842210fc38c3a9287d757874ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=91=D0=BE?= =?UTF-8?q?=D0=B9=D0=BA=D0=BE=D0=B2?= Date: Tue, 28 May 2019 13:38:54 +0300 Subject: [PATCH] emit response events allowing user override on response XML --- Readme.md | 2 + src/server.ts | 11 +++- test/server-response-event-test.js | 97 ++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 test/server-response-event-test.js diff --git a/Readme.md b/Readme.md index 799b9803a..ec50a0aeb 100644 --- a/Readme.md +++ b/Readme.md @@ -270,6 +270,8 @@ Server instances emit the following events: * request - Emitted for every received messages. The signature of the callback is `function(request, methodName)`. +* response - Emitted before sending SOAP response. + The signature of the callback is `function(response, methodName)`. * headers - Emitted when the SOAP Headers are not empty. The signature of the callback is `function(headers, methodName)`. diff --git a/src/server.ts b/src/server.ts index f5019b041..6a7a6f9b6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -51,11 +51,14 @@ function getDateString(d) { export interface Server { emit(event: 'request', request: any, methodName: string): boolean; emit(event: 'headers', headers: any, methodName: string): boolean; + emit(event: 'response', headers: any, methodName: string): boolean; /** Emitted for every received messages. */ on(event: 'request', listener: (request: any, methodName: string) => void): this; /** Emitted when the SOAP Headers are not empty. */ on(event: 'headers', listener: (headers: any, methodName: string) => void): this; + /** Emitted before sending SOAP response. */ + on(event: 'response', listener: (response: any, methodName: string) => void): this; } interface IExecuteMethodOptions { @@ -277,7 +280,7 @@ export class Server extends EventEmitter { } } - private _process(input, req: Request, callback: (result: any, statusCode?: number) => any) { + private _process(input, req: Request, cb: (result: any, statusCode?: number) => any) { const pathname = url.parse(req.url).pathname.replace(/\/$/, ''); const obj = this.wsdl.xmlToObject(input); const body = obj.Body; @@ -289,6 +292,12 @@ export class Server extends EventEmitter { const includeTimestamp = obj.Header && obj.Header.Security && obj.Header.Security.Timestamp; const authenticate = this.authenticate || function defaultAuthenticate() { return true; }; + const callback = (result, statusCode) => { + const response = { result: result }; + this.emit('response', response, methodName); + cb(response.result, statusCode); + }; + const process = () => { if (typeof this.log === 'function') { diff --git a/test/server-response-event-test.js b/test/server-response-event-test.js new file mode 100644 index 000000000..441837f3e --- /dev/null +++ b/test/server-response-event-test.js @@ -0,0 +1,97 @@ +'use strict'; + +var request = require('request'); +var assert = require('assert'); +var http = require('http'); +var soap = require('../'); +var server; +var url; + +var wsdl = 'WSDL File for HelloService'; +var requestXML = '' + + '' + + '' + + 'Bob' + + '' + + '' + + ''; + +var responseXML = '' + + '' + + '' + + '' + + 'Bob' + + '' + + '' + + ''; + +var responseXMLChanged = '' + + '' + + '' + + '' + + 'John' + + '' + + '' + + ''; + + var service = { + Hello_Service: { + Hello_Port: { + sayHello: function (args) { + return { + greeting: args.firstName + }; + } + } + } + }; + +describe('server response event test', function () { + + before(function (done) { + + + server = http.createServer(function(request,response) { + response.end('404: Not Found: ' + request.url); + }); + + server.listen(51515, function () { + var soapServer = soap.listen(server, '/SayHello', service, wsdl); + + soapServer.on('response', function(response, methodName){ + assert.equal(response.result, responseXML); + assert.equal(methodName, 'sayHello'); + response.result = response.result.replace('Bob','John'); + }); + + url = 'http://' + server.address().address + ':' + server.address().port; + if (server.address().address === '0.0.0.0' || server.address().address === '::') { + url = 'http://127.0.0.1:' + server.address().port; + } + done(); + }); + }); + + after(function () { + server.close(); + }); + + it('should replace Bob with John', function (done) { + request({ + url: url + '/SayHello', + method: 'POST', + headers: { + SOAPAction: "sayHello", + "Content-Type": 'text/xml; charset="utf-8"' + }, + body: requestXML + }, function (err, response, body) { + if (err) { + throw err; + } + assert.equal(body, responseXMLChanged); + done(); + }); + }); + +});