Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate request package to needle #588

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"httpntlm-maa": "^2.0.6",
"lodash": "^4.17.21",
"node-rsa": "^1.1.1",
"request": "^2.88.2",
"needle": "^3.2.0",
"sax": "^1.2.4",
"selectn": "^1.1.2",
"strong-globalize": "^6.0.5",
Expand Down
2 changes: 1 addition & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class Client extends Base {

// Added mostly for testability, but possibly useful for debugging
if (req != null) {
self.lastRequestHeaders = req.headers;
self.lastRequestHeaders = req && req.request ? req.request.getHeaders() : undefined
}
debug('client response. lastRequestHeaders: %j', self.lastRequestHeaders);
}
Expand Down
49 changes: 40 additions & 9 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

var url = require('url');
var requestModule = require('request');
var requestModule = require('needle');
var debug = require('debug')('strong-soap:http');
var debugSensitive = require('debug')('strong-soap:http:sensitive');
var httpntlm = require('httpntlm-maa');
Expand All @@ -15,6 +15,11 @@ var uuid = require('uuid').v4;

var VERSION = require('../package.json').version;

requestModule.defaults({
parse_response: false,
follow_max: 5,
})

/**
* A class representing the http client
* @param {Object} [options] Options object. It allows the customization of
Expand Down Expand Up @@ -70,7 +75,7 @@ class HttpClient {
}

var options = {
uri: curl,
uri: curl.href,
method: method,
headers: headers,
followAllRedirects: true
Expand Down Expand Up @@ -136,6 +141,38 @@ class HttpClient {
return body;
}

requestCallback(req, callback) {
const self = this
return function (err, res, body) {
if (err) {
return callback(err)
}
body = self.handleResponse(req, res, body)
callback(null, res, body)
}
}

makeHttpRequest(options, callback) {
let req
if (options.method === 'POST') {
const isMultipart = !!options.multipart
req = this._request.post(
options.uri,
isMultipart ? options.multipart : options.body,
{ ...options, multipart: isMultipart },
this.requestCallback(req, callback),
)
} else if (options.method === 'GET') {
req = this._request.get(
options.uri,
options,
this.requestCallback(req, callback),
)
}

return req
}

//check if NTLM authentication needed
isNtlmAuthRequired(ntlmSecurity, methodName) {
//if ntlmSecurity is not set, then remote web service is not NTLM authenticated Web Service
Expand All @@ -161,13 +198,7 @@ class HttpClient {
var ntlmSecurity = this.options.NTLMSecurity;
var ntlmAuth = self.isNtlmAuthRequired(ntlmSecurity, options.method);
if (!ntlmAuth) {
req = self._request(options, function (err, res, body) {
if (err) {
return callback(err);
}
body = self.handleResponse(req, res, body);
callback(null, res, body);
});
req = self.makeHttpRequest(options, callback)
} else {
//httpntlm code needs 'url' in options{}. It should be plain string, not parsed uri
options.url = rurl;
Expand Down
20 changes: 18 additions & 2 deletions src/parser/wsdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var Element = require('./element');

class WSDL {
constructor(definition, uri, options) {
this.content = definition;
this.content = definition ? definition.toString() : undefined
assert(this.content != null && (typeof this.content === 'string' ||
typeof this.content === 'object'),
'WSDL constructor takes either an XML string or definitions object');
Expand Down Expand Up @@ -479,7 +479,7 @@ class WSDL {
if (err) {
callback(err);
} else if (response && response.statusCode === 200) {
const wsdlUri = _.get(response, 'request.uri.href', uri);
const wsdlUri = getWsdlUri(response) || uri
wsdl = new WSDL(definition, wsdlUri, options);
WSDL_CACHE[wsdlUri] = wsdl;
wsdl.WSDL_CACHE = WSDL_CACHE;
Expand Down Expand Up @@ -580,6 +580,22 @@ WSDL.prototype.xmlKey = '$xml';

module.exports = WSDL;


function getWsdlUri(response) {
const { protocol, path, getHeaders } = response.req || {}

if (!protocol || !path || typeof getHeaders !== 'function') {
return null
}

const reqHost = response.req.getHeaders().host
if (!reqHost) {
return null
}

return `${protocol}//${reqHost}${path}`
}

function deepMerge(destination, source) {
return _.mergeWith(destination || {}, source, function(a, b) {
return _.isArray(a) ? a.concat(b) : undefined;
Expand Down
25 changes: 2 additions & 23 deletions test/client-customHttp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,7 @@ describe('custom http client', function() {
var options = self.buildRequest(rurl, data, exheaders, exoptions);
//Specify agent to use
options.agent = this.agent;
var headers = options.headers;
var req = this.httpCl._request(options, function(err, res, body) {
if (err) {
return callback(err);
}
body = self.handleResponse(req, res, body);
callback(null, res, body);
});
if (headers.Connection !== 'keep-alive') {
req.end(data);
}
this.httpCl.makeHttpRequest(options, callback)

util.inherits(CustomAgent, events.EventEmitter);

Expand Down Expand Up @@ -114,18 +104,7 @@ describe('custom http client', function() {
var options = self.buildRequest(rurl, data, exheaders, exoptions);
//Specify agent to use
options.agent = this.agent;
var headers = options.headers;
var req = this.httpCl._request(options, function(err, res, body) {
if (err) {
return callback(err);
}
body = self.handleResponse(req, res, body);
callback(null, res, body);
});
if (headers.Connection !== 'keep-alive') {
req.end(data);
}
return req;
return this.httpCl.makeHttpRequest(options, callback);
};
};

Expand Down
13 changes: 1 addition & 12 deletions test/client-customHttp-xsdinclude-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,7 @@ describe('custom http client', function() {
var options = self.buildRequest(rurl, data, exheaders, exoptions);
//Specify agent to use
options.agent = this.agent;
var headers = options.headers;
var req = this.httpCl._request(options, function(err, res, body) {
if (err) {
return callback(err);
}
body = self.handleResponse(req, res, body);
callback(null, res, body);
});
if (headers.Connection !== 'keep-alive') {
req.end(data);
}
return req;
return this.httpCl.makeHttpRequest(options, callback);
};

var httpCustomClient = new MyHttpClient({},
Expand Down
39 changes: 11 additions & 28 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('SOAP Client', function() {
assert.ok(!err);

client.MyOperation({}, function(err, result) {
assert.notEqual(client.lastRequestHeaders.Host.indexOf(':' + server.address().port ), -1);
assert.notEqual(client.lastRequestHeaders.host.indexOf(':' + server.address().port ), -1);

done();
}, null, {'test-header': 'test'});
Expand All @@ -207,7 +207,7 @@ describe('SOAP Client', function() {
assert.ok(!err);

client.MyOperation({}, function(err, result) {
assert.equal(client.lastRequestHeaders.Host.indexOf(':80'), -1);
assert.equal(client.lastRequestHeaders.host.indexOf(':80'), -1);

done();
}, null, {'test-header': 'test'});
Expand All @@ -220,7 +220,7 @@ describe('SOAP Client', function() {
assert.ok(!err);

client.MyOperation({}, function() {
assert.equal(client.lastRequestHeaders.Host.indexOf(':443'), -1);
assert.equal(client.lastRequestHeaders.host.indexOf(':443'), -1);
done();
}, null, {'test-header': 'test'});
}, 'https://127.0.0.1');
Expand All @@ -232,7 +232,7 @@ describe('SOAP Client', function() {
assert.ok(!err);

client.MyOperation({}, function() {
assert.ok(client.lastRequestHeaders.Host.indexOf(':443') > -1);
assert.ok(client.lastRequestHeaders.host.indexOf(':443') > -1);
done();
}, null, {'test-header': 'test'});
}, 'https://127.0.0.1:443');
Expand Down Expand Up @@ -283,22 +283,6 @@ describe('SOAP Client', function() {
}, 'http://' + hostname + ':' + server.address().port );
});

it('should have lastElapsedTime after a call with the time option passed', function(done) {
soap.createClient(__dirname+'/wsdl/default_namespace.wsdl', function(err, client) {
assert.ok(client);
assert.ok(!err);

client.MyOperation({}, function(err, result) {
assert.ok(result);
assert.ok(client.lastResponse);
assert.ok(client.lastResponseHeaders);
assert.ok(client.lastElapsedTime);

done();
}, {time: true}, {'test-header': 'test'});
}, 'http://' + hostname + ':' + server.address().port );
});

it('should add http headers in method call options', function(done) {
soap.createClient(__dirname+'/wsdl/default_namespace.wsdl', function(err, client) {
assert.ok(client);
Expand Down Expand Up @@ -444,7 +428,7 @@ describe('SOAP Client', function() {
assert.ok(result);
assert.ok(client.lastRequestHeaders);
assert.ok(client.lastRequest);
assert.equal(client.lastRequestHeaders['Content-Type'], 'application/soap+xml; charset=utf-8');
assert.equal(client.lastRequestHeaders['content-type'], 'application/soap+xml; charset=utf-8');
assert.notEqual(client.lastRequest.indexOf('xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"'), -1);
assert( !client.lastRequestHeaders.SOAPAction );
done();
Expand Down Expand Up @@ -527,7 +511,7 @@ describe('SOAP Client', function() {
assert.ok(!err);
assert.ok(result);
assert.ok(result === 'temp response');
assert.ok(envelope === rawBody);
assert.ok(envelope.toString() === rawBody);
assert.ok(client.lastResponseHeaders.status === 'fail');
done();
}, done).catch(done);
Expand All @@ -542,7 +526,7 @@ describe('SOAP Client', function() {
assert.ok(!err);
assert.ok(result);
assert.ok(result === 'temp response');
assert.ok(envelope === rawBody);
assert.ok(envelope.toString() === rawBody);
assert.ok(client.lastResponseHeaders.status === 'fail');
done();
}, done).catch(done);
Expand All @@ -557,7 +541,7 @@ describe('SOAP Client', function() {
assert.ok(!err);
assert.ok(result);
assert.ok(result === 'temp response');
assert.ok(envelope === rawBody);
assert.ok(envelope.toString() === rawBody);
assert.ok(client.lastResponseHeaders.status === 'fail');
assert.ok(client.lastRequestHeaders['options-test-header'] === 'test');
done();
Expand All @@ -573,7 +557,7 @@ describe('SOAP Client', function() {
assert.ok(!err);
assert.ok(result);
assert.ok(result === 'temp response');
assert.ok(envelope === rawBody);
assert.ok(envelope.toString() === rawBody);
assert.ok(client.lastResponseHeaders.status === 'pass');
assert.ok(client.lastRequestHeaders['options-test-header'] === 'test');
assert.ok(client.lastRequestHeaders['test-header'] === 'test');
Expand Down Expand Up @@ -847,10 +831,9 @@ describe('SOAP Client', function() {
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', function (err, client) {
var didEmitEvent = false;
client.on('response', function (xml, response) {
const xmlString = xml.toString()
didEmitEvent = true;
// Should contain entire soap message
assert.equal(typeof xml, 'string');
assert.equal(xml.indexOf('soap:Envelope'), -1);
assert.equal(xmlString.indexOf('soap:Envelope'), -1)
assert.ok(response);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
--MIMEBoundary_2af39b87d6c9f2ba6d1c13d73db88d0bfdf9414786fcc315
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.Dummy.com" xmlns:n="http://www.Dummy.com/Name/Types">
<soap:Header></soap:Header>
<soap:Body>
Expand All @@ -14,4 +9,3 @@ Content-ID: <[email protected]>
</dummy:DummyResponse>
</soap:Body>
</soap:Envelope>
--MIMEBoundary_2af39b87d6c9f2ba6d1c13d73db88d0bfdf9414786fcc315--
Loading