简体   繁体   中英

Custom Request Headers within TestCafe are not displayed

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM