Skip to content

Commit

Permalink
emit response events allowing user override on response XML
Browse files Browse the repository at this point in the history
  • Loading branch information
Дмитрий Бойков committed May 28, 2019
1 parent 7b4fb5d commit f348df9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.

Expand Down
11 changes: 10 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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') {
Expand Down
97 changes: 97 additions & 0 deletions test/server-response-event-test.js
Original file line number Diff line number Diff line change
@@ -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 = '<definitions name="HelloService" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><message name="SayHelloRequest"><part name="firstName" type="xsd:string"/></message><message name="SayHelloResponse"><part name="greeting" type="xsd:string"/></message><portType name="Hello_PortType"><operation name="sayHello"><input message="tns:SayHelloRequest"/><output message="tns:SayHelloResponse"/></operation></portType><binding name="Hello_Binding" type="tns:Hello_PortType"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="sayHello"><soap:operation soapAction="sayHello"/><input><soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/></input><output><soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/></output></operation></binding><service name="Hello_Service"><documentation>WSDL File for HelloService</documentation><port binding="tns:Hello_Binding" name="Hello_Port"><soap:address location="http://localhost:51515/SayHello/" /></port></service></definitions>';
var requestXML = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">' +
'<Body>' +
'<sayHello xmlns="http://www.examples.com/wsdl/HelloService.wsdl">' +
'<firstName>Bob</firstName>' +
'</sayHello>' +
'</Body>' +
'</Envelope>';

var responseXML = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl">' +
'<soap:Body>' +
'<tns:sayHelloResponse>' +
'<tns:greeting>Bob</tns:greeting>' +
'</tns:sayHelloResponse>' +
'</soap:Body>' +
'</soap:Envelope>';

var responseXMLChanged = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl">' +
'<soap:Body>' +
'<tns:sayHelloResponse>' +
'<tns:greeting>John</tns:greeting>' +
'</tns:sayHelloResponse>' +
'</soap:Body>' +
'</soap:Envelope>';

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();
});
});

});

0 comments on commit f348df9

Please sign in to comment.