当前位置:  开发笔记 > 编程语言 > 正文

使用fetch-mock模拟一个zip响应

如何解决《使用fetch-mock模拟一个zip响应》经验,为你挑选了0个好方法。

我试过以下代码:

require( 'isomorphic-fetch' );

const fetchMock = require( 'fetch-mock' ),
    fsp = require( 'fs-promise' ),
    unzip = require( 'unzip' ),
    rimraf = require( 'rimraf-then' ),
    path = require( 'path' );

let zipLink = 'https://github.com/stevemao/left-pad/archive/master.zip',
    out = 'left-pad-master';

// Careful: lib might be removed at any moment.
fetchMock.get( zipLink,
    fsp.createReadStream( path.join( __dirname, 'left-pad-master.zip' ) ) );

rimraf( out )
    .then( () => fetch( zipLink ) )
    .then( response => {
        return new Promise( ( resolve, reject ) => {
            // For example purpose, just parse zip file, and log each entry.
            response.body.pipe( unzip.Parse() )
                .on( 'entry', ( entry ) => console.log( entry.path ) )
                .on( 'close', resolve )
                .on( 'error', reject );
        } );
    } )
    .then( () => console.log( 'done' ) )
    .catch( console.log );

但它抛出:

Error: invalid signature: 0x725f227b
    at C:\dev\unzip-mock\node_modules\unzip\lib\parse.js:59:13
    at runCallback (timers.js:628:20)
    at tryOnImmediate (timers.js:601:5)
    at processImmediate [as _immediateCallback] (timers.js:578:5)

如果你注释掉fetchMock.get电话,并使用真实的电话,fetch它运作良好.

代码可在https://github.com/mlewand/unzip-mock-example获得

推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有