【问题标题】:Custom Request Headers within TestCafe are not displayed不显示 TestCafe 中的自定义请求标头
【发布时间】:2022-06-15 16:31:53
【问题描述】:

我尝试了几种不同的方法,并在不同的建议之间反复讨论可能是什么问题,但没有任何运气。最终,我需要测试应用程序的某些方面,该应用程序位于可以通过请求标头访问的功能标志后面。

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

代码:


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

日志:

{
  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'
}

【问题讨论】:

    标签: javascript testcafe request-headers


    【解决方案1】:

    TestCafe 的 RequestLogger 仅记录原始请求标头,而不记录修改后的标头。不过,这行代码绝对有效。

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

    TestCafe 正确修改了请求标头。 如果您想记录修改后的标头,您应该扩展您的 RequestHook 实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 2023-03-21
      • 2016-06-24
      • 2020-05-12
      • 2011-03-04
      • 2014-08-08
      相关资源
      最近更新 更多