Skip to content

Commit

Permalink
added forceMTOM option and updated the Readme (#1086)
Browse files Browse the repository at this point in the history
* fixed MTOM removing soap12header

* cleaned code + added test

* added forceMTOM extra request option to send MTOM requests even if no attachement

* Update Readme.md
  • Loading branch information
g-m-a authored and jsdevel committed Aug 8, 2019
1 parent 5af5fab commit aee79a8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,19 @@ The `options` object is optional and is passed to the `request`-module.
Interesting properties might be:
* `timeout`: Timeout in milliseconds
* `forever`: Enables keep-alive connections and pools them
* `attachments`: array of attachment objects. This converts the request into MTOM: _headers['Content-Type']='multipart/related; type="application/xop+xml"; start= ... '_
```
[{
mimetype: content mimetype,
contentId: part id,
name: file name,
body: binary data
},
...
]
```
* `forceMTOM`: set to True if you want to send the request as MTOM even if you don't have attachments


### Client.*method*Async(args) - call *method* on the SOAP service.

Expand Down
6 changes: 3 additions & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class HttpClient {
const mergeOptions = ['headers'];
const attachments: IAttachment[] = exoptions.attachments || [];

if (typeof data === 'string' && attachments.length === 0) {
if (typeof data === 'string' && attachments.length === 0 && !exoptions.forceMTOM) {
headers['Content-Length'] = Buffer.byteLength(data, 'utf8');
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
Expand All @@ -85,7 +85,7 @@ export class HttpClient {
followAllRedirects: true,
};

if (exoptions.alwaysMTOM || attachments.length > 0) {
if (exoptions.forceMTOM || attachments.length > 0) {
const start = uuid();
let action = null;
if (headers['Content-Type'].indexOf('action') > -1) {
Expand All @@ -105,6 +105,7 @@ export class HttpClient {
'Content-ID': '<' + start + '>',
'body': data,
}];

attachments.forEach((attachment) => {
multipart.push({
'Content-Type': attachment.mimetype,
Expand Down Expand Up @@ -163,7 +164,6 @@ export class HttpClient {
) {
const options = this.buildRequest(rurl, data, exheaders, exoptions);
let req: req.Request;

if (exoptions !== undefined && exoptions.hasOwnProperty('ntlm')) {
// sadly when using ntlm nothing to return
// Not sure if this can be handled in a cleaner way rather than an if/else,
Expand Down
26 changes: 25 additions & 1 deletion test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ var fs = require('fs'),
});
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}'))
Expand Down Expand Up @@ -290,6 +289,31 @@ var fs = require('fs'),
}, baseUrl)
})

it('Should send MTOM request even without attachment', 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);
const contentType = {};
body.contentType.split(/;\s?/).forEach(dir => {
const keyValue = dir.match(/(.*)="?([^"]*)?/);
if (keyValue && keyValue.length > 2) {
contentType[keyValue[1].trim()] = keyValue[2].trim();
} else {
contentType.rootType = dir;
}
});
assert.equal(contentType.rootType, 'multipart/related');
assert.equal(body.parts.length, 1);

const dataHeaders = body.parts[0];
assert(dataHeaders['Content-Type'].indexOf('application/xop+xml') > -1);
assert.equal(dataHeaders['Content-ID'], contentType.start);
done();
}, { forceMTOM: true })
}, baseUrl)
})
})

describe('Headers in request and last response', function () {
Expand Down

0 comments on commit aee79a8

Please # to comment.