未显示testcafe中的自定义请求标头

发布于 2025-02-07 05:17:29 字数 1490 浏览 1 评论 0原文

我尝试了几件不同的事情,并在没有任何运气的情况下在不同的建议之间反弹。最终,我需要测试一个应用程序标志后面的应用程序的某些方面,该应用程序可以使用请求标头访问。

我可能正在尝试完成此实现无法实现的目标,但是我在下面附上了代码示例,以及下面生成的日志。我希望添加的标头在日志输出中存在。

代码:


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-02-14 05:17:30

TestCafe的RequestLogger仅记录原始请求标头,但不登录修改后的标题。但是,这条代码肯定有效。

e.requestOptions.headers['test-header'] = 'test-value'

TestCafe正确修改请求标头。
如果要记录修改后的标头,则应扩展requestHook实现。

TestCafe's RequestLogger logs only original request headers, but not the modified ones. However, this line of code definitely works.

e.requestOptions.headers['test-header'] = 'test-value'

TestCafe correctly modifies request headers.
If you want to log modified headers, you should extend your RequestHook implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文