用nock嘲笑肥皂服务

我正在研究与肥皂服务进行通信的node应用程序,使用foam模块将jsonparsing为有效的肥皂请求,并在接收到响应时再返回。 这一切工作正常时,与肥皂服务进行沟通。

我遇到的问题是为此编写unit testing(集成testing工作正常)。 我使用nock来模拟http服务并发送回复。 这个答复确实被foam分析,然后我可以对这个答复做出断言。

所以我不能传递一个JSON对象作为回复,因为foam需要一个肥皂响应。 如果我尝试这样做,我得到的错误:

 Error: Start tag expected, '<' not found 

在JavaScriptvariables中存储XML是痛苦的,并且不起作用(即,将其包含在引号中,并且转义内部引号无效),所以我想将嘲讽的XML响应放入文件中,并将其作为回复传递。

我已经尝试读取stream中的文件

 return fs.createReadStream('response.xml') 

…并用文件回复

 .replyWithFile(201, __dirname + 'response.xml'); 

两者都失败,错误

 TypeError: Cannot read property 'ObjectReference' of undefined 

这是文件中的XML

 <env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'> <env:Header></env:Header> <env:Body> <FLNewIndividualID xmlns='http://www.lagan.com/wsdl/FLTypes'> <ObjectType>1</ObjectType> <ObjectReference>12345678</ObjectReference> <ObjectReference xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/> <ObjectReference xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/> </FLNewIndividualID> </env:Body> </env:Envelope> 

正在testing的模块是

 var foam = require('./foam-promise.js'); module.exports = { createUserRequest: function(url, operation, action, message, namespace) { var actionOp = action + '/actionRequestOp', uri = url + '/actionRequest'; return new Promise(function(resolve, reject) { foam.soapRequest(uri, operation, actionOp, message, namespace) .then(function(response) { resolve(response.FLNewIndividualID.ObjectReference[0]); }) .catch(function(err) { reject(err); }); }); } }; 

断言是使用should-promised

 return myRequest(url, operation, action, data, namespace) .should.finally.be.exactly('12345678'); 

所以它看起来像XMLparsing器不会只接受一个文件(这是有道理的)。 在testing之前stream不完整吗?

XML回复可以用nock成功模拟吗?

我也在Github上提出这个问题

遵循pgte的build议https://github.com/pgte/nock/issues/326我可以通过设置正确的头文件来获得这个工作,用一个xmlstring(带有转义引号)回答&#x3002;

从pgte:

它可以。 我不太了解泡沫,但我想你必须设置响应内容types头(请参阅https://github.com/pgte/nock#specifying-reply-headers ),以便客户端可以正确parsingXML。

以下是工作testing的外观:

 it('should return a user ID', function(){ var response = '<env:Envelope xmlns:env=\'http://schemas.xmlsoap.org/soap/envelope/\'><env:Header></env:Header><env:Body><UserReference>12345678</UserReference></env:Body></env:Envelope>' nock(url) .post('/createUserRequest') .reply(201, response, { 'Content-Type': 'application/xml' } ); return createUserRequest(url, operation, action, message, options) .should.be.fulfilledWith('12345678'); }); it('should be rejected if the request fails', function() { nock(url) .post('/createCaseRequest') .replyWithError('The request failed'); return createUserRequest(url, operation, action, message, options) .should.be.rejected; });