node-file-handler.ts 3.03 KB
import { NodeFileHandler } from '../../../src/file-handler/node-file-handler';
import * as http from 'http';
import * as https from 'https';
import * as fs from 'fs';

describe("Node Downloader", () => {

    let nodeFileHandler = new NodeFileHandler();

    beforeEach(() => {
        nodeFileHandler.removeAllListeners();
    });

    it("should retrieve right handler", () => {

        let httpHandler = nodeFileHandler.selectProtocol("http://someurl.com/image.jpg");
        expect(httpHandler).toEqual(http);

        let httpsHandler = nodeFileHandler.selectProtocol("https://someurl.com/image.jpg");
        expect(httpsHandler).toEqual(https);

        let nullHandler = nodeFileHandler.selectProtocol("notsupported://blub");
        expect(nullHandler).toBeNull();

    });

    it("should download sample image from https source and store with new name", (done) => {

        let source = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/800px-FullMoon2010.jpg";
        let target = "./.tmp/full-moon-" + (new Date()).getTime() + ".jpg";
        let lastProgress = 0;

        nodeFileHandler
            .on('progress', (progress: number) => {
                expect(progress).toBeGreaterThan(lastProgress);
                lastProgress = progress;
            })
            .once('error', (error) => {})
            .once('complete', () => { 
                expect(lastProgress).toEqual(1);
                expect(fs.existsSync(target)).toBeTruthy();
                done();
            })
            .download(source, target);
    });

    it('should not download if file with same name exists and bytesize > 0', (done) => {
        let source = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/800px-FullMoon2010.jpg";
        let target = "./.tmp/full-moon-sensless.jpg";
        let lastProgress = 0;

        let file = fs.createWriteStream(target, {'flags': 'a'});
        let dummyData = "some sensless information to fill bytes...";
       
        file.write(new Buffer(dummyData));
        file.end(() => {
            nodeFileHandler
                .on('progress', () => { fail("progress should not be called"); })
                .once('error', () => {})
                .once('complete', () => {
                    expect(fs.existsSync(target)).toBeTruthy();
                    done();
                })
                .download(source, target);
        });
    });

    it('should correctly cleanup files', () => {
        let basePath = "./.tmp-files";
        fs.mkdirSync(basePath);
        fs.writeFileSync(basePath + "/file1", "some data for file 1");
        fs.writeFileSync(basePath + "/file2", "some data for file 2");
        fs.writeFileSync(basePath + "/file3", "some data for file 3");
        fs.writeFileSync(basePath + "/file4", "some data for file 4");

        nodeFileHandler.cleanup([
            { source : '' , target : 'file1' },
            { source : '' , target : 'file2' }], basePath);

        let files = fs.readdirSync(basePath);

        expect(files.length).toEqual(2);
    });

});