Proxy / Request Inspection Middleware in Testcafe-Hammerhead

Could anyone point me in the direction of where in the codebase for testcafe-hammerhead I would be able to implement a piece of middleware that inspects an incoming request and sets a forward / external proxy depending on for example the url + path the request contains?

I read that testcafe supports plugins and such, but I'm only working with the webproxy (testcafe-hammerhead) so I'm a bit lost on what the best approach would be here.

If this isn't natively supported and requires large changes, that's totally fine, I'm just looking for someone to point me in the right direction of where I can start, I'm new to the codebase and it's quite overwhelming.

I looked into the http library that testcafe-hammerhead is using, however I see multiple points in the code where connections are established, it's unclear for me if I should continue hunting down http.createServer implementations or if there's maybe a better way to achieve my goals.

1 Answer

I'm not really sure if this is sufficient for your use case and goes into the direction you intend to go, but this might be a minimalistic approach to reach your goal. You can configure TestCafè to use a proxy whenever it runs tests and also tell for what url patterns it should not use one. One way to specify proxy settings is via TestCafè's CLI. A sample package.json with a script to run your tests could look as follows:

{
  "name": "some-package",
  "version": "0.1.0",
  "scripts": {
    "test:chrome": "testcafe chrome src/tests --proxy '[email protected]' --proxy proxy.mycompany.com --proxy-bypass 'localhost,*.mycompany.com'",
  "devDependencies": {
    "testcafe": "2.6.2",
    "typescript": "^4.9.5"
  }
}

The script in the example assumes that you have your tests located at src/test. You could run via npm or yarn like, e.g.: yarn test:chrome. In this case, a proxy would be used for all calls BUT what patterns are specified in the value for the flag --proxy-bypass. Hence, for localhost as well as any request that matches the pattern *.mycompany.com, no proxy would be used.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest