title: Node Cannot pipe pdf response
body: <p>Trying to write a test (mocha) to check that the PDF returned from my api endpoint holds the correct data and looks like it should. The PDF is generated on the sever. It returns 'corrrectly' when hit the endpoint manually, but would like to write some tests. I uploaded an example 'correct' PDF to my test suite which I am able to parse with hummus js and pull out the necessary aspects for comparison.</p>
  
  <p>I would like to make a request to my endpoint (with superagent) and then pipe the response (a pdf) into a temp pdf. Then I'll parse both PDF's (the uploaded perfect one, and the one returned from my endpoint) and make sure they match.</p>
  
  <p>My code for the request:</p>
  
  <pre><code>    it('Should look like the proposed pdf', function (done) {
  
          request(app)
             .get(url) //var that sets the path earlier
             .expect(200)
             .end(function (err, res) {
                 if(err) return done(err); 
  
                 var writeStream = fs.createWriteStream('./test/materials/tmp.pdf');
                 writeStream.pipe(res); //ERROR can't pipe
                 writeStream.on('end', function() {
                     console.log('complete');
                     done();
                  })
            });
  
        });
  </code></pre>
  
  <p>When I run my tests I get: Uncaught Error: Cannot Pipe. Not readable. I am very new to node so I am not sure what is causing the error. When I console out res, I get a large binary encoded jumble so maybe that is the issue? I've tried a couple things - using the hummus pdfWriter, trying to decode with </p>
  
  <blockquote>
    <p>new Buffer(res, 'base64')</p>
  </blockquote>
  
  <p>etc... but still no luck. I believe I have all the necessary packages installed for these operations and it seems to be a piping/decoding/superagent issue. Thanks for the help!</p>
  
  <p>EDIT: I was misunderstanding piping. You can simply write the response to a file:</p>
  
  <pre><code>fs.writeFile('./test/materials/test_tmp.pdf', new Buffer(res.text, 'ascii'));
  </code></pre>
  
  <p>Converting in this case to ascii. I am closer now, but still hung up on the encoding piece. This creates a blank PDF. When I observe the file contents in sublime they appear to be the same as the PDF I am comparing against, but have a different encoding. Does anybody know how to match the encoding of the original PDF or figure out how it is encoded? Or if this is even possible? I used PDFkit to build the PDF. </p>