Skip to content

Commit

Permalink
Preserves leading and trailing whitespace when preserveWhitespace opt…
Browse files Browse the repository at this point in the history
…ion is true

 add test response-preserve-whitespace-test.js
  • Loading branch information
yffrankwang committed Apr 27, 2024
1 parent 8652457 commit 6f22534
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/wsdl/index.ts
Expand Up @@ -353,7 +353,9 @@ export class WSDL {
const name = splitQName(nsName).name;

if (typeof cur.schema === 'string' && (cur.schema === 'string' || cur.schema.split(':')[1] === 'string')) {
if (typeof obj === 'object' && Object.keys(obj).length === 0) { obj = cur.object = ''; }
if (typeof obj === 'object' && Object.keys(obj).length === 0) {
obj = cur.object = (this.options.preserveWhitespace ? cur.text || '' : '');
}
}

if (cur.nil === true) {
Expand Down Expand Up @@ -420,13 +422,17 @@ export class WSDL {
};

p.ontext = (text) => {
const top = stack[stack.length - 1];

const originalText = text;
text = trim(text);
if (!text.length) {
if (this.options.preserveWhitespace) {
top.text = (top.text || '') + originalText;
}
return;
}

const top = stack[stack.length - 1];
const name = splitQName(top.schema).name;
let value;

Expand Down
62 changes: 62 additions & 0 deletions test/response-preserve-whitespace-test.js
@@ -0,0 +1,62 @@
'use strict';

var request = require('request');
var assert = require('assert');
var http = require('http');
var soap = require('../');
var server;
var port;

describe('Preverse whitespace', function() {
var wsdl = __dirname + '/wsdl/hello.wsdl';

before(function(done) {
server = http.createServer(function(req, res) {
res.statusCode = 200;
res.end('"<?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> </tns:greeting></tns:sayHelloResponse></soap:Body></soap:Envelope>"');
}).listen(51515, done);
});

after(function() {
server.close();
});

it('preserves leading and trailing whitespace when preserveWhitespace option is true',
function(done) {
var 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;
}

soap.createClient(
wsdl,
{
endpoint: url,
disableCache: true, // disable wsdl cache, otherwise 'mocha test/client-response-options-test.js test/response-preserve-whitespace-test.js' will fail.
preserveWhitespace: true
},
function(err, client) {
if (err) {
console.log(err);
throw err;
}

client.sayHello(
{
firstName: 'hello world'
},
function(err, result, rawResponse, soapHeader, rawRequest) {
if (err) {
console.log(err);
throw err;
}
assert.equal(' ', result.greeting);
done();
}
);
}
);
});

});

0 comments on commit 6f22534

Please sign in to comment.