Spaces:
Running
Running
Download tests/server-binding.test.mjs from Luca448/APP-Backend: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/tests/server-binding.test.mjs
- Command line
-
hf download hf://spaces/Luca448/APP-Backend/tests/server-binding.test.mjs
-
curl -L -o server-binding.test.mjs https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/tests/server-binding.test.mjs
1.47 kB
| import assert from 'node:assert/strict'; | |
| import { spawn } from 'node:child_process'; | |
| import { networkInterfaces } from 'node:os'; | |
| import test from 'node:test'; | |
| function firstExternalIpv4() { | |
| for (const addresses of Object.values(networkInterfaces())) { | |
| for (const address of addresses ?? []) { | |
| if (address.family === 'IPv4' && !address.internal) return address.address; | |
| } | |
| } | |
| throw new Error('No external IPv4 address is available for the binding test'); | |
| } | |
| async function waitForResponse(url, child) { | |
| const deadline = Date.now() + 15_000; | |
| let lastError; | |
| while (Date.now() < deadline) { | |
| if (child.exitCode !== null) { | |
| throw new Error(`Server exited before becoming reachable (code ${child.exitCode})`); | |
| } | |
| try { | |
| return await fetch(url); | |
| } catch (error) { | |
| lastError = error; | |
| await new Promise((resolve) => setTimeout(resolve, 100)); | |
| } | |
| } | |
| throw lastError ?? new Error('Server did not become reachable'); | |
| } | |
| test('serves the Space app through a non-loopback container interface', async (t) => { | |
| const port = 20_000 + Math.floor(Math.random() * 20_000); | |
| const child = spawn(process.execPath, ['server.js'], { | |
| cwd: new URL('..', import.meta.url), | |
| env: { ...process.env, PORT: String(port) }, | |
| stdio: 'ignore', | |
| }); | |
| t.after(() => child.kill()); | |
| const response = await waitForResponse(`http://${firstExternalIpv4()}:${port}/`, child); | |
| assert.equal(response.status, 200); | |
| }); | |