Skip to content

Commit

Permalink
test(cli): replace nc with node:net
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Dec 15, 2024
1 parent d4446ec commit ba563c3
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import assert from 'node:assert'
import { test, describe, before, after } from 'node:test'
import { fileURLToPath } from 'node:url'
import net from 'node:net'
import getPort from 'get-port'
import '../build/globals.js'
import { isMain, normalizeExt } from '../build/cli.js'
Expand All @@ -23,6 +24,19 @@ const __filename = fileURLToPath(import.meta.url)
const spawn = $.spawn
const nodeMajor = +process.versions?.node?.split('.')[0]
const test22 = nodeMajor >= 22 ? test : test.skip
const getServer = (resp = [], log = console.log) => {
const server = net.createServer()
server.on('connection', (conn) => {
conn.on('data', (d) => {
conn.write(resp.pop() || 'pong')
})
})
server.start = (port) =>
new Promise((resolve) => server.listen(port, () => resolve(server)))
server.stop = () =>
new Promise((resolve) => server.close(() => resolve(server)))
return server
}

describe('cli', () => {
// Helps detect unresolved ProcessPromise.
Expand Down Expand Up @@ -125,21 +139,21 @@ describe('cli', () => {
})

test('scripts from https', async () => {
const resp = await fs.readFile(path.resolve('test/fixtures/echo.http'))
const port = await getPort()
const server = $`cat ${path.resolve('test/fixtures/echo.http')} | nc -l ${port}`
const server = await getServer([resp]).start(port)
const out =
await $`sleep 10 && node build/cli.js --verbose http://127.0.0.1:${port}/echo.mjs`
await $`node build/cli.js --verbose http://127.0.0.1:${port}/echo.mjs`
assert.match(out.stderr, /test/)
await server.kill()
await server.stop()
})

test('scripts from https not ok', async () => {
const port = await getPort()
const server = $`echo $'HTTP/1.1 500\n\n' | nc -l ${port}`
const out =
await $`sleep 10 && node build/cli.js http://127.0.0.1:${port}`.nothrow()
const server = await getServer(['HTTP/1.1 500\n\n']).listen(port)
const out = await $`node build/cli.js http://127.0.0.1:${port}`.nothrow()
assert.match(out.stderr, /Error: Can't get/)
await server.kill()
await server.stop()
})

test('scripts with no extension', async () => {
Expand Down

0 comments on commit ba563c3

Please sign in to comment.