-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathserver.test.ts
100 lines (78 loc) · 3.36 KB
/
server.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
// tests/server.test.ts
// Tests for the server creating function.
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';
import { logger } from '../source/utilities/logger.js';
// The path to the fixtures for this test file.
const fixture = 'tests/__fixtures__/server/';
// The configuration from the fixture.
const config = await loadConfiguration(process.cwd(), fixture, {});
// A `fetch` instance to make requests to the server.
const fetch = createFetch({ throwHttpErrors: false });
afterEach(() => {
vi.restoreAllMocks();
});
describe('utilities/server', () => {
// Make sure the server starts on the specified port.
test('start server on specified port', async () => {
const address = await startServer({ port: 3001 }, config, {});
expect(address.local).toBe('http://localhost:3001');
expect(address.network).toMatch(/^http:\/\/.*:3001$/);
expect(address.previous).toBeUndefined();
const response = await fetch(address.local!);
expect(response.ok);
});
// Make sure the server starts on the specified port and host.
test('start server on specified port and host', async () => {
const address = await startServer({ port: 3002, host: '::1' }, config, {});
expect(address.local).toBe('http://[::1]:3002');
expect(address.network).toMatch(/^http:\/\/.*:3002$/);
expect(address.previous).toBeUndefined();
const response = await fetch(address.local!);
expect(response.ok);
});
// Make sure the server starts on the specified port and host.
test('start server on different port if port is already occupied', async () => {
const address = await startServer({ port: 3002, host: '::1' }, config, {});
expect(address.local).not.toBe('http://[::1]:3002');
expect(address.network).not.toMatch(/^http:\/\/.*:3002$/);
expect(address.previous).toBe(3002);
const response = await fetch(address.local!);
expect(response.ok);
});
// Make sure the server logs requests by default.
test('log requests to the server by default', async () => {
const consoleSpy = vi.spyOn(logger, 'http');
const address = await startServer({ port: 3003, host: '::1' }, config, {});
const response = await fetch(address.local!);
expect(response.ok);
expect(consoleSpy).toBeCalledTimes(2);
const requestLog = consoleSpy.mock.calls[0].join(' ');
const responseLog = consoleSpy.mock.calls[1].join(' ');
const time = new Date();
const formattedTime = `${time.toLocaleDateString()} ${time.toLocaleTimeString()}`;
const ip = '::1';
const requestString = 'GET /';
const status = 200;
expect(requestLog).toMatch(
new RegExp(`${formattedTime}.*${ip}.*${requestString}`),
);
expect(responseLog).toMatch(
new RegExp(
`${formattedTime}.*${ip}.*Returned ${status} in [0-9][0-9]? ms`,
),
);
});
// Make sure the server logs requests by default.
test('log requests to the server by default', async () => {
const consoleSpy = vi.spyOn(logger, 'http');
const address = await startServer({ port: 3004 }, config, {
'--no-request-logging': true,
});
const response = await fetch(address.local!);
expect(response.ok);
expect(consoleSpy).not.toHaveBeenCalled();
});
});