title: Mocking soap services with nock
body: <p>I'm working on a <code>node</code> app that communicates with soap services, using the <code>foam</code> module to parse json into a valid soap request and back again when the response is received. This all works fine when communicating with the soap services.</p>
  
  <p>The issue I'm having is writing unit tests for this (integration tests work fine). I'm using <code>nock</code> to mock the http service and send a reply. This reply does get parsed by <code>foam</code> and then I can make assertions against the response. </p>
  
  <p>So I cannot pass a json object as a reply because <code>foam</code> expects a soap response. If I try to do this I get the error:</p>
  
  <pre><code>Error: Start tag expected, '&lt;' not found
  </code></pre>
  
  <p>Storing XML in javascript variables is painful and doesn't work (i.e. wrapping it in quotes and escaping inner quotes isn't valid), so I wanted to put the mocked XML response into a file and pass that as a reply. </p>
  
  <p>I've tried reading the file in as a stream</p>
  
  <pre><code>return fs.createReadStream('response.xml')
  </code></pre>
  
  <p>...and replying with a file</p>
  
  <pre><code>.replyWithFile(201, __dirname + 'response.xml');
  </code></pre>
  
  <p>Both fail with an error of </p>
  
  <pre><code>TypeError: Cannot read property 'ObjectReference' of undefined
  </code></pre>
  
  <p>Here is the XML in the file</p>
  
  <pre><code>&lt;env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'&gt;
      &lt;env:Header&gt;&lt;/env:Header&gt;
      &lt;env:Body&gt;
          &lt;FLNewIndividualID xmlns='http://www.lagan.com/wsdl/FLTypes'&gt;
              &lt;ObjectType&gt;1&lt;/ObjectType&gt;
              &lt;ObjectReference&gt;12345678&lt;/ObjectReference&gt;
              &lt;ObjectReference xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/&gt;
              &lt;ObjectReference xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/&gt;
          &lt;/FLNewIndividualID&gt;
      &lt;/env:Body&gt;
  &lt;/env:Envelope&gt;
  </code></pre>
  
  <p>The module being tested is</p>
  
  <pre><code>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);
              });
          });
  
      }
  };
  </code></pre>
  
  <p>The assertion is using <code>should-promised</code></p>
  
  <pre><code>return myRequest(url, operation, action, data, namespace)
      .should.finally.be.exactly('12345678');
  </code></pre>
  
  <p>So it looks like the xml parser won't just accept a file (which makes sense). Does the stream not complete before it is tested? </p>
  
  <p>Can an XML reply be mocked successfully with nock?</p>
  
  <p>I also raised this on <a href="https://github.com/pgte/nock/issues/326" rel="nofollow">Github</a></p>