Skip to content

Commit

Permalink
Merge c61512b into b4c9f28
Browse files Browse the repository at this point in the history
  • Loading branch information
g-m-a committed Aug 5, 2019
2 parents b4c9f28 + c61512b commit 5a2e085
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,21 @@ export class HttpClient {
followAllRedirects: true,
};

if (attachments.length > 0) {
if (exoptions.alwaysMTOM || attachments.length > 0) {
const start = uuid();
let action = null;
if (headers['Content-Type'].indexOf('action') > -1) {
for (const ct of headers['Content-Type'].split('; ')) {
if (ct.indexOf('action') > -1) {
action = ct;
}
}
}
headers['Content-Type'] =
'multipart/related; type="application/xop+xml"; start="<' + start + '>"; start-info="text/xml"; boundary=' + uuid();
if (action) {
headers['Content-Type'] = headers['Content-Type'] + '; ' + action;
}
const multipart: any[] = [{
'Content-Type': 'application/xop+xml; charset=UTF-8; type="text/xml"',
'Content-ID': '<' + start + '>',
Expand Down
65 changes: 65 additions & 0 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,71 @@ var fs = require('fs'),
});
});

describe('SOAP 1.2 and MTOM binary data', function (){
var server = null;
var hostname = '127.0.0.1';
var port = 15099;
var baseUrl = 'http://' + hostname + ':' + port;

var attachment = {
mimetype: 'image/png',
contentId: 'file_0',
name: 'nodejs.png',
body: fs.createReadStream(__dirname + '/static/nodejs.png')
};

function parsePartHeaders(part) {
const headersAndBody = part.split(/\r\n\r\n/);
const headersParts = headersAndBody[0].split(/\r\n/);
const headers = {};
headersParts.forEach(header => {
let index;
if ((index = header.indexOf(':')) > -1) {
headers[header.substring(0, index)] = header.substring(index + 1).trim();
}
});
return headers;
}

before(function (done) {
server = http.createServer(function (req, res) {
var bufs = [];
req.on('data', function (chunk) {
bufs.push(chunk);
});
req.on('end', function () {
const body = Buffer.concat(bufs).toString().trim();
console.log(body);
const headers = req.headers;
const boundary = headers['content-type'].match(/boundary="?([^"]*"?)/)[1];
const parts = body.split(new RegExp('--' + boundary + '-{0,2}'))
.filter(part => part)
.map(parsePartHeaders);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ contentType: headers['content-type'], parts: parts }), 'utf8');
});
}).listen(port, hostname, done);
});

after(function (done) {
server.close();
server = null;
done();
});

it('Should preserve SOAP 1.2 "action" header when sending MTOM request', function (done) {
soap.createClient(__dirname + '/wsdl/attachments.wsdl', _.assign({ forceSoap12Headers: true }, meta.options), function (initError, client) {
assert.ifError(initError);

client.MyOperation({}, function (error, response, body, soapHeader, rawRequest) {
assert.ifError(error);
assert(body.contentType.indexOf('action') > -1);
done();
}, { attachments: [attachment] })
}, baseUrl)
})

})

describe('Headers in request and last response', function () {
var server = null;
Expand Down

0 comments on commit 5a2e085

Please sign in to comment.