Skip to content

Commit

Permalink
Test: Included tests for verifying SSL hosting (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
schemburkar authored Jul 27, 2022
1 parent 92fd640 commit 0616ce3
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source/utilities/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export const startServer = async (
const sslCert = args['--ssl-cert'];
const sslKey = args['--ssl-key'];
const sslPass = args['--ssl-pass'];
const isPFXFormat = sslCert && /[.](?<extension>pfx|p12)$/.exec(sslCert);
const isPFXFormat =
sslCert && /[.](?<extension>pfx|p12)$/.exec(sslCert) !== null;
const useSsl = sslCert && (sslKey || sslPass || isPFXFormat);

let serverConfig: http.ServerOptions | https.ServerOptions = {};
Expand Down
23 changes: 23 additions & 0 deletions tests/__fixtures__/server/ssl/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- index.html -->
<!-- The HTML page rendered when the user visits the root URL. -->

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Page description -->
<title>Serve Application</title>
<meta
name="description"
content="An example web page you can serve with `serve`."
/>
<!-- Define the character set we use, as well as the default width. -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<!-- Tell the browser which icons to show. -->
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="alternate icon" type="image/x-icon" href="/favicon.ico" />
</head>
<body>
Hello there!
</body>
</html>
1 change: 1 addition & 0 deletions tests/__fixtures__/server/ssl/cert-password
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vercel
30 changes: 30 additions & 0 deletions tests/__fixtures__/server/ssl/cert-with-pass.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFNjBgBgkqhkiG9w0BBQ0wUzAyBgkqhkiG9w0BBQwwJQQQ7KCQzNwvExz/4GIj
PAE0pgIDAYagMAwGCCqGSIb3DQIJBQA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Pca0ssRWdzSEQE3uENtThXqm4pJZQ2IBHoMG
vT89Q+nDhS6zESdv4DAdc0nYeGsFwfzfrnogaZkny+Gnvtkx0qa3pcL2GznQx95N
Qzb17TULscmjuCz+5q2dzIvybyw5pHQAOYfHyzJWe95cZtQMD1kadB1HmFWWtiMU
36rBPZXGvLkqzLfaLlhyFoQYHZqyi89SZNAMhBh86Rgb7piyD9aGe8kc1s3VLk2Y
BqaOnu6lEsj6DAOA0sqsVEhrHTIfNS/NKG/rvDOjTAdlYNc24TEq6oukv0hQBF97
hYvgye0/wh5gwu4qx9KjFyVNCAMWWnlqCm73DK5ZNk5Jjkis4Jm5POFdJRLQgjHL
0usNk2aJNubDqJym2M7X53/gsg4fqbXBu6dg3/8b2Tx2QHOJM69PKK02NQf3By3f
ByAE/mdBdxVvlxRJyyjjuWN3xyWPgGcnDqWBqLW4Kh9oRZik9EMYT92UHxAuilBa
vm1MORT4EYNLRLTeRrO/p9ENRkr7G6c/1p1+Uh2fz0bqyvwxUAlW2UC1+8nx2fGm
N2xwsBaG9+tjseSR/OSMhYzImmdagTk6uYWuLofKqBTte0oqQyNrVs2L
-----END ENCRYPTED PRIVATE KEY-----
19 changes: 19 additions & 0 deletions tests/__fixtures__/server/ssl/cert-with-pass.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDDDCCAfSgAwIBAgIIFtK8Sw/EzO4wDQYJKoZIhvcNAQELBQAwFDESMBAGA1UE
AxMJbG9jYWxob3N0MB4XDTIyMDcxODE3MDY1MloXDTIzMDcxODE3MDY1MlowFDES
MBAGA1UEAxMJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAxicgLe07Kg/oHOPTvljjpLhfUd3VLlrStvIF2RSF25jowmg1LtronKEaMZZF
TbdXwrvM1erkOALZ5e1FBhvuziYZ3BzjRxCI4omdWLYwzjPHWbbGermPIdN6zjHT
LwInqCq55a3lLXge8TClA3Ws92uZcuwJKe9uDnC/IV2P1QLNsbpb+dSnZyKyIO8A
ILgMSknh2fPcXdMklKi50CiUo6i6xAKShcvI8BOr18CUdWqEVWPBvyvT4jgnGfBo
GPlMl2DE4UwHVcl8Ga7If3pxbpFzz284gKXXLRwhyiYwM5RIHOAm+u8jqqB1eR4N
IqjbB8zgi9JMxMwVZeO5nF6kGQIDAQABo2IwYDAMBgNVHRMBAf8EAjAAMA4GA1Ud
DwEB/wQEAwIFoDAWBgNVHSUBAf8EDDAKBggrBgEFBQcDATAXBgNVHREBAf8EDTAL
gglsb2NhbGhvc3QwDwYKKwYBBAGCN1QBAQQBAjANBgkqhkiG9w0BAQsFAAOCAQEA
U69M0yKkku3biHXhpCieV99wvMqhn0D8zHmMbq/DJhmo0rWX4ka4n6nFy9JafEAa
QdmvUyAeHKUbk1S2MVeM/TXyPtSHwoQ4IoRfhN6YUuY6zTOkeQL5bPJ51LJawqc9
DRGYQlXw/al88b0cmLr/twI2yf38I+PTpieD3ILVmcEYcp+Vb5nA2mDyHwYM6Rgn
3nXh1XXwlJiw9KCDN7wBYzRwAoH36Xz9bsm1hTQM/LKAJfT4fv8Gv+mN8hCLN+Je
Srgl5QWgozszZZ4+0NQbanornmxygpXvbZCa+f8HLaG+QbQhYhE7vGXTZjN/eHmn
ouGRrIy1RYE8fFUAgFKyJw==
-----END CERTIFICATE-----
Binary file added tests/__fixtures__/server/ssl/cert-with-pass.pfx
Binary file not shown.
28 changes: 28 additions & 0 deletions tests/__fixtures__/server/ssl/cert.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDGJyAt7TsqD+gc
49O+WOOkuF9R3dUuWtK28gXZFIXbmOjCaDUu2uicoRoxlkVNt1fCu8zV6uQ4Atnl
7UUGG+7OJhncHONHEIjiiZ1YtjDOM8dZtsZ6uY8h03rOMdMvAieoKrnlreUteB7x
M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6LK7URX6mRDZ981Ww+0L4pQKBgHH/
oLfV/dexHc9NiqY9FPTm3YiktJjQwSDkj/J8Cuc0uatSoVxeOITWYKVTq18GkqFj
xz2/UujSUiPzmdQrM44WLFtjNGcM5gqtyCmZYblFOW6H8dI7zXbVy/+FQ7Ae7hJZ
ABdzci7u/0HQbl/dMWAdfFA+u+rBYwT4K5BGs6vxAoGBAK4PqlC/zEafEdHxqVlE
OuXoXR2xn9zWsilMVqAj+lF2AtvepDh/ofVBcnsYmf/kMepah4mnZNRoBvyHUcBH
DYd/Jae5DKyBALHKuScF+gaacVeY70qhD/oVCws3frMayU/eddn5MBeWS5w28ZRY
vdKLC/aPF8s/C4cD3F7LTJ2k
-----END PRIVATE KEY-----
19 changes: 19 additions & 0 deletions tests/__fixtures__/server/ssl/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDDDCCAfSgAwIBAgIIFtK8Sw/EzO4wDQYJKoZIhvcNAQELBQAwFDESMBAGA1UE
AxMJbG9jYWxob3N0MB4XDTIyMDcxODE3MDY1MloXDTIzMDcxODE3MDY1MlowFDES
MBAGA1UEAxMJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAxicgLe07Kg/oHOPTvljjpLhfUd3VLlrStvIF2RSF25jowmg1LtronKEaMZZF
TbdXwrvM1erkOALZ5e1FBhvuziYZ3BzjRxCI4omdWLYwzjPHWbbGermPIdN6zjHT
LwInqCq55a3lLXge8TClA3Ws92uZcuwJKe9uDnC/IV2P1QLNsbpb+dSnZyKyIO8A
ILgMSknh2fPcXdMklKi50CiUo6i6xAKShcvI8BOr18CUdWqEVWPBvyvT4jgnGfBo
GPlMl2DE4UwHVcl8Ga7If3pxbpFzz284gKXXLRwhyiYwM5RIHOAm+u8jqqB1eR4N
IqjbB8zgi9JMxMwVZeO5nF6kGQIDAQABo2IwYDAMBgNVHRMBAf8EAjAAMA4GA1Ud
DwEB/wQEAwIFoDAWBgNVHSUBAf8EDDAKBggrBgEFBQcDATAXBgNVHREBAf8EDTAL
gglsb2NhbGhvc3QwDwYKKwYBBAGCN1QBAQQBAjANBgkqhkiG9w0BAQsFAAOCAQEA
U69M0yKkku3biHXhpCieV99wvMqhn0D8zHmMbq/DJhmo0rWX4ka4n6nFy9JafEAa
QdmvUyAeHKUbk1S2MVeM/TXyPtSHwoQ4IoRfhN6YUuY6zTOkeQL5bPJ51LJawqc9
DRGYQlXw/al88b0cmLr/twI2yf38I+PTpieD3ILVmcEYcp+Vb5nA2mDyHwYM6Rgn
3nXh1XXwlJiw9KCDN7wBYzRwAoH36Xz9bsm1hTQM/LKAJfT4fv8Gv+mN8hCLN+Je
Srgl5QWgozszZZ4+0NQbanornmxygpXvbZCa+f8HLaG+QbQhYhE7vGXTZjN/eHmn
ouGRrIy1RYE8fFUAgFKyJw==
-----END CERTIFICATE-----
Binary file added tests/__fixtures__/server/ssl/cert.pfx
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/__fixtures__/server/ssl/serve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"public": "app/",
"renderSingle": true
}
114 changes: 114 additions & 0 deletions tests/server.ssl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// tests/server.ssl.test.ts
// Tests for the serve with SSL/HTTPS.

import { afterEach, describe, test, expect, vi } from 'vitest';
import { extend as createFetch } from 'got';

import { loadConfiguration } from '../source/utilities/config.js';
import { startServer } from '../source/utilities/server.js';

// The path to the fixtures for this test file.
const fixtures = 'tests/__fixtures__/server/ssl';
// The configuration from the fixture.
const config = await loadConfiguration(process.cwd(), fixtures, {});
// A `fetch` instance to make requests to the server.
const fetch = createFetch({
https: { rejectUnauthorized: false },
});

afterEach(() => {
vi.restoreAllMocks();
});

describe('utilities/server', () => {
test('start server with SSL when PFX certificate is supplied', async () => {
const address = await startServer({ port: 5001 }, config, {
['--ssl-cert']: `${fixtures}/cert.pfx`,
});

expect(address.local).toBe('https://localhost:5001');
expect(address.network).toMatch(/^https:\/\/.*:5001$/);
expect(address.previous).toBeUndefined();

const response = await fetch(address.local!);

expect(response.ok);
});

test('start server with SSL when PFX certificate and password is supplied', async () => {
const address = await startServer({ port: 5002 }, config, {
['--ssl-cert']: `${fixtures}/cert-with-pass.pfx`,
['--ssl-pass']: `${fixtures}/cert-password`,
});

expect(address.local).toBe('https://localhost:5002');
expect(address.network).toMatch(/^https:\/\/.*:5002$/);
expect(address.previous).toBeUndefined();

const response = await fetch(address.local!);

expect(response.ok);
});

test('start server with SSL when PFX certificate is supplied and password is missing', async () => {
await expect(() =>
startServer({ port: 5003 }, config, {
['--ssl-cert']: `${fixtures}/cert-with-pass.pfx`,
}),
).rejects.toThrowError('failure');
});

test('start server with SSL when PFX certificate is supplied and password is invalid', async () => {
await expect(() =>
startServer({ port: 5003 }, config, {
['--ssl-cert']: `${fixtures}/cert-with-pass.pfx`,
['--ssl-pass']: `${fixtures}/cert-with-pass.pfx`,
}),
).rejects.toThrowError('failure');
});

test('start server with SSL when PEM certificate, key and password is supplied', async () => {
const address = await startServer({ port: 5004 }, config, {
['--ssl-cert']: `${fixtures}/cert-with-pass.pem`,
['--ssl-key']: `${fixtures}/cert-with-pass.key`,
['--ssl-pass']: `${fixtures}/cert-password`,
});

expect(address.local).toBe('https://localhost:5004');
expect(address.network).toMatch(/^https:\/\/.*:5004$/);
expect(address.previous).toBeUndefined();

const response = await fetch(address.local!);

expect(response.ok);
});

test('start server with SSL when PEM certificate, key is supplied and no password is supplied', async () => {
const address = await startServer({ port: 5005 }, config, {
['--ssl-cert']: `${fixtures}/cert.pem`,
['--ssl-key']: `${fixtures}/cert.key`,
});

expect(address.local).toBe('https://localhost:5005');
expect(address.network).toMatch(/^https:\/\/.*:5005$/);
expect(address.previous).toBeUndefined();

const response = await fetch(address.local!);

expect(response.ok);
});

test('start server without SSL when PEM certificate is supplied and key is missing', async () => {
const address = await startServer({ port: 5006 }, config, {
['--ssl-cert']: `${fixtures}/cert.pem`,
});

expect(address.local).toBe('http://localhost:5006');
expect(address.network).toMatch(/^http:\/\/.*:5006$/);
expect(address.previous).toBeUndefined();

const response = await fetch(address.local!);

expect(response.ok);
});
});

0 comments on commit 0616ce3

Please sign in to comment.