mardi 21 juillet 2020

Cannot add header value to HTTP request via hooks

Using Testcafe, I am trying to add an HTTP Header key/value pair to all my requests, but it is not appearing in the request header when I inspect the network request in-browser in Live mode.

I'm wondering if it's potentially an issue with TypeScript maybe? I had to modify the example code in documentation to allow method arguments to be of type any in order to get it to compile, which was surprising, because I haven't seen that requirement elsewhere, TC just 'handles' it.

Two questions, then:

  • I am expecting to see the header wafAccessToken pair to appear in the request to http://www.google.com, but it isn't - any idea what could be the problem?
  • Is there a way I can have my RequestHook not specify the exact URL in the constructor? I just want it to match and modify every request regardless of where I'm going.

Here is the code:

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 => {
  
});

Aucun commentaire:

Enregistrer un commentaire