-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathserver.ssl.test.ts
114 lines (88 loc) · 3.8 KB
/
server.ssl.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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);
});
});