|
1 | 1 | import process from 'node:process'; |
2 | 2 | import {fileURLToPath} from 'node:url'; |
3 | 3 | import path from 'node:path'; |
4 | | -import test from 'ava'; |
| 4 | +import {test, after} from 'node:test'; |
| 5 | +import assert from 'node:assert/strict'; |
5 | 6 | import got from 'got'; |
6 | 7 | import phpServer from '../index.js'; |
7 | 8 |
|
8 | 9 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
9 | 10 |
|
10 | 11 | process.chdir(__dirname); |
11 | 12 |
|
12 | | -test('start a PHP server', async t => { |
| 13 | +// Track all servers created during tests |
| 14 | +const activeServers = new Set(); |
| 15 | + |
| 16 | +// Cleanup hook to ensure all servers are stopped |
| 17 | +after(() => { |
| 18 | + for (const server of activeServers) { |
| 19 | + server.stop(); |
| 20 | + } |
| 21 | + |
| 22 | + activeServers.clear(); |
| 23 | +}); |
| 24 | + |
| 25 | +test('start a PHP server', async () => { |
13 | 26 | const server = await phpServer({base: 'fixtures/200'}); |
| 27 | + activeServers.add(server); |
14 | 28 | const {statusCode, body} = await got(server.url); |
15 | | - t.is(statusCode, 200); |
16 | | - t.is(body, 'Hello World'); |
| 29 | + assert.equal(statusCode, 200); |
| 30 | + assert.equal(body, 'Hello World'); |
17 | 31 | server.stop(); |
| 32 | + activeServers.delete(server); |
18 | 33 | }); |
19 | 34 |
|
20 | | -test('start a PHP server when the status code is 301', async t => { |
| 35 | +test('start a PHP server when the status code is 301', async () => { |
21 | 36 | const server = await phpServer({base: 'fixtures/301'}); |
| 37 | + activeServers.add(server); |
22 | 38 | const {statusCode, body} = await got(server.url); |
23 | | - t.is(statusCode, 200); |
24 | | - t.is(body, '301 Redirected!'); |
| 39 | + assert.equal(statusCode, 200); |
| 40 | + assert.equal(body, '301 Redirected!'); |
25 | 41 | server.stop(); |
| 42 | + activeServers.delete(server); |
26 | 43 | }); |
27 | 44 |
|
28 | | -test('start a PHP server when the status code is 400', async t => { |
| 45 | +test('start a PHP server when the status code is 400', async () => { |
29 | 46 | const server = await phpServer({base: 'fixtures/400'}); |
| 47 | + activeServers.add(server); |
30 | 48 | const {statusCode} = await got(server.url, {throwHttpErrors: false}); |
31 | | - t.is(statusCode, 400); |
| 49 | + assert.equal(statusCode, 400); |
32 | 50 | server.stop(); |
| 51 | + activeServers.delete(server); |
33 | 52 | }); |
34 | 53 |
|
35 | | -test('start a PHP server when the status code is 404', async t => { |
| 54 | +test('start a PHP server when the status code is 404', async () => { |
36 | 55 | const server = await phpServer({base: 'fixtures/404'}); |
| 56 | + activeServers.add(server); |
37 | 57 | const {statusCode} = await got(server.url, {throwHttpErrors: false}); |
38 | | - t.is(statusCode, 404); |
| 58 | + assert.equal(statusCode, 404); |
39 | 59 | server.stop(); |
| 60 | + activeServers.delete(server); |
40 | 61 | }); |
41 | 62 |
|
42 | | -test('expose environment variables', async t => { |
| 63 | +test('expose environment variables', async () => { |
43 | 64 | const server = await phpServer({ |
44 | 65 | base: 'fixtures/env', |
45 | 66 | env: { |
46 | 67 | FOOBAR: 'foobar', |
47 | 68 | }, |
48 | 69 | }); |
| 70 | + activeServers.add(server); |
49 | 71 | const {body} = await got(server.url); |
50 | | - t.is(body, 'foobar'); |
| 72 | + assert.equal(body, 'foobar'); |
51 | 73 | server.stop(); |
| 74 | + activeServers.delete(server); |
52 | 75 | }); |
53 | 76 |
|
54 | | -test('expose custom INI directive', async t => { |
| 77 | +test('expose custom INI directive', async () => { |
55 | 78 | const server = await phpServer({ |
56 | 79 | base: 'fixtures/directives', |
57 | 80 | directives: { |
58 | 81 | error_log: 'foobar', // eslint-disable-line camelcase |
59 | 82 | }, |
60 | 83 | }); |
| 84 | + activeServers.add(server); |
61 | 85 | const {body} = await got(server.url); |
62 | | - t.is(body, 'foobar'); |
| 86 | + assert.equal(body, 'foobar'); |
63 | 87 | server.stop(); |
| 88 | + activeServers.delete(server); |
| 89 | +}); |
| 90 | + |
| 91 | +test('show detailed error message when the status code is 500', async () => { |
| 92 | + await assert.rejects( |
| 93 | + phpServer({base: 'fixtures/500'}), |
| 94 | + error => { |
| 95 | + // Should contain "Fatal error" and "undefined_function" |
| 96 | + assert.match(error.message, /500 error: Fatal error/); |
| 97 | + assert.match(error.message, /undefined_function/); |
| 98 | + return true; |
| 99 | + }, |
| 100 | + ); |
64 | 101 | }); |
0 commit comments