node.js - 使用 fetch-mock 模拟 zip 响应

标签 node.js mocking fetch

我试过以下代码:

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 获得

最佳答案

Response 的实例作为第二个参数传递给 fetchMock.get(),其中响应对象的主体是本地文件流:

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.
var resp = new Response(
    fsp.createReadStream( path.join( __dirname, 'left-pad-master.zip' ) ),
    { headers: { "Content-Type" : "application/zip" } }
);
fetchMock.get(zipLink, resp);

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

关于node.js - 使用 fetch-mock 模拟 zip 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784848/

相关文章:

node.js - 如何获得 aws 转码器作业状态的通知

node.js - Sentry 不会向所需邮件抛出错误

python - 模拟 side_effect 迭代器用完后可以重置吗?

javascript - 获取和等待后 React 中的 "Cannot read property of undefined"

javascript - React js 与 redux fetch 总是返回一个空的 json

node.js - 如何在 2015 年部署生产 meteor 服务器?

asp.net-mvc - 你能模拟/ stub 内部属性吗?

perl - 使用 Test::MockObject->fake_module 时出现错误消息

php - 如何使用$_GET?

javascript - 设置一个 typescript 库以在 Nodejs、浏览器和其他 Typescript 项目中使用