mardi 31 mars 2015

Node Cannot pipe pdf response

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.


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.


My code for the request:



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();
})
});

});


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



new Buffer(res, 'base64')



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!


Aucun commentaire:

Enregistrer un commentaire