From 0797eafc2793f3fb0159e8d4e4341593f4eeb3fb Mon Sep 17 00:00:00 2001 From: Prasun Sultania Date: Thu, 11 Aug 2016 12:06:59 +0530 Subject: [PATCH] better error 1. SOAP message missing evelope and body 2. request/response tests --- lib/server.js | 6 +++ test/request-no-envelope-body-test.js | 65 +++++++++++++++++++++++++++ test/request-response-samples-test.js | 6 +++ 3 files changed, 77 insertions(+) create mode 100644 test/request-no-envelope-body-test.js diff --git a/lib/server.js b/lib/server.js index a76acd105..3edf62fb0 100644 --- a/lib/server.js +++ b/lib/server.js @@ -211,6 +211,12 @@ Server.prototype._process = function(input, req, callback) { self.log("info", "Attempting to bind to " + pathname); } + //Avoid Cannot convert undefined or null to object due to Object.keys(body) + //and throw more meaningful error + if(!body){ + throw new Error('Failed to parse the SOAP Message body'); + } + // use port.location and current url to find the right binding binding = (function(self) { var services = self.wsdl.definitions.services; diff --git a/test/request-no-envelope-body-test.js b/test/request-no-envelope-body-test.js new file mode 100644 index 000000000..ccdf10a4c --- /dev/null +++ b/test/request-no-envelope-body-test.js @@ -0,0 +1,65 @@ +'use strict'; + +var request = require('request'); +var assert = require('assert'); +var http = require('http'); +var soap = require('../'); +var server; +var port; + + +describe('No envelope and body elements', function() { + var wsdl = 'WSDL File for HelloService'; + before(function(done){ + server = http.createServer(function(req, res) { + res.statusCode = 404; + res.end(); + }).listen(51515, function() { + var soapServer = soap.listen(server, '/SayHello', { + Hello_Service: { + Hello_Port: { + sayHello: function(args){ + return { + greeting: args.firstName + } + } + } + } + }, wsdl); + done(); + }); + }); + + after(function(){ + server.close(); + }); + + + it('should throw an error when Body and Envelope are missing', + function(done){ + var requestXML = 'tarun'; + 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; + } + + request({ + url: url + '/SayHello', + method: 'POST', + headers: {SOAPAction: "sayHello", + "Content-Type": 'text/xml; charset="utf-8"'}, + body: requestXML + }, function(err, response, body){ + if(err){ + throw err; + } + assert.equal(body.indexOf('Failed to parse the SOAP Message body') !== -1, true); + done(); + }); + + }); + +}); diff --git a/test/request-response-samples-test.js b/test/request-response-samples-test.js index 584bddc65..301d47546 100644 --- a/test/request-response-samples-test.js +++ b/test/request-response-samples-test.js @@ -133,6 +133,12 @@ function generateTest(name, methodName, wsdlPath, headerJSON, securityJSON, requ if (securityJSON && securityJSON.type === 'ws') { client.setSecurity(new WSSecurity(securityJSON.username, securityJSON.password, securityJSON.options)); } + + //throw more meaningful error + if(typeof client[methodName] !== 'function'){ + throw new Error(method + ' ' + methodName + ' does not exists in wsdl specified in test wsdl: ' + wsdlPath); + } + client[methodName](requestJSON, function(err, json, body, soapHeader){ if(requestJSON){ if (err) {