未显示testcafe中的自定义请求标头
我尝试了几件不同的事情,并在没有任何运气的情况下在不同的建议之间反弹。最终,我需要测试一个应用程序标志后面的应用程序的某些方面,该应用程序可以使用请求标头访问。
我可能正在尝试完成此实现无法实现的目标,但是我在下面附上了代码示例,以及下面生成的日志。我希望添加的标头在日志输出中存在。
代码:
class myRequestHook extends RequestHook {
constructor () {
super()
}
async onRequest (e) {
e.requestOptions.headers['test-header'] = 'test-value'
}
async onResponse (e) {
}
}
const customHook = new myRequestHook()
const customLogger = RequestLogger('http://example.com', { logRequestHeaders: true })
fixture ('Request fixture')
.page('http://example.com/')
.requestHooks( [ customLogger, customHook ])
test('Request test', async () => {
console.log(customLogger.requests[0].request.headers)
})
logs:
{
host: 'example.com',
connection: 'keep-alive',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.115 Safari/537.36',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-dest': 'document',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9'
}
I have tried a handful of different things and bounced between different suggestions on what could be the problem without any luck. Ultimately I need to test certain aspects of an application that is behind a feature flag which can be accessed with request headers.
I may be trying to accomplish something that isn't possible with this implementation, but I have attached my code sample below, and the logs generated below that. I expected the added header to be present in the log output.
Code:
class myRequestHook extends RequestHook {
constructor () {
super()
}
async onRequest (e) {
e.requestOptions.headers['test-header'] = 'test-value'
}
async onResponse (e) {
}
}
const customHook = new myRequestHook()
const customLogger = RequestLogger('http://example.com', { logRequestHeaders: true })
fixture ('Request fixture')
.page('http://example.com/')
.requestHooks( [ customLogger, customHook ])
test('Request test', async () => {
console.log(customLogger.requests[0].request.headers)
})
Logs:
{
host: 'example.com',
connection: 'keep-alive',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.115 Safari/537.36',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-dest': 'document',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9'
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TestCafe的RequestLogger仅记录原始请求标头,但不登录修改后的标题。但是,这条代码肯定有效。
TestCafe正确修改请求标头。
如果要记录修改后的标头,则应扩展
requestHook
实现。TestCafe's RequestLogger logs only original request headers, but not the modified ones. However, this line of code definitely works.
TestCafe correctly modifies request headers.
If you want to log modified headers, you should extend your
RequestHook
implementation.