Skip to content

Commit 2ed5cdd

Browse files
committed
test: replace netcat with Python HTTP server in network tests
1 parent 4f048b8 commit 2ed5cdd

File tree

3 files changed

+62
-49
lines changed

3 files changed

+62
-49
lines changed

packages/js-sdk/tests/sandbox/network.test.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,27 @@ describe('maskRequestHost option', () => {
205205
sandboxTest.skipIf(isDebug)(
206206
'verify maskRequestHost modifies Host header correctly',
207207
async ({ sandbox }) => {
208-
// Install netcat for testing
209-
await sandbox.commands.run('apt-get update', { user: 'root' })
210-
await sandbox.commands.run('apt-get install -y netcat-traditional', {
211-
user: 'root',
212-
})
213-
214208
const port = 8080
215-
const outputFile = '/tmp/nc_output.txt'
216-
217-
// Start netcat listener in background to capture request headers
218-
sandbox.commands.run(`nc -l -p ${port} > ${outputFile}`, {
219-
background: true,
220-
user: 'root',
221-
})
209+
const outputFile = '/tmp/headers.txt'
210+
211+
// Start a Python HTTP server that captures request headers and writes them to a file
212+
sandbox.commands.run(
213+
`python3 -c "
214+
import http.server
215+
class H(http.server.BaseHTTPRequestHandler):
216+
def do_GET(self):
217+
with open('${outputFile}', 'w') as f:
218+
for k, v in self.headers.items():
219+
f.write(k + ': ' + v + chr(10))
220+
self.send_response(200)
221+
self.end_headers()
222+
def log_message(self, *a): pass
223+
http.server.HTTPServer(('', ${port}), H).handle_request()
224+
"`,
225+
{ background: true }
226+
)
222227

223-
// Wait for netcat to start
224-
await new Promise((resolve) => setTimeout(resolve, 3000))
228+
await new Promise((resolve) => setTimeout(resolve, 2000))
225229

226230
// Get the public URL for the sandbox
227231
const sandboxUrl = `https://${sandbox.getHost(port)}`
@@ -231,13 +235,13 @@ describe('maskRequestHost option', () => {
231235
try {
232236
await fetch(sandboxUrl, { signal: AbortSignal.timeout(5000) })
233237
} catch (error) {
234-
// Request may fail since netcat doesn't respond properly, but headers are captured
238+
// Request may timeout, but headers are captured by the server
235239
}
236240

237-
// Read the captured output from inside the sandbox
238-
const result = await sandbox.commands.run(`cat ${outputFile}`, {
239-
user: 'root',
240-
})
241+
await new Promise((resolve) => setTimeout(resolve, 1000))
242+
243+
// Read the captured headers from inside the sandbox
244+
const result = await sandbox.commands.run(`cat ${outputFile}`)
241245

242246
// Verify the Host header was modified according to maskRequestHost
243247
assert.include(result.stdout, 'Host:')

packages/python-sdk/tests/async/sandbox_async/test_network.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,27 @@ async def test_mask_request_host(async_sandbox_factory):
171171

172172
import httpx
173173

174-
# Install netcat for testing
175-
await async_sandbox.commands.run("apt-get update", user="root")
176-
await async_sandbox.commands.run(
177-
"apt-get install -y netcat-traditional", user="root"
178-
)
179-
180174
port = 8080
181-
output_file = "/tmp/nc_output.txt"
175+
output_file = "/tmp/headers.txt"
182176

183-
# Start netcat listener in background to capture request headers
177+
# Start a Python HTTP server that captures request headers and writes them to a file
184178
await async_sandbox.commands.run(
185-
f"nc -l -p {port} > {output_file}",
179+
f"""python3 -c "
180+
import http.server, json
181+
class H(http.server.BaseHTTPRequestHandler):
182+
def do_GET(self):
183+
with open('{output_file}', 'w') as f:
184+
for k, v in self.headers.items():
185+
f.write(k + ': ' + v + chr(10))
186+
self.send_response(200)
187+
self.end_headers()
188+
def log_message(self, *a): pass
189+
http.server.HTTPServer(('', {port}), H).handle_request()
190+
" """,
186191
background=True,
187-
timeout=0,
188-
user="root",
189192
)
190193

191-
# Wait for netcat to start
192-
await asyncio.sleep(3)
194+
await asyncio.sleep(2)
193195

194196
# Get the public URL for the sandbox
195197
sandbox_url = f"https://{async_sandbox.get_host(port)}"
@@ -200,11 +202,12 @@ async def test_mask_request_host(async_sandbox_factory):
200202
try:
201203
await client.get(sandbox_url, timeout=5.0)
202204
except Exception:
203-
# Request may fail since netcat doesn't respond properly, but headers are captured
204205
pass
205206

206-
# Read the captured output from inside the sandbox
207-
result = await async_sandbox.commands.run(f"cat {output_file}", user="root")
207+
await asyncio.sleep(1)
208+
209+
# Read the captured headers from inside the sandbox
210+
result = await async_sandbox.commands.run(f"cat {output_file}")
208211

209212
# Verify the Host header was modified according to mask_request_host
210213
assert "Host:" in result.stdout

packages/python-sdk/tests/sync/sandbox_sync/test_network.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,27 @@ def test_mask_request_host(sandbox_factory):
169169

170170
import httpx
171171

172-
# Install netcat for testing
173-
sandbox.commands.run("apt-get update", user="root")
174-
sandbox.commands.run("apt-get install -y netcat-traditional", user="root")
175-
176172
port = 8080
177-
output_file = "/tmp/nc_output.txt"
173+
output_file = "/tmp/headers.txt"
178174

179-
# Start netcat listener in background to capture request headers
175+
# Start a Python HTTP server that captures request headers and writes them to a file
180176
sandbox.commands.run(
181-
f"nc -l -p {port} > {output_file}",
177+
f"""python3 -c "
178+
import http.server, json
179+
class H(http.server.BaseHTTPRequestHandler):
180+
def do_GET(self):
181+
with open('{output_file}', 'w') as f:
182+
for k, v in self.headers.items():
183+
f.write(k + ': ' + v + chr(10))
184+
self.send_response(200)
185+
self.end_headers()
186+
def log_message(self, *a): pass
187+
http.server.HTTPServer(('', {port}), H).handle_request()
188+
" """,
182189
background=True,
183-
user="root",
184190
)
185191

186-
# Wait for netcat to start
187-
time.sleep(3)
192+
time.sleep(2)
188193

189194
# Get the public URL for the sandbox
190195
sandbox_url = f"https://{sandbox.get_host(port)}"
@@ -195,11 +200,12 @@ def test_mask_request_host(sandbox_factory):
195200
try:
196201
client.get(sandbox_url, timeout=5.0)
197202
except Exception:
198-
# Request may fail since netcat doesn't respond properly, but headers are captured
199203
pass
200204

201-
# Read the captured output from inside the sandbox
202-
result = sandbox.commands.run(f"cat {output_file}", user="root")
205+
time.sleep(1)
206+
207+
# Read the captured headers from inside the sandbox
208+
result = sandbox.commands.run(f"cat {output_file}")
203209

204210
# Verify the Host header was modified according to mask_request_host
205211
assert "Host:" in result.stdout

0 commit comments

Comments
 (0)