@codeceptjs/mock-request

中文文档
0.3.1发布于 2021/11/10ISCCommonJS近 30 天下载 1万

与 PollyJS 混合请求密码

中文使用文档
文档目录3 节
  1. API 密码
  2. 嘲笑请求
  3. 使用量

Mock 请求帮助代码ceptJS

Build Status

Playwright 引擎不为此帮助所支持. 使用本地语言 模拟请求的 Playwright API。 。 。 .

QQ 通过 HTTP 执行 HTTP 请求 嘲笑、 记录和重放 波利贾斯 页:1 密码JS。 。 。 .

用来:

  • QQ 记录和重放 HTTP 请求
  • 替换服务器响应
  • QQ 块第三方集成:分析,聊天,支持系统.

与 Puppeteer 和 WebDriver 帮助者合作 密码JS。 。 。 .

API 密码

目录

嘲笑请求

此助手允许 在 Puppeteer 或 WebDriver 中运行测试时模拟请求。 。 。 . 例如,你可以屏蔽给Google Analytics,CDNs等第三方服务的呼叫. 另一种使用方法是通过传递已准备好的数据来模拟服务器的请求.

Mock Request 帮助者在此工作 模式数字 :

  • 通过( 默认) - 模拟预定义的 HTTP 请求
  • 记录 - 将所有请求记录为文件
  • 重放 - 从文件中重放所有已记录的请求

合并记录/重放模式可以使用大型数据集测试网站.

在通过模式下使用一套规则来模拟请求,它们将被自动拦截并被替换:

// default mode
I.mockRequest('GET', '/api/users', '[]');

在记录-重放模式中,开始嘲笑让HTTP请求被记录/重放,并在不再需要屏蔽请求时停止:

// record or replay all XHR for /users page
I.startMocking();
I.amOnPage('/users');
I.stopMocking();

安装

npm i @codeceptjs/mock-request --save-dev

需要 Puppeteer 帮助或启用 WebDriver 帮助

配置

和普普特尔

在配置文件中启用助手 :

helpers: {
   Puppeteer: {
     // regular Puppeteer config here
   },
   MockRequestHelper: {
     require: '@codeceptjs/mock-request',
   }
}

Polly 配置选项 也可以通过:

// sample options
helpers: {
  MockRequestHelper: {
     require: '@codeceptjs/mock-request',
     mode: record,
     recordIfMissing: true,
     recordFailedRequests: false,
     expiresIn: null,
     persisterOptions: {
       keepUnusedRequests: false
       fs: {
         recordingsDir: './data/requests',
       },
    },
  }
}

障碍: Puppeteer 不以无头模式模拟请求 :

问题: 请求取笑不起作用, 在调试模式下您在输出中看到 :

Access to fetch at {url} from origin {url} has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

解决方案: 更新 Puppeteer 配置以包含 --disable-web-security 参数 :

 Puppeteer: {
   show: false,
   chrome: {
     args: [
       '--disable-web-security',
     ],
   },
 },

使用 Web 驱动程序

这个帮助器部分与WebDriver合作. 它可以拦截和模拟请求 仅在已装入的页面上。 。 。 .

helpers: {
   WebDriver: {
     // regular WebDriver config here
   },
   MockRequestHelper: {
     require: '@codeceptjs/mock-request',
   }
}

记录/重放模式不测试于 WebDriver, 但技术上可以与 REST 长者

使用量

QQ 模拟请求

截取 API 请求并按 API 进行模拟

通话 mockRequest 或者说 mockServer 如果尚未启用, 将开始嘲笑 .

