Testcafe: Client Certificate Authentication Documentation

Created on 5 Jun 2018  路  4Comments  路  Source: DevExpress/testcafe

Are you requesting a feature or reporting a bug?

Requesting documentation

Previously I requested a feature to be added ( #2104 ) and support for it was added in 0.20... but I can't see a way on how to add support in my tests and the documentation mentions it but doesn't provide any way to actually support it... could you please demonstrate how to use this in my tests? Even just some sample code. We are unable to write any tests until we can figure out how to use request hooks to support this

Thanks

level 2 enhancement

Most helpful comment

For anyone finding this issue, it's actually not that hard:

Create a RequestHook like:

const target = /https:\/\/my\.secure\.host/
export class MyRequestHook extends RequestHook {
  constructor() {
    super(target)
  }

  onRequest(event) {
    // add keys to request
    Object.assign(event.requestOptions, {
      ...keys
    })
  }

  onResponse() {}
}

where keys is

const keys = {
  key: fs.readFileSync(USER_KEY),
  cert: fs.readFileSync(USER_CRT),
  ca: fs.readFileSync(CA_CRT)
}

the constants USER_KEY, USER_CRT, CA_CRT are paths to the PEM formatted keys/certs.

All 4 comments

Hi @snowl

The current development sprint is already planned. We will consider this issue during planning the next development sprint.

For anyone finding this issue, it's actually not that hard:

Create a RequestHook like:

const target = /https:\/\/my\.secure\.host/
export class MyRequestHook extends RequestHook {
  constructor() {
    super(target)
  }

  onRequest(event) {
    // add keys to request
    Object.assign(event.requestOptions, {
      ...keys
    })
  }

  onResponse() {}
}

where keys is

const keys = {
  key: fs.readFileSync(USER_KEY),
  cert: fs.readFileSync(USER_CRT),
  ca: fs.readFileSync(CA_CRT)
}

the constants USER_KEY, USER_CRT, CA_CRT are paths to the PEM formatted keys/certs.

@konqi hope all is well.
I have a couple of questions:
1- Is the the CA_CRT required? I'm testing an application that only requires a key and a cert.
2- How are you passing the requestHook?
3- Are you navigating to the page prior to the requestHook?

@omartaghlabi
1) It was required for me, since the web app used a custom CA. It doesn't hurt to provide the CA and it should be easy to do so since the CA cert is usually public. Can it work without? I don't know, have you tried it?
2) see https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/creating-a-custom-http-request-hook.html
3) should be answered by (2)

Was this page helpful?
0 / 5 - 0 ratings