supertest icon indicating copy to clipboard operation
supertest copied to clipboard

Use supertest for self-sign certificate

Open fedika opened this issue 6 years ago • 13 comments

Hi guys,

We have a problem when run test, the result is 'unable to verify the first certificate', is there any built-in options on supertest to disable ssl certificate verification? just like rejectUnauthorized on request. We found many suggestions use process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 but it doesn't work on us.

Thank you

fedika avatar Feb 06 '19 08:02 fedika

In v4.x.... If you need to test with HTTPS against localhost, you can use superagent's .trustLocalhost(), which let's you bypass any errors related to broken/insecure HTTPS on localhost.

gjohnson avatar Mar 09 '19 11:03 gjohnson

this method does not exists anymore...

LandryDubus avatar Aug 06 '19 13:08 LandryDubus

What happened? The docs and code suggest these functions exist, but I get is not a function when trying to invoke connect() or trustLocalhost(). Am I insane? I mean JavaScript is clearly way over my head, but it looks like it should work.

nlfiedler avatar Feb 12 '20 00:02 nlfiedler

Same here on Node 12 and supertest 4.0.2. trustLocalHost() no longer exists and setting process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 does not work.

ktamaral avatar Apr 14 '20 23:04 ktamaral

This should be reopened @gjohnson

garrettg123 avatar Apr 25 '20 10:04 garrettg123

I was able to solve this with the solution in this github issue. I solved it by adding testEnvironment: 'node', to jest.config.js file (in addition to the process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 setting).

ktamaral avatar Apr 25 '20 16:04 ktamaral

I was able to solve this with the solution in this github issue. I solved it by adding testEnvironment: 'node', to jest.config.js file (in addition to the process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 setting).

this will not work on node 12

pskfry avatar Apr 27 '20 18:04 pskfry

This works after upgrading to Supertest v5.0.0-0 on Node v12.16.2:

      const response = await request(server)
        .get('/')
        .trustLocalhost()
        .key(sslConfig.privateKey)
        .cert(sslConfig.certificate)
        .expect(200)

garrettg123 avatar Apr 27 '20 23:04 garrettg123

This works after upgrading to Supertest v5.0.0-0 on Node v12.16.2:

      const response = await request(server)
        .get('/')
        .trustLocalhost()
        .key(sslConfig.privateKey)
        .cert(sslConfig.certificate)
        .expect(200)

i don't think .trustLocalhost() is doing anything there, as you seem to be including your cert and a private key in the request as well

pskfry avatar Apr 28 '20 00:04 pskfry

Has this issue been resolved? I am using: macos 10.15.7, supertest version - 6.0.1 node version - v14.15.0

And getting the same issue: I am using - process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; and using trustLocalhost()

unfortunately, I'm still getting this error: Error: write EPROTO 4637937088:error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1544:SSL alert number 40

dvrich avatar Dec 05 '20 21:12 dvrich

This is still an issue using supertest 6.2.4 with node 16.13.0 Neither process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' or disableTLSCerts() work.

antmo646 avatar Sep 01 '22 11:09 antmo646

This is still an issue using supertest 6.2.4 with node 16.13.0 Neither process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' or disableTLSCerts() work.

I found the following strategy to work when using supertest, jest, and self-signed certificates in my local environment:

supertest: 6.3.1
node: 14.15
import 'dotenv/config';
import Fs from 'fs-extra';
import request from 'supertest';

const options = {};

if (process.env.SSL_ACTIVATED === 'true') {
	options['key'] = Fs.readFileSync('/path/to/key.pem');
	options['cert'] = Fs.readFileSync('/path/to/cert.pem');
}

const baseUrl = process.env.API_DOMAIN;

describe('/get products', () => {
  test('request succeeds', async () => {
    return await request(baseUrl)
    .get(`/products`)
    .set('Accept', 'application/json')
    .key(options.key)
    .cert(options.cert)
    .disableTLSCerts()
    .expect(200)
    .catch((error, response) => {
      if (error) throw error;
      console.log(response);
    });
  });
});

ehubbell avatar Nov 14 '22 20:11 ehubbell

Check here: #737

andre5s avatar Dec 02 '22 15:12 andre5s