Skip to content

Commit

Permalink
Merge 7c0e2b6 into 00a5e94
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmayanei1 committed Jul 19, 2019
2 parents 00a5e94 + 7c0e2b6 commit 6992fbd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var req = require('request');
var debug = require('debug')('strong-soap:http');
var debugSensitive = require('debug')('strong-soap:http:sensitive');
var httpntlm = require('httpntlm');
var uuid = require('uuid/v4')


var VERSION = require('../package.json').version;
Expand Down Expand Up @@ -52,9 +53,13 @@ class HttpClient {
};
var attr;
var header;
var attachments = []
var mergeOptions = ['headers'];
if (exoptions) {
attachments = exoptions.attachments || [];
}

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

options.body = data;
if (attachments.length > 0) {
const start = uuid();
headers['Content-Type'] =
'multipart/related; type="application/xop+xml"; start="<' + start + '>"; start-info="text/xml"; boundary=' + uuid();
const multipart = [{
'Content-Type': 'application/xop+xml; charset=UTF-8; type="text/xml"',
'Content-ID': '<' + start + '>',
'body': data,
}];

attachments.forEach((attachment) => {
multipart.push({
'Content-Type': attachment.mimetype,
'Content-Transfer-Encoding': 'binary',
'Content-ID': '<' + attachment.contentId + '>',
'Content-Disposition': 'attachment; filename="' + attachment.name + '"',
'body': attachment.body,
});
});
options.multipart = multipart;
} else {
options.body = data;
}


exoptions = exoptions || {};
for (attr in exoptions) {
Expand Down
34 changes: 34 additions & 0 deletions test/client-attachment-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var fs = require('fs'),
http = require('..').http,
//http = require('http'),
//WSDL = soap.WSDL,
assert = require('assert'),
QName = require('..').QName;

describe('SOAP Http attachment', function() {
it('attachment null', function(done){
var httpClient = new http({})
const option = httpClient.buildRequest('http://localhost:1', {}, {}, {});
assert.ok(!option.multipart)
assert.ok(option.body)
done()
})

it('attachment length 1', function(done){
var httpClient = new http({})
const optionEx = {
attachments:[{
name: 'teste',
contentId: '123445555',
mimetype: 'jpeg',
body: 'stream file'
}]
}
const option = httpClient.buildRequest('http://localhost:1', {}, {}, optionEx);
assert.ok(option.multipart.length > 0)
assert.ok(!option.body)
done()
})
})

0 comments on commit 6992fbd

Please sign in to comment.