I.startMocking(); // optionally
I.mockRequest('/google-analytics/*path', 200);
// return an empty successful response 
I.mockRequest('GET', '/api/users', 200);
// mock users api
I.mockServer(server => {
  server.get('https://server.com/api/users*').
    intercept((req, res) => { res.status(200).json(users);
  });
});
I.click('Get users);
I.stopMocking();

QQ 记录重放( R)

此时此刻,只和傀儡合作

记录重放模式允许您记录所有 xhr 获取请求并保存到文件 . 在接下来的运行中,这些请求可以重播. 默认情况下, 它会存储所有通过的请求, 但此行为可以被定制 I.mockServer

通过 enironment 变量设置模式, replay 默认模式 :

// enable replay mode
helpers: {
 Puppeteer: {
   // regular Puppeteer config here
 },
 MockRequest: {
    require: '@codeceptjs/mock-request',
    mode: process.env.MOCK_MODE || 'replay',
 },
}

相互作用 I.startMocking() 财务报告和审定财务报表 I.stopMocking() 将被记录并保存到 data/requests 目录.

I.startMocking() // record requests under 'Test' name
I.startMocking('users') // record requests under 'users' name

使用 I.mockServer() 定制哪些请求应予记录,以及名称:

I.startMocking();
I.mockServer((server) => {
  // mock request only from ap1.com and api2.com and
  // store recording into two different files
  server.any('https://api1.com/*').passthrough(false).recordingName('api1');
  server.any('https://api2.com/*').passthrough(false).recordingName('api2');
});

停止请求录音/重放使用 I.stopMocking()。 。 。 .

QQ 将 HTTP 交互执行测试与 MOCK MODE 环境变量设定为“ 记录 ” :

MOCK_MODE=record npx codeceptjs run --debug

重新进行发射测试

npx codeceptjs run --debug

参数

  • config

开始嘲笑

开始嘲笑 http 请求 . 在记录模式下开始记录所有请求. 在重放模式中,将所有请求块并替换为已保存.

如果在一个测试中,您计划在多个地方记录/重播请求,请提供 记录名称 作为参数.

// start mocking requests for a test
I.startMocking(); 

// start mocking requests for main page
I.startMocking('main-page');
// do actions
I.stopMocking();
I.startMocking('login-page');

更新 PollyJS 配置 使用secon参数 :

// change mode
I.startMocking('comments', { mode: 'replay' });

// override config
I.startMocking('users-loaded', {
   recordFailedRequests: true
})
参数
  • title 任意 (可选,默认) 'Test'(中文(简体) )
  • config (可选,默认) {}(中文(简体) )

模拟服务器

使用 PollyJS 服务器路由 API 通过回调函数声明模拟:

// basic usage
server.get('/api/v2/users').intercept((req, res) => {
  res.sendStatus(200).json({ users });
});

// passthrough requests to "/api/v2"
server.get('/api/v1').passthrough();

在记录重放模式中, 您可以定义要记录的路由以及存储它们的地点 :

I.startMocking('mock');
I.mockServer((server) => {

  // record requests from cdn1.com and save them to data/recording/xml
  server.any('https://cdn1.com/*').passthrough(false).recordingName('xml');
  
  // record requests from cdn2.com and save them to data/recording/svg
  server.any('https://cdn2.com/*').passthrough(false).recordingName('svg');

  // record requests from /api and save them to data/recording/mock (default)
  server.any('/api/*').passthrough(false);
});
参数
  • configFn

录音

用于嘲弄的力量记录模式. 需要开始嘲笑.

I.recordMocking();

重放模拟

用于嘲笑的重放模式. 需要开始嘲笑.

I.replayMocking();

通过移动

部队通过模式进行嘲笑. 需要开始嘲笑.

I.passthroughMocking();

冲出

等待Mock处理的所有请求得到解决:

I.flushMocking();

停止嘲笑

停止嘲笑请求. 必须调用才能将记录的请求保存为失败 .

I.stopMocking();

模拟请求

模拟反应状态

I.mockRequest('GET', '/api/users', 200);
I.mockRequest('ANY', '/secretsRoutes/*', 403);
I.mockRequest('POST', '/secrets', { secrets: 'fakeSecrets' });
I.mockRequest('GET', '/api/users/1', 404, 'User not found');

多项请求

I.mockRequest('GET', ['/secrets', '/v2/secrets'], 403);
参数
  • method 字符串 请求方法。 可能会 GET, (中文(简体) ), POST, (中文(简体) ), PUT等,或 ANY。 。 。 .
  • oneOrMoreUrls (单位:千美元)字符串 | 阵列<字符串>] (简体中文) url(s)来嘲笑。 可以是精确的 URL,一个图案,或者一系列 URL.
  • dataOrStatusCode (单位:千美元)编号 | 字符串 | 对象(中文(简体) ) 提供数字时状态代码 。 反应机构
  • additionalData (单位:千美元)字符串 | 对象(中文(简体) ) 当一个状态代码由先前参数设定时,响应正体。 (可选,默认) null(中文(简体) )

版本与发布记录

@codeceptjs/mock-request 共发布 4 个版本,首次发布于 2020/03/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.1latest2021/11/104 年前
0.3.02020/03/296 年前
0.2.02020/03/226 年前
0.1.02020/03/206 年前

关于 @codeceptjs/mock-request 的常见问题

@codeceptjs/mock-request 是什么?

与 PollyJS 混合请求密码

如何安装 @codeceptjs/mock-request?

在项目目录运行 npm install @codeceptjs/mock-request,或使用 pnpm add / yarn add / bun add。

@codeceptjs/mock-request 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/codeceptjs__mock-request 是否存在。

@codeceptjs/mock-request 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codeceptjs/mock-request 可以商用吗?

@codeceptjs/mock-request 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codeceptjs/mock-request 的下载量有多少?

根据 npm 官方下载统计,@codeceptjs/mock-request 在 2026/10/10 – 2026/11/08 这 30 天内被下载 10,498 次,日均约 350 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codeceptjs/mock-request 还在维护吗?

最新版本 0.3.1 发布于 2021/11/10(4 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。