testing - 无法通过钩子将标头值添加到 HTTP 请求
问题描述
使用 Testcafe,我正在尝试向我的所有请求添加一个 HTTP 标头键/值对,但是当我在实时模式下检查浏览器中的网络请求时,它没有出现在请求标头中。
我想知道这是否可能是 TypeScript 的问题?我不得不修改文档中的示例代码以允许方法参数为类型any
以使其编译,这令人惊讶,因为我在其他地方没有看到该要求,TC 只是“处理”它。
那么两个问题:
- 我希望看到标头
wafAccessToken
对出现在 to 的请求中http://www.google.com
,但它不是 - 知道可能是什么问题吗? - 有没有办法让我
RequestHook
不在构造函数中指定确切的 URL?我只希望它匹配和修改每个请求,无论我要去哪里。
这是代码:
import { RequestHook } from 'testcafe';
export class WebSwitchHook extends RequestHook
{
constructor(requestFilterRules: any, responseEventConfigureOpts: any)
{
super(requestFilterRules, responseEventConfigureOpts);
// ...
}
async onRequest (e: any)
{
e.requestOptions.headers['wafAccessToken'] = "REDACTED";
console.log(e.requestOptions);
}
async onResponse (e: any) {
console.log(e.body);
}
}
const webSwitchHook = new WebSwitchHook("http://www.google.com", {
includeHeaders: true,
includeBody: true
});
fixture.only('Test')
.page("http://www.google.com")
.requestHooks(webSwitchHook);
test('Test', async t => {
});
解决方案
您共享的代码可以正常工作。您可以看到您在服务器上添加的标头。您还可以将RequestLogger附加到夹具并在那里查看更新的标头。
至于您的第二个问题,您可以使用此示例中的代码RequestHook
对所有请求使用:
class JwtBearerAuthorization extends RequestHook {
constructor () {
super();
}
//...
}
或者,将正则表达式传递给 RequestHook 构造函数以仅将其用于匹配 URL。
推荐阅读
- android - 如何解决 minifyEnabled true 的错误?
- javascript - 如何在尺寸取决于不同移动屏幕尺寸的 iFrame 中居中对齐内容?
- r - Why does .SD in a data.table non-equi join throws an error occasionally?
- entity-framework-6 - EF DbContext Attach 仅在禁用代理创建时有效
- sql - SQL从组中按最大值选择一行
- node.js - 使用不同的场景 Async/Await 多次运行 Puppeteer 测试
- javascript - 是否可以将 javascript 或 css 清除到特定部分或 div
- sql - SQL 仅在工作日平均
- javascript - Javascript:-嵌入的“For Loop”在 HTML 中不起作用
- vba - 使用零件文件名重命名切割清单实体的任何方式