supertest
supertest copied to clipboard
Use supertest for self-sign certificate
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
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.
this method does not exists anymore...
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.
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.
This should be reopened @gjohnson
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).
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
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)
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
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
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.
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);
});
});
});
Check here: #737