|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('tap').test; |
| 4 | +const http = require('http'); |
| 5 | +const httpServer = require('../lib/http-server'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +const root = path.join(__dirname, 'fixtures', 'root'); |
| 9 | + |
| 10 | +test('timeout: default behavior (no timeout specified)', (t) => { |
| 11 | + t.plan(2); |
| 12 | + |
| 13 | + const server = httpServer.createServer({ |
| 14 | + root |
| 15 | + }); |
| 16 | + |
| 17 | + server.listen(0, () => { |
| 18 | + const port = server.address().port; |
| 19 | + // Verify server was created successfully |
| 20 | + t.ok(server, 'server created without timeout option'); |
| 21 | + |
| 22 | + // Check that server has a timeout property (Node.js default is usually 120000ms = 2 minutes) |
| 23 | + // But we're not setting it, so it should use Node.js default |
| 24 | + t.ok(server.server, 'server has underlying server instance'); |
| 25 | + |
| 26 | + server.close(); |
| 27 | + t.end(); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +test('timeout: custom timeout value in seconds', (t) => { |
| 32 | + t.plan(2); |
| 33 | + |
| 34 | + const timeoutSeconds = 60; |
| 35 | + const server = httpServer.createServer({ |
| 36 | + root, |
| 37 | + timeout: timeoutSeconds |
| 38 | + }); |
| 39 | + |
| 40 | + server.listen(0, () => { |
| 41 | + const port = server.address().port; |
| 42 | + t.ok(server, 'server created with custom timeout'); |
| 43 | + |
| 44 | + // Verify timeout was set on the underlying server |
| 45 | + // Note: Node.js setTimeout expects milliseconds, but we're passing seconds |
| 46 | + // This test verifies the current behavior (may need adjustment after PR merge) |
| 47 | + const underlyingServer = server.server; |
| 48 | + t.ok(underlyingServer, 'server has underlying server instance'); |
| 49 | + |
| 50 | + server.close(); |
| 51 | + t.end(); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +test('timeout: disabled timeout (0)', (t) => { |
| 56 | + t.plan(2); |
| 57 | + |
| 58 | + const server = httpServer.createServer({ |
| 59 | + root, |
| 60 | + timeout: 0 |
| 61 | + }); |
| 62 | + |
| 63 | + server.listen(0, () => { |
| 64 | + const port = server.address().port; |
| 65 | + t.ok(server, 'server created with timeout disabled'); |
| 66 | + |
| 67 | + const underlyingServer = server.server; |
| 68 | + t.ok(underlyingServer, 'server has underlying server instance'); |
| 69 | + |
| 70 | + server.close(); |
| 71 | + t.end(); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +test('timeout: connection actually times out after specified duration', (t) => { |
| 76 | + t.plan(2); |
| 77 | + |
| 78 | + // Use a short timeout for testing (1 second = 1000ms) |
| 79 | + // Note: This assumes timeout is in milliseconds. If PR converts to milliseconds, |
| 80 | + // we may need to adjust this test |
| 81 | + const timeoutMs = 1000; |
| 82 | + const server = httpServer.createServer({ |
| 83 | + root, |
| 84 | + timeout: timeoutMs |
| 85 | + }); |
| 86 | + |
| 87 | + let timeoutFired = false; |
| 88 | + |
| 89 | + server.server.on('timeout', (socket) => { |
| 90 | + if (!timeoutFired) { |
| 91 | + timeoutFired = true; |
| 92 | + t.pass('timeout event fired'); |
| 93 | + socket.destroy(); |
| 94 | + server.close(); |
| 95 | + t.pass('timeout handling works'); |
| 96 | + t.end(); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + server.listen(0, () => { |
| 101 | + const port = server.address().port; |
| 102 | + |
| 103 | + // Create a connection but don't send any data |
| 104 | + const socket = require('net').createConnection(port, 'localhost', () => { |
| 105 | + // Don't send any data - keep connection idle to trigger timeout |
| 106 | + }); |
| 107 | + |
| 108 | + socket.on('error', () => { |
| 109 | + // Connection errors are expected when timeout fires |
| 110 | + }); |
| 111 | + |
| 112 | + // Safety timeout in case the server timeout doesn't work |
| 113 | + setTimeout(() => { |
| 114 | + if (!timeoutFired) { |
| 115 | + t.fail('server timeout did not fire within expected time'); |
| 116 | + socket.destroy(); |
| 117 | + server.close(); |
| 118 | + t.end(); |
| 119 | + } |
| 120 | + }, timeoutMs + 2000); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +test('timeout: server handles requests normally with timeout set', (t) => { |
| 125 | + t.plan(3); |
| 126 | + |
| 127 | + const server = httpServer.createServer({ |
| 128 | + root, |
| 129 | + timeout: 60 // 60 seconds |
| 130 | + }); |
| 131 | + |
| 132 | + server.listen(0, () => { |
| 133 | + const port = server.address().port; |
| 134 | + t.ok(server, 'server created with timeout option'); |
| 135 | + |
| 136 | + const req = http.get(`http://localhost:${port}/file`, (res) => { |
| 137 | + t.equal(res.statusCode, 200, 'request succeeds with timeout set'); |
| 138 | + |
| 139 | + let body = ''; |
| 140 | + res.on('data', (chunk) => { |
| 141 | + body += chunk; |
| 142 | + }); |
| 143 | + |
| 144 | + res.on('end', () => { |
| 145 | + t.ok(body.length > 0, 'response body received'); |
| 146 | + server.close(); |
| 147 | + t.end(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + req.on('error', (err) => { |
| 152 | + t.fail(`request failed: ${err.message}`); |
| 153 | + server.close(); |
| 154 | + t.end(); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +test('timeout: multiple timeout values', (t) => { |
| 160 | + t.plan(4); |
| 161 | + |
| 162 | + const testCases = [ |
| 163 | + { timeout: 30, description: '30 seconds' }, |
| 164 | + { timeout: 120, description: '120 seconds (default)' }, |
| 165 | + { timeout: 300, description: '300 seconds' }, |
| 166 | + { timeout: 0, description: 'disabled (0)' } |
| 167 | + ]; |
| 168 | + |
| 169 | + let completed = 0; |
| 170 | + const total = testCases.length; |
| 171 | + |
| 172 | + testCases.forEach((testCase) => { |
| 173 | + const server = httpServer.createServer({ |
| 174 | + root, |
| 175 | + timeout: testCase.timeout |
| 176 | + }); |
| 177 | + |
| 178 | + server.listen(0, () => { |
| 179 | + const port = server.address().port; |
| 180 | + t.ok(server, `server created with timeout ${testCase.description}`); |
| 181 | + |
| 182 | + server.close(); |
| 183 | + completed++; |
| 184 | + if (completed === total) { |
| 185 | + t.end(); |
| 186 | + } |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
| 190 | + |
0 commit comments