-
Notifications
You must be signed in to change notification settings - Fork 702
Expand file tree
/
Copy pathserver.test.ts
More file actions
130 lines (102 loc) · 4.49 KB
/
Copy pathserver.test.ts
File metadata and controls
130 lines (102 loc) · 4.49 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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();
});
test('rewrite extensionless routes to index when single-page rewrites are set', async () => {
const configWithSingle = {
...config,
rewrites: [{ source: '/**/:path([^/.]+)', destination: '/index.html' }],
};
const address = await startServer({ port: 3005 }, configWithSingle, {});
const response = await fetch(`${address.local!}/users/settings`);
expect(response.statusCode).toBe(200);
expect(response.body).toContain('Hello there!');
});
test('do not rewrite dotted request paths when single-page rewrites are set', async () => {
const configWithSingle = {
...config,
rewrites: [{ source: '/**/:path([^/.]+)', destination: '/index.html' }],
};
const address = await startServer({ port: 3006 }, configWithSingle, {});
const extensionResponse = await fetch(`${address.local!}/bundle.js`);
const missingExtensionResponse = await fetch(
`${address.local!}/does-not-exist.js`,
);
expect(extensionResponse.statusCode).toBe(200);
expect(extensionResponse.body.trimEnd()).toBe("console.log('bundle');");
expect(missingExtensionResponse.statusCode).toBe(404);
});
});