|
15 | 15 | import assert from 'node:assert'
|
16 | 16 | import { test, describe, before, after } from 'node:test'
|
17 | 17 | import { fileURLToPath } from 'node:url'
|
| 18 | +import net from 'node:net' |
| 19 | +import getPort from 'get-port' |
18 | 20 | import '../build/globals.js'
|
19 | 21 | import { isMain, normalizeExt } from '../build/cli.js'
|
20 | 22 |
|
21 | 23 | const __filename = fileURLToPath(import.meta.url)
|
22 | 24 | const spawn = $.spawn
|
23 | 25 | const nodeMajor = +process.versions?.node?.split('.')[0]
|
24 | 26 | const test22 = nodeMajor >= 22 ? test : test.skip
|
| 27 | +const getServer = (resp = [], log = console.log) => { |
| 28 | + const server = net.createServer() |
| 29 | + server.on('connection', (conn) => { |
| 30 | + conn.on('data', (d) => { |
| 31 | + conn.write(resp.shift() || 'pong') |
| 32 | + }) |
| 33 | + }) |
| 34 | + server.stop = () => new Promise((resolve) => server.close(() => resolve())) |
| 35 | + server.start = (port) => |
| 36 | + new Promise((resolve) => server.listen(port, () => resolve(server))) |
| 37 | + return server |
| 38 | +} |
25 | 39 |
|
26 | 40 | describe('cli', () => {
|
27 | 41 | // Helps detect unresolved ProcessPromise.
|
@@ -123,19 +137,22 @@ describe('cli', () => {
|
123 | 137 | assert.ok(p.stderr.endsWith(cwd + '\n'))
|
124 | 138 | })
|
125 | 139 |
|
126 |
| - test('scripts from https', async () => { |
127 |
| - const server = $`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080` |
| 140 | + test('scripts from https 200', async () => { |
| 141 | + const resp = await fs.readFile(path.resolve('test/fixtures/echo.http')) |
| 142 | + const port = await getPort() |
| 143 | + const server = await getServer([resp]).start(port) |
128 | 144 | const out =
|
129 |
| - await $`node build/cli.js --verbose http://127.0.0.1:8080/echo.mjs` |
| 145 | + await $`node build/cli.js --verbose http://127.0.0.1:${port}/echo.mjs` |
130 | 146 | assert.match(out.stderr, /test/)
|
131 |
| - await server.kill() |
| 147 | + await server.stop() |
132 | 148 | })
|
133 | 149 |
|
134 |
| - test('scripts from https not ok', async () => { |
135 |
| - const server = $`echo $'HTTP/1.1 500\n\n' | nc -l 8081` |
136 |
| - const out = await $`node build/cli.js http://127.0.0.1:8081`.nothrow() |
| 150 | + test('scripts from https 500', async () => { |
| 151 | + const port = await getPort() |
| 152 | + const server = await getServer(['HTTP/1.1 500\n\n']).listen(port) |
| 153 | + const out = await $`node build/cli.js http://127.0.0.1:${port}`.nothrow() |
137 | 154 | assert.match(out.stderr, /Error: Can't get/)
|
138 |
| - await server.kill() |
| 155 | + await server.stop() |
139 | 156 | })
|
140 | 157 |
|
141 | 158 | test('scripts with no extension', async () => {
|
|
0 commit comments