Skip to content

Commit 40b056d

Browse files
Natan Carvalhodhmlau
authored andcommitted
feat: adding option to disable server pretty formatting
Signed-off-by: Natan Carvalho <[email protected]>
1 parent 577a896 commit 40b056d

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

src/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Base extends EventEmitter {
6666
this.wsdl.options.attributesKey = options.attributesKey || 'attributes';
6767
this.wsdl.options.envelopeKey = options.envelopeKey || 'soap';
6868
this.wsdl.options.forceSoapVersion = options.forceSoapVersion;
69+
this.wsdl.options.prettyResponse = options.prettyResponse !== undefined ? options.prettyResponse : true;
6970
}
7071

7172
static createSOAPEnvelope(prefix, nsURI) {

src/server.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ class Server extends Base {
325325
self.xmlHandler.jsonToXml(envelope.body, nsContext, outputBodyDescriptor, result);
326326

327327
self._envelope(envelope, includeTimestamp);
328-
var message = envelope.body.toString({pretty: true});
329-
var xml = envelope.doc.end({pretty: true});
328+
var message = envelope.body.toString({pretty: self.wsdl.options.prettyResponse});
329+
var xml = envelope.doc.end({pretty: self.wsdl.options.prettyResponse});
330330

331331
debug('Server handleResult. xml: %s ', xml);
332332
callback(xml);
@@ -423,9 +423,8 @@ class Server extends Base {
423423
this.xmlHandler.jsonToXml(envelope.body, nsContext, faultDescriptor, error.Fault);
424424

425425
self._envelope(envelope, includeTimestamp);
426-
var message = envelope.body.toString({pretty: true});
427-
var xml = envelope.doc.end({pretty: true});
428-
426+
var message = envelope.body.toString({pretty: self.wsdl.options.prettyResponse});
427+
var xml = envelope.doc.end({pretty: self.wsdl.options.prettyResponse});
429428
debug('Server sendError. Response envelope: %s ', xml);
430429
callback(xml, statusCode);
431430
}

test/server-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,62 @@ it('should return a valid error if the server stops responding': function(done)
498498
*/
499499

500500
});
501+
502+
describe('SOAP Server Options', function() {
503+
before(function(done) {
504+
fs.readFile(__dirname + '/wsdl/strict/stockquote.wsdl', 'utf8', function(err, data) {
505+
assert.ok(!err);
506+
test.wsdl = data;
507+
done();
508+
});
509+
});
510+
511+
var startServer = function(optionsOverride) {
512+
test.server = http.createServer(function(req, res) {
513+
res.statusCode = 404;
514+
res.end();
515+
});
516+
517+
var options = {
518+
path: '/stockquote',
519+
services: test.service,
520+
xml: test.wsdl,
521+
...optionsOverride
522+
};
523+
524+
test.server.listen(15099, null, null, function() {
525+
test.soapServer = soap.listen(test.server, options);
526+
test.baseUrl =
527+
'http://' + test.server.address().address + ":" + test.server.address().port;
528+
529+
//windows return 0.0.0.0 as address and that is not
530+
//valid to use in a request
531+
if (test.server.address().address === '0.0.0.0' || test.server.address().address === '::') {
532+
test.baseUrl =
533+
'http://127.0.0.1:' + test.server.address().port;
534+
}
535+
});
536+
};
537+
538+
afterEach(function(done) {
539+
test.server.close(function() {
540+
test.server = null;
541+
delete test.soapServer;
542+
test.soapServer = null;
543+
done();
544+
});
545+
});
546+
547+
it('should return response without newlines and white spaces', function(done) {
548+
startServer({prettyResponse: false});
549+
soap.createClient(test.baseUrl + '/stockquote?wsdl', function(err, client) {
550+
assert.ok(!err);
551+
var expectedBody = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header/><soap:Body><ns1:TradePrice xmlns:ns1="http://example.com/stockquote.xsd"><price>19.56</price></ns1:TradePrice></soap:Body></soap:Envelope>';
552+
client.GetLastTradePrice({TradePriceRequest: { tickerSymbol: 'AAPL'}}, function(err, res, body) {
553+
assert.ok(!err);
554+
assert.strictEqual(body.toString(), expectedBody);
555+
done();
556+
});
557+
});
558+
});
559+
});

0 commit comments

Comments
 (0)