2

My server code has a simple route in which I need to send a tiny thumbnail (jpeg) to the client for display. Here's a sample of the code:

router.get('/thumbnail/:title/',function(req,resp){
    let title = req.params.title;

    getFileName(title).then(thumbnailCb,errorCb);

    function thumbnailCb(jpeg){
        fs.readFile(jpegFile,sendJpegFile);
    }

    function sendJpegFile(err,data){
        if ( err ) {
            fs.readFile(DEFAULT_THUMBNAIL,sendDefault);
        } else {
            resp.send(data);
        }
    };

    function sendDefault(err,data){
        if ( err ) {console.log(err);} 
        else { resp.send(data); }
    };

    function errorCb(error){
        console.log(error);
    };
});

I am trying to test this in Mocha. My first thought was to compare Buffers, and apparently, I was not the only one to think this: https://github.com/mochajs/mocha/issues/1624

So, that is not working. The following is my Mocha/Chai code. Mocha hangs on the assert call. Does anyone have any suggestions on testing this route?

it('should get a thumbnail for from a title',function(done){
    const DEFAULT_THUMBNAIL = /* some default file */;
    const titleNotFound = /* some random title */;

    request(app)
    .get(URL + '/thumbnail/' + titleNotFound)
    .expect(200)
    .expect('Content-Type','application/octet-stream')
    .end(function(err,res){
        fs.readFile(DEFAULT_THUMBNAIL,'utf-8',function(err,data){
            if(err){done(err);}
            assert(data.toString(),res.text.toString()); // No bueno
            done();
        });
    });
});
So_oP
  • 1,211
  • 15
  • 31
westandy
  • 1,360
  • 2
  • 16
  • 41

0 Answers0