http-server.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * The `node:http` seam the worker's webserver boots through: no socket exists,
  3. * so `createServer` retains the request listener for the tunnel to feed and
  4. * `listen` reports success on its own.
  5. *
  6. * Two failure modes make this worth pinning rather than trusting. A `listen`
  7. * that never invokes its callback leaves the webserver fiber in LOADING with no
  8. * error anywhere, and a capture the tunnel misses leaves every synthesized
  9. * request unanswered — both look like a hang, not a fault.
  10. *
  11. * The capture is module state, so the cases below run in order: the first
  12. * observes the empty slot before anything fills it.
  13. */
  14. import { describe, expect, it } from 'vitest'
  15. import {
  16. createServer, get, request, requestListener, ServerResponse, STATUS_CODES, whenRequestListener,
  17. } from '../../src/node/builtin_modules/implemented/http.ts'
  18. import type { RequestListener } from '../../src/transport/synthetic-http.ts'
  19. const listener: RequestListener = () => {}
  20. describe('request listener capture', () => {
  21. it('keeps a caller waiting until the webserver installs its listener', async () => {
  22. expect(requestListener()).toBeUndefined()
  23. let settled = false
  24. const awaited = whenRequestListener().then((captured) => {
  25. settled = true
  26. return captured
  27. })
  28. await Promise.resolve()
  29. expect(settled).toBe(false)
  30. createServer(listener)
  31. expect(await awaited).toBe(listener)
  32. })
  33. it('answers a later caller from the capture instead of waiting again', async () => {
  34. expect(requestListener()).toBe(listener)
  35. expect(await whenRequestListener()).toBe(listener)
  36. })
  37. it('holds the capture across a server created without a listener', () => {
  38. // The tunnel reads one listener; an unrelated createServer must not blank it.
  39. createServer()
  40. expect(requestListener()).toBe(listener)
  41. })
  42. })
  43. describe('binding', () => {
  44. it('exposes the response prototype middleware probes during module loading', () => {
  45. expect(ServerResponse.prototype).not.toHaveProperty('appendHeader')
  46. })
  47. it('reports the bind through the callback the webserver fiber waits on', async () => {
  48. const server = createServer(listener)
  49. let bound = false
  50. expect(server.listen(3080, () => { bound = true })).toBe(server)
  51. await Promise.resolve()
  52. expect(bound).toBe(true)
  53. })
  54. it('reports the loopback authority the tunnel synthesizes requests against', () => {
  55. expect(createServer(listener).address()).toEqual({ address: '127.0.0.1', family: 'IPv4', port: 3080 })
  56. })
  57. it('completes close without a socket to release', async () => {
  58. const server = createServer(listener)
  59. let closed = false
  60. server.close(() => { closed = true })
  61. server.closeAllConnections()
  62. server.closeIdleConnections()
  63. await Promise.resolve()
  64. expect(closed).toBe(true)
  65. })
  66. })
  67. describe('outbound requests', () => {
  68. it('refuses, naming the carrier the worker does have', () => {
  69. expect(() => request()).toThrow(/node:http\.request is not available.*use fetch/)
  70. expect(() => get()).toThrow(/node:http\.get is not available.*use fetch/)
  71. })
  72. it('publishes the status texts a handler writes by hand', () => {
  73. expect([STATUS_CODES[200], STATUS_CODES[404], STATUS_CODES[503]]).toEqual([
  74. 'OK', 'Not Found', 'Service Unavailable',
  75. ])
  76. })
  77. })