Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More HTTPS tests #1704

Merged
merged 5 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion test/helpers/create-https-test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import net from 'net';
import express from 'express';
import pify from 'pify';
import pem from 'pem';
import type {SecureContextOptions} from 'tls';

export type HttpsServerOptions = {
commonName?: string;
days?: number;
ciphers?: SecureContextOptions['ciphers'];
honorCipherOrder?: SecureContextOptions['honorCipherOrder'];
minVersion?: SecureContextOptions['minVersion'];
maxVersion?: SecureContextOptions['maxVersion'];
};

export interface ExtendedHttpsTestServer extends express.Express {
Expand Down Expand Up @@ -49,7 +54,11 @@ const createHttpsTestServer = async (options: HttpsServerOptions = {}): Promise<
cert: serverCert,
ca: caCert,
requestCert: true,
rejectUnauthorized: false // This should be checked by the test
rejectUnauthorized: false, // This should be checked by the test
ciphers: options.ciphers,
honorCipherOrder: options.honorCipherOrder,
minVersion: options.minVersion,
maxVersion: options.maxVersion
},
server
);
Expand Down
55 changes: 54 additions & 1 deletion test/https.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import {DetailedPeerCertificate} from 'tls';
import tls, {DetailedPeerCertificate} from 'tls';
import pEvent from 'p-event';
import pify from 'pify';
import pem from 'pem';
Expand Down Expand Up @@ -434,3 +434,56 @@ test('client certificate PFX', withHttpsServer(), async (t, server, got) => {
t.is(response.peerCertificate.subject.CN, 'client');
t.is(response.peerCertificate.issuer.CN, 'authority');
});

const ciphers = tls.getCiphers().map(cipher => cipher.toUpperCase());

test('https request with `ciphers` option', withHttpsServer({ciphers: `${ciphers[0]!}:${ciphers[1]!}:${ciphers[2]!}`}), async (t, server, got) => {
server.get('/', (request, response) => {
response.json({
cipher: (request.socket as any).getCipher().name
});
});

const response: any = await got({
Giotino marked this conversation as resolved.
Show resolved Hide resolved
httpsOptions: {
ciphers: ciphers[0]
}
}).json();

t.is(response.cipher, ciphers[0]);
});

test('https request with `honorCipherOrder` option', withHttpsServer({ciphers: `${ciphers[0]!}:${ciphers[1]!}`}), async (t, server, got) => {
server.get('/', (request, response) => {
response.json({
cipher: (request.socket as any).getCipher().name
});
});

const response: any = await got({
Giotino marked this conversation as resolved.
Show resolved Hide resolved
httpsOptions: {
ciphers: `${ciphers[1]!}:${ciphers[0]!}`,
honorCipherOrder: true
}
}).json();

t.is(response.cipher, ciphers[0]);
});

test('https request with `minVersion` option', withHttpsServer({maxVersion: 'TLSv1.2'}), async (t, server, got) => {
server.get('/', (request, response) => {
response.json({
version: (request.socket as any).getCipher().version
});
});

const request = got({
httpsOptions: {
minVersion: 'TLSv1.3'
}
});

await t.throwsAsync(request, {
code: 'EPROTO'
});
});