sailfork / t3code-github / .repos/ effect/ packages/ platform/ node/ test/ NodeHttpServer.test.ts

/** @effect-diagnostics preferSchemaOverJson:skip-file */
import { NodeHttpClient, NodeHttpServer, NodeHttpServerRequest, NodeWS } from "@effect/platform-node"
import { assert, describe, expect, it } from "@effect/vitest"
import { ByteSize, Effect, Exit, Formatter, Logger, Option, References } from "effect"
import * as Deferred from "effect/Deferred"
import * as Duration from "effect/Duration"
import * as Fiber from "effect/Fiber"
import { constVoid } from "effect/Function"
import {
  Cookies,
  FetchHttpClient,
  Headers,
  HttpBody,
  HttpClient,
  HttpClientRequest,
  HttpClientResponse,
  HttpIncomingMessage,
  HttpMiddleware,
  HttpPlatform,
  HttpRouter,
  HttpServer,
  HttpServerRequest,
  HttpServerRespondable,
  HttpServerResponse,
  Multipart,
  UrlParams
} from "effect/http"
import * as HttpApiError from "effect/http-api/HttpApiError"
import * as Latch from "effect/Latch"
import * as Layer from "effect/Layer"
import * as ManagedRuntime from "effect/ManagedRuntime"
import * as IpNetwork from "effect/net/IpNetwork"
import * as NetAddress from "effect/net/NetAddress"
import * as Schema from "effect/Schema"
import * as Scope from "effect/Scope"
import { Socket } from "effect/socket"
import * as Stream from "effect/Stream"
import * as Tracer from "effect/Tracer"
import * as Buffer from "node:buffer"
import { randomBytes } from "node:crypto"
import { EventEmitter } from "node:events"
import { createReadStream, readFileSync } from "node:fs"
import * as Http from "node:http"
import * as Http2 from "node:http2"
import * as Https from "node:https"
import * as Net from "node:net"
import { Readable } from "node:stream"
import * as Tls from "node:tls"
import * as Zlib from "node:zlib"
import {
  describeHttpServerProtocol,
  http2Get,
  layerHttp2Client,
  layerTestClient
} from "../../node-shared/test/HttpServerHttp2.test-utils.ts"

const Todo = Schema.Struct({
  id: Schema.Number,
  title: Schema.String
})
const IdParams = Schema.Struct({
  id: Schema.FiniteFromString
})
const todoResponse = HttpServerResponse.schemaJson(Todo)

describe("HttpServer", () => {
  it.effect.each([8 * 1024, 512 * 1024])(
    "delivers a partial-read 413 and drains or closes for %i leftover bytes",
    (leftoverBytes) =>
      Effect.gen(function*() {
        const server = Http.createServer()
        const body = "rejected".repeat(16 * 1024)
        yield* HttpServer.serve(
          HttpServerRequest.HttpServerRequest.use((request) =>
            request.url === "/next"
              ? Effect.succeed(HttpServerResponse.text("next"))
              : request.stream.pipe(
                Stream.take(1),
                Stream.runDrain,
                Effect.as(HttpServerResponse.text(body, { status: 413 }))
              )
          )
        ).pipe(
          Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
          Layer.build
        )
        if (leftoverBytes > 256 * 1024) {
          // A raw client observes the server FIN; node:http closes its own socket on Connection: close.
          yield* Effect.callback<void, Error>((resume) => {
            const socket = Net.connect({ host: "127.0.0.1", port: tcpPort(server), allowHalfOpen: true })
            let received = ""
            let sentLeftover = false
            let fin = false
            socket.setEncoding("utf8")
            socket.on("error", (error) => resume(Effect.fail(error)))
            socket.on("connect", () => {
              socket.write(
                `POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: ${
                  leftoverBytes + 1
                }\r\nConnection: keep-alive\r\n\r\nx`
              )
            })
            socket.on("data", (chunk: string) => {
              received += chunk
              if (!sentLeftover) {
                sentLeftover = true
                // Wait for the response so the handler consumes only the first byte.
                socket.write(Buffer.Buffer.alloc(leftoverBytes))
              }
            })
            socket.on("end", () => {
              fin = true
              resume(Effect.sync(() => {
                const separator = received.indexOf("\r\n\r\n")
                assert.isAtLeast(separator, 0)
                const headers = received.slice(0, separator).toLowerCase()
                assert.match(headers, /^http\/1\.1 413 /)
                assert.include(headers, "\r\nconnection: close")
                assert.include(headers, `\r\ncontent-length: ${Buffer.Buffer.byteLength(body)}`)
                assert.strictEqual(received.slice(separator + 4), body)
              }))
            })
            socket.on("close", () => {
              if (!fin) resume(Effect.fail(new Error("Socket closed before the server FIN")))
            })
            return Effect.sync(() => socket.destroy())
          }).pipe(Effect.timeout("3 seconds"))
          return
        }
        const agent = yield* Effect.acquireRelease(
          Effect.sync(() => new Http.Agent({ keepAlive: true, maxSockets: 1 })),
          (agent) => Effect.sync(() => agent.destroy())
        )
        yield* Effect.callback<void, Error>((resume) => {
          const fail = (error: Error) => resume(Effect.fail(error))
          const request = Http.request({
            host: "127.0.0.1",
            port: tcpPort(server),
            method: "POST",
            headers: { "content-length": leftoverBytes + 1 },
            agent
          })
          let firstSocket: Net.Socket
          request.on("error", fail)
          request.on("socket", (socket) => {
            firstSocket = socket
            socket.on("error", fail)
          })
          request.on("response", (response) => {
            let received = ""
            response.setEncoding("utf8")
            response.on("data", (chunk: string) => received += chunk)
            response.on("error", fail)
            response.on("end", () => {
              const status = response.statusCode
              const next = Http.get({ host: "127.0.0.1", port: tcpPort(server), path: "/next", agent }, (response) => {
                response.on("error", fail)
                response.on("end", () => {
                  resume(Effect.sync(() => {
                    assert.strictEqual(status, 413)
                    assert.strictEqual(received, body)
                    assert.strictEqual(response.statusCode, 200)
                    // Queued requests can reuse the socket without setting ClientRequest.reusedSocket.
                    assert.strictEqual(next.socket, firstSocket)
                  }))
                })
                response.resume()
              })
              next.on("error", fail)
            })
            // The first chunk must be consumed on its own, not coalesced with the leftover body.
            request.end(Buffer.Buffer.alloc(leftoverBytes))
          })
          request.write("x")
          return Effect.sync(() => request.destroy())
        }).pipe(Effect.timeout("3 seconds"))
      })
  )

  it.effect("returns 413 before a chunked upload finishes", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/body",
        HttpServerRequest.HttpServerRequest.use((request) => Effect.as(request.text, HttpServerResponse.empty())).pipe(
          Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(4))
        )
      ).pipe(HttpRouter.serve, Layer.build)
      const server = yield* HttpServer.HttpServer
      const port = (server.address as NetAddress.InetAddress).port

      const response = yield* Effect.callback<{ status: number | undefined; uploadComplete: boolean }, Error>(
        (resume) => {
          const request = Http.request({ hostname: "127.0.0.1", port, path: "/body", method: "POST", agent: false })
          request.on("error", (error) => resume(Effect.fail(error)))
          request.on("response", (response) => {
            const result = { status: response.statusCode, uploadComplete: request.writableEnded }
            // Withhold the final chunk until the server has sent its response.
            request.end("remaining upload")
            response.on("error", (error) => resume(Effect.fail(error)))
            response.on("end", () => {
              request.destroy()
              resume(Effect.succeed(result))
            })
            response.resume()
          })
          request.write("12345")
          return Effect.sync(() => request.destroy())
        }
      )
      assert.strictEqual(response.status, 413)
      assert.isFalse(response.uploadComplete)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("returns 400 for malformed JSON within MaxBodySize", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/body",
        HttpServerRequest.HttpServerRequest.use((request) => Effect.as(request.json, HttpServerResponse.empty())).pipe(
          Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(4))
        )
      ).pipe(HttpRouter.serve, Layer.build)
      const client = yield* HttpClient.HttpClient
      const response = yield* client.post("/body", { body: HttpBody.text("{") })
      assert.strictEqual(response.status, 400)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("does not duplicate Node's automatic 100 Continue with makeHandler", () =>
    Effect.gen(function*() {
      const scope = yield* Effect.scope
      const handler = yield* NodeHttpServer.makeHandler(
        HttpServerRequest.HttpServerRequest.use((request) => request.text.pipe(Effect.map(HttpServerResponse.text))),
        { scope }
      )
      // No checkContinue listener: Node acknowledges before invoking the handler.
      const server = yield* Effect.acquireRelease(
        Effect.sync(() => Http.createServer(handler)),
        (server) =>
          Effect.callback<void>((resume) => {
            server.close(() => resume(Effect.void))
          })
      )
      yield* Effect.callback<void, Error>((resume) => {
        server.once("error", (error) => resume(Effect.fail(error)))
        server.listen(0, "127.0.0.1", () => resume(Effect.void))
      })

      const response = yield* expectContinueRequest(tcpPort(server), "hello")
      assert.strictEqual(response.status, 200)
      assert.strictEqual(response.body, "hello")
      assert.isTrue(response.uploaded)
      assert.strictEqual(response.continues, 1)
    }))

  it.effect("rejects Expect: 100-continue without requesting the body", () =>
    Effect.gen(function*() {
      const server = Http.createServer()
      yield* HttpServer.serve(
        HttpServerRequest.HttpServerRequest.use((request) => {
          // Obtaining a body accessor must not start reading the body.
          void request.stream
          void request.text
          void request.multipart
          void request.multipartStream
          return Effect.succeed(HttpServerResponse.empty({ status: 413 }))
        })
      ).pipe(
        Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
        Layer.build
      )

      const response = yield* expectContinueRequest(tcpPort(server), "hello")
      assert.strictEqual(response.status, 413)
      assert.strictEqual(response.continues, 0)
      assert.isFalse(response.uploaded)
    }))

  it.effect.each(["stream", "text", "json", "urlParamsBody", "arrayBuffer"] as const)(
    "sends one 100 Continue when consuming %s",
    (accessor) =>
      Effect.gen(function*() {
        const server = Http.createServer()
        yield* HttpServer.serve(
          HttpServerRequest.HttpServerRequest.use((original) => {
            const request = original.modify({ url: "/body" })
            const body = accessor === "stream"
              ? request.stream.pipe(Stream.decodeText(), Stream.mkString)
              : accessor === "arrayBuffer"
              ? request.arrayBuffer.pipe(Effect.map((buffer) => new TextDecoder().decode(buffer)))
              : accessor === "json"
              ? request.json.pipe(Effect.map((value) => String(value)))
              : accessor === "urlParamsBody"
              ? request.urlParamsBody.pipe(Effect.map(UrlParams.toString))
              : request.text
            return body.pipe(Effect.map(HttpServerResponse.text))
          })
        ).pipe(
          Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
          Layer.build
        )

        const payload = accessor === "json" ? "\"hello\"" : accessor === "urlParamsBody" ? "hello=world" : "hello"
        const response = yield* expectContinueRequest(tcpPort(server), payload)
        assert.strictEqual(response.status, 200)
        assert.strictEqual(response.body, accessor === "urlParamsBody" ? "hello=world" : "hello")
        assert.strictEqual(response.continues, 1)
        assert.isTrue(response.uploaded)
      })
  )

  it.effect.each(["multipart", "multipartStream"] as const)(
    "sends one 100 Continue when consuming %s",
    (accessor) =>
      Effect.gen(function*() {
        const server = Http.createServer()
        yield* HttpServer.serve(
          HttpServerRequest.HttpServerRequest.use((original) => {
            const request = original.modify({ url: "/upload" })
            const field = accessor === "multipart"
              ? request.multipart.pipe(Effect.map((parts) => parts.message))
              : request.multipartStream.pipe(
                Stream.runCollect,
                Effect.map((parts) => parts.map((part) => Multipart.isField(part) ? part.value : "file").join(""))
              )
            return field.pipe(Effect.map((value) => HttpServerResponse.text(String(value))))
          })
        ).pipe(
          Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
          Layer.build
        )

        const payload =
          "--boundary\r\nContent-Disposition: form-data; name=\"message\"\r\n\r\nhello\r\n--boundary--\r\n"
        const response = yield* expectContinueRequest(tcpPort(server), payload, {
          "content-type": "multipart/form-data; boundary=boundary"
        })
        assert.strictEqual(response.status, 200)
        assert.strictEqual(response.body, "hello")
        assert.strictEqual(response.continues, 1)
        assert.isTrue(response.uploaded)
      })
  )

  it.effect("sends one 100 Continue across repeated original and modified body reads", () =>
    Effect.gen(function*() {
      const server = Http.createServer()
      yield* HttpServer.serve(
        HttpServerRequest.HttpServerRequest.use((original) =>
          Effect.gen(function*() {
            const modified = original.modify({ url: "/body" })
            // Start both readers before the client uploads, then read their cached bodies again.
            const bodies = yield* Effect.all([original.text, modified.text], { concurrency: "unbounded" })
            const repeated = yield* Effect.all([original.text, modified.text])
            return HttpServerResponse.text([...bodies, ...repeated].join(","))
          })
        )
      ).pipe(
        Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
        Layer.build
      )

      const response = yield* expectContinueRequest(tcpPort(server), "hello")
      assert.strictEqual(response.status, 200)
      assert.strictEqual(response.body, "hello,hello,hello,hello")
      assert.strictEqual(response.continues, 1)
      assert.isTrue(response.uploaded)
    }))

  it.effect("keeps routes isolated between independent servers", () =>
    Effect.gen(function*() {
      const publicServer = Http.createServer()
      const internalServer = Http.createServer()

      const Health = Layer.effectDiscard(
        Effect.flatMap(HttpRouter.HttpRouter, (router) =>
          router.add("GET", "/health", HttpServerResponse.text("healthy")))
      )
      const publicApp = Layer.mergeAll(
        HttpRouter.add("GET", "/public", HttpServerResponse.text("public")),
        Health
      )
      const internalApp = Layer.mergeAll(
        HttpRouter.add("GET", "/internal", HttpServerResponse.text("internal")),
        Health
      )

      yield* Layer.mergeAll(
        HttpRouter.serve(publicApp, { disableListenLog: true, disableLogger: true }).pipe(
          Layer.provide(NodeHttpServer.layer(() =>
            publicServer, { port: 0 }))
        ),
        HttpRouter.serve(internalApp, { disableListenLog: true, disableLogger: true }).pipe(
          Layer.provide(NodeHttpServer.layer(() => internalServer, { port: 0 }))
        )
      ).pipe(Layer.build)

      const status = (port: number, path: string) =>
        Effect.promise(() => fetch("http://localhost:" + port + path).then((response) => response.status))

      assert.strictEqual(yield* status(tcpPort(publicServer), "/public"), 200)
      assert.strictEqual(yield* status(tcpPort(internalServer), "/internal"), 200)
      assert.strictEqual(yield* status(tcpPort(publicServer), "/health"), 200)
      assert.strictEqual(yield* status(tcpPort(internalServer), "/health"), 200)
      assert.strictEqual(yield* status(tcpPort(publicServer), "/internal"), 404)
      assert.strictEqual(yield* status(tcpPort(internalServer), "/public"), 404)
    }))

  it.effect("takes the host header from the HTTP/2 authority", () =>
    Effect.gen(function*() {
      const server = Http2.createServer()
      yield* HttpServer.serve(
        HttpServerRequest.HttpServerRequest.use((request) =>
          Effect.succeed(HttpServerResponse.text(
            `${request.headers.host} ${Option.getOrThrow(HttpServerRequest.toURL(request)).href}`
          ))
        )
      ).pipe(
        Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
        Layer.build
      )
      const port = tcpPort(server)

      for (const host of [undefined, "client.test"]) {
        assert.strictEqual(
          yield* http2Get(port, { ":path": "/info", ":authority": "example.test:4573", host }),
          "example.test:4573 http://example.test:4573/info"
        )
      }
    }))

  it.live("acknowledges Expect: 100-continue over HTTP/2", () =>
    Effect.gen(function*() {
      const server = Http2.createServer()
      yield* HttpServer.serve(
        HttpServerRequest.HttpServerRequest.use((request) => Effect.map(request.text, HttpServerResponse.text))
      ).pipe(
        Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
        Layer.build
      )
      const session = yield* Effect.acquireRelease(
        Effect.sync(() => Http2.connect(`http://127.0.0.1:${tcpPort(server)}`)),
        (session) => Effect.sync(() => session.destroy())
      )
      const body = yield* Effect.callback<string, Error>((resume) => {
        const stream = session.request({ ":method": "POST", ":path": "/", expect: "100-continue" })
        let body = ""
        stream.setEncoding("utf8")
        stream.on("continue", () => stream.end("hello"))
        stream.on("data", (chunk: string) => {
          body += chunk
        })
        stream.on("end", () => resume(Effect.succeed(body)))
        stream.on("error", (error) => resume(Effect.fail(error)))
      }).pipe(Effect.timeout("2 seconds"))
      assert.strictEqual(body, "hello")
    }))

  it.effect("schema", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/todos/:id",
        Effect.flatMap(
          HttpRouter.schemaParams(IdParams),
          ({ id }) => todoResponse({ id, title: "test" })
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const todo = yield* HttpClient.get("/todos/1").pipe(
        Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo))
      )
      expect(todo).toEqual({ id: 1, title: "test" })
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("toServerResponse works behind the decompression middleware", () =>
    Effect.gen(function*() {
      yield* HttpServer.serveEffect(
        HttpServerRequest.HttpServerRequest.use((request) => {
          const responses = [request, request.modify({ url: "/modified" })].map(NodeHttpServerRequest.toServerResponse)
          return Effect.succeed(
            HttpServerResponse.text(responses.map((response) => response instanceof Http.ServerResponse).join(","))
          )
        }),
        HttpMiddleware.decompression()
      )
      const response = yield* HttpClient.post("/", {
        body: HttpBody.uint8Array(Zlib.gzipSync("hello")),
        headers: { "content-encoding": "gzip" }
      })
      assert.strictEqual(yield* response.text, "true,true")
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("redacts query params when inspecting requests", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/inspect",
        Effect.gen(function*() {
          const request = yield* HttpServerRequest.HttpServerRequest
          return yield* HttpServerResponse.json({
            url: request.url,
            json: JSON.stringify(request),
            formatted: Formatter.format(request)
          })
        }).pipe(Effect.provideService(UrlParams.CurrentRedactedNames, ["token"]))
      ).pipe(HttpRouter.serve, Layer.build)

      const result = (yield* HttpClient.get("/inspect?token=token-secret&sig=sig-visible").pipe(
        Effect.flatMap((response) => response.json)
      )) as { readonly url: string; readonly json: string; readonly formatted: string }

      assert.strictEqual(result.url, "/inspect?token=token-secret&sig=sig-visible")
      assert.match(result.json, /"url":"http:\/\/[^/"]+\/inspect\?token=REDACTED&sig=sig-visible"/)
      assert.include(result.formatted, "/inspect?token=REDACTED&sig=sig-visible")
      assert.notInclude(result.formatted, "token-secret")
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("redacts configured headers when inspecting requests", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/inspect",
        Effect.gen(function*() {
          const request = yield* HttpServerRequest.HttpServerRequest
          return yield* HttpServerResponse.json({
            json: JSON.stringify(request.headers),
            formatted: Formatter.format(request.headers)
          })
        }).pipe(Effect.provideService(Headers.CurrentRedactedNames, ["x-secret"]))
      ).pipe(HttpRouter.serve, Layer.build)

      const result = (yield* HttpClient.get("/inspect", { headers: { "x-secret": "header-secret" } }).pipe(
        Effect.flatMap((response) => response.json)
      )) as { readonly json: string; readonly formatted: string }

      assert.include(result.json, `"x-secret":"<redacted>"`)
      assert.notInclude(result.json, "header-secret")
      assert.notInclude(result.formatted, "header-secret")
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("schemaJson applies a JSON reviver", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/json",
        Effect.gen(function*() {
          const decoded = yield* HttpRouter.schemaJson(
            Schema.Struct({ body: Schema.Struct({ value: Schema.String }) }),
            { reviver: (key, value) => key === "value" ? "revived" : value }
          )
          assert.deepStrictEqual(decoded, { body: { value: "revived" } })
          return HttpServerResponse.empty()
        })
      ).pipe(HttpRouter.serve, Layer.build)

      const response = yield* HttpClient.post("/json", {
        body: HttpBody.jsonUnsafe({ value: "original" })
      })
      assert.strictEqual(response.status, 204)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect.each(["Uploaded", ""] as const)("forwards status text %j", (statusText) =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/",
        HttpServerResponse.empty({ status: 201, statusText })
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const server = yield* HttpServer.HttpServer
      assert.isTrue(NetAddress.isInetAddress(server.address))
      if (!NetAddress.isInetAddress(server.address)) return
      const port = server.address.port

      assert.strictEqual(yield* getStatusText(port), statusText)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("formData", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/upload",
        Effect.gen(function*() {
          const request = yield* HttpServerRequest.HttpServerRequest
          const formData = yield* request.multipart
          const part = formData.file
          assert(typeof part !== "string")
          const file = part[0]
          assert(typeof file !== "string")
          expect(file.path.endsWith("/test.txt")).toEqual(true)
          expect(file.contentType).toEqual("text/plain")
          return yield* HttpServerResponse.json({ ok: "file" in formData })
        })
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const formData = new FormData()
      formData.append("file", new Blob(["test"], { type: "text/plain" }), "test.txt")
      const result = yield* client.post("/upload", { body: HttpBody.formData(formData) }).pipe(
        Effect.flatMap((r) => r.json)
      )
      expect(result).toEqual({ ok: true })
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("schemaBodyForm", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/upload",
        Effect.gen(function*() {
          const files = yield* HttpServerRequest.schemaBodyForm(Schema.Struct({
            file: Multipart.FilesSchema,
            test: Schema.String
          }))
          expect(files).toHaveProperty("file")
          expect(files).toHaveProperty("test")
          return HttpServerResponse.empty()
        })
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const formData = new FormData()
      formData.append("file", new Blob(["test"], { type: "text/plain" }), "test.txt")
      formData.append("test", "test")
      const response = yield* client.post("/upload", { body: HttpBody.formData(formData) })
      expect(response.status).toEqual(204)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("formData withMaxFileSize", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/upload",
        Effect.gen(function*() {
          const request = yield* HttpServerRequest.HttpServerRequest
          yield* request.multipart
          return HttpServerResponse.empty()
        }).pipe(
          Effect.catchTag("MultipartError", (error) =>
            error.reason._tag === "FileTooLarge" ?
              Effect.succeed(HttpServerResponse.empty({ status: 413 })) :
              Effect.fail(error))
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build,
        Effect.provideService(Multipart.MaxFileSize, ByteSize.bytes(100))
      )
      const client = yield* HttpClient.HttpClient
      const formData = new FormData()
      const data = new Uint8Array(1000)
      formData.append("file", new Blob([data], { type: "text/plain" }), "test.txt")
      const response = yield* client.post("/upload", { body: HttpBody.formData(formData) })
      expect(response.status).toEqual(413)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("formData withMaxFieldSize", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/upload",
        Effect.gen(function*() {
          const request = yield* HttpServerRequest.HttpServerRequest
          yield* request.multipart
          return HttpServerResponse.empty()
        }).pipe(
          Effect.catchTag("MultipartError", (error) =>
            error.reason._tag === "FieldTooLarge" ?
              Effect.succeed(HttpServerResponse.empty({ status: 413 })) :
              Effect.fail(error))
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build,
        Effect.provideService(Multipart.MaxFieldSize, ByteSize.bytes(100))
      )
      const client = yield* HttpClient.HttpClient
      const formData = new FormData()
      const data = new Uint8Array(1000).fill(1)
      formData.append("file", new TextDecoder().decode(data))
      const response = yield* client.post("/upload", { body: HttpBody.formData(formData) })
      expect(response.status).toEqual(413)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("mountApp", () =>
    Effect.gen(function*() {
      const child = Effect.map(HttpServerRequest.HttpServerRequest, (_) => HttpServerResponse.text(_.url))
      yield* HttpRouter.use((router) => router.prefixed("/child").add("*", "*", child)).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const todo = yield* client.get("/child/1").pipe(Effect.flatMap((_) => _.text))
      expect(todo).toEqual("/1")
      const root = yield* client.get("/child").pipe(Effect.flatMap((_) => _.text))
      expect(root).toEqual("/")
      const rootSearch = yield* client.get("/child?foo=bar").pipe(Effect.flatMap((_) => _.text))
      expect(rootSearch).toEqual("/?foo=bar")
      const rootSlash = yield* client.get("/child/").pipe(Effect.flatMap((_) => _.text))
      expect(rootSlash).toEqual("/")
      const invalid = yield* client.get("/child1/", {
        urlParams: { foo: "bar" }
      }).pipe(Effect.map((_) => _.status))
      expect(invalid).toEqual(404)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("file", () =>
    Effect.gen(function*() {
      yield* (yield* HttpServerResponse.file(`${__dirname}/fixtures/text.txt`).pipe(
        Effect.updateService(
          HttpPlatform.HttpPlatform,
          (_) => ({
            ..._,
            fileResponse: (path, options) =>
              Effect.map(
                _.fileResponse(path, options),
                (res) => {
                  ;(res as any).headers.etag = "\"etag\""
                  return res
                }
              )
          })
        )
      )).pipe(
        Effect.succeed,
        HttpServer.serveEffect()
      )
      const client = yield* HttpClient.HttpClient
      const res = yield* client.get("/")
      expect(res.status).toEqual(200)
      expect(res.headers["content-type"]).toEqual("text/plain")
      expect(res.headers["content-length"]).toEqual("27")
      expect(res.headers.etag).toEqual("\"etag\"")
      const text = yield* res.text
      expect(text.trim()).toEqual("lorem ipsum dolar sit amet")
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("fileWeb", () =>
    Effect.gen(function*() {
      const now = new Date()
      const file = new Buffer.File([new TextEncoder().encode("test")], "test.txt", {
        type: "text/plain",
        lastModified: now.getTime()
      })
      yield* HttpServerResponse.fileWeb(file).pipe(
        Effect.updateService(
          HttpPlatform.HttpPlatform,
          (_) => ({
            ..._,
            fileWebResponse: (path, options) =>
              Effect.map(
                _.fileWebResponse(path, options),
                (res) => ({ ...res, headers: { ...res.headers, etag: "W/\"etag\"" } })
              )
          })
        ),
        HttpServer.serveEffect()
      )
      const client = yield* HttpClient.HttpClient
      const res = yield* client.get("/")
      expect(res.status).toEqual(200)
      expect(res.headers["content-type"]).toEqual("text/plain")
      expect(res.headers["content-length"]).toEqual("4")
      expect(res.headers["last-modified"]).toEqual(now.toUTCString())
      expect(res.headers.etag).toEqual("W/\"etag\"")
      const text = yield* res.text
      expect(text.trim()).toEqual("test")
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("schemaBodyUrlParams", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/todos",
        Effect.flatMap(
          HttpServerRequest.schemaBodyUrlParams(Schema.Struct({
            id: Schema.FiniteFromString,
            title: Schema.String
          })),
          ({ id, title }) => todoResponse({ id, title })
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const todo = yield* HttpClientRequest.post("/todos").pipe(
        HttpClientRequest.bodyUrlParams({ id: "1", title: "test" }),
        HttpClient.execute,
        Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo))
      )
      expect(todo).toEqual({ id: 1, title: "test" })
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("schemaBodyUrlParams error", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/todos",
        Effect.flatMap(
          HttpServerRequest.schemaBodyUrlParams(Schema.Struct({
            id: Schema.FiniteFromString,
            title: Schema.String
          })),
          ({ id, title }) => todoResponse({ id, title })
        ).pipe(
          Effect.catchTag("SchemaError", (error) =>
            Effect.succeed(HttpServerResponse.jsonUnsafe({ error }, { status: 400 })))
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const response = yield* client.get("/todos")
      expect(response.status).toEqual(400)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("schemaBodyFormJson", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/upload",
        Effect.gen(function*() {
          const result = yield* HttpServerRequest.schemaBodyFormJson(
            Schema.Struct({
              test: Schema.String
            }),
            { reviver: (key, value) => key === "test" ? "revived" : value }
          )("json")
          expect(result.test).toEqual("revived")
          return HttpServerResponse.empty()
        })
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const formData = new FormData()
      formData.append("json", JSON.stringify({ test: "content" }))
      const response = yield* client.post("/upload", { body: HttpBody.formData(formData) })
      expect(response.status).toEqual(204)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("schemaBodyFormJson file", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/upload",
        Effect.gen(function*() {
          const result = yield* HttpServerRequest.schemaBodyFormJson(Schema.Struct({
            test: Schema.String
          }))("json")

          expect(result.test).toEqual("content")
          return HttpServerResponse.empty()
        })
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const formData = new FormData()
      formData.append(
        "json",
        new Blob([JSON.stringify({ test: "content" })], { type: "application/json" }),
        "test.json"
      )
      const response = yield* client.post("/upload", { body: HttpBody.formData(formData) })
      expect(response.status).toEqual(204)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("schemaBodyFormJson url encoded", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "POST",
        "/upload",
        Effect.gen(function*() {
          const result = yield* HttpServerRequest.schemaBodyFormJson(Schema.Struct({
            test: Schema.String
          }))("json")
          expect(result.test).toEqual("content")
          return HttpServerResponse.empty()
        })
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const response = yield* client.post("/upload", {
        body: HttpBody.urlParams(UrlParams.fromInput({
          json: JSON.stringify({ test: "content" })
        }))
      })
      expect(response.status).toEqual(204)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("tracing", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/",
        Effect.flatMap(
          Effect.currentSpan,
          (_) => HttpServerResponse.json({ spanId: _.spanId, parent: _.parent })
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const requestSpan = yield* Effect.makeSpan("client request")
      const body = yield* client.get("/").pipe(
        Effect.flatMap((r) => r.json),
        Effect.provideService(
          Tracer.Tracer,
          Tracer.make({
            span(options) {
              assert.strictEqual(options.name, "GET")
              assert.strictEqual(options.kind, "client")
              assert(options.parent._tag === "Some")
              if (options.parent.value._tag !== "Span") {
                throw new Error("Expected span parent")
              }
              assert.strictEqual(options.parent.value.name, "request parent")
              return requestSpan
            }
          })
        ),
        Effect.withSpan("request parent"),
        Effect.repeat({ times: 2 })
      )
      expect((body as any).parent._tag).toEqual("Some")
      expect((body as any).parent.value.spanId).toEqual(requestSpan.spanId)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("html", () =>
    Effect.gen(function*() {
      yield* HttpRouter.addAll([
        HttpRouter.route("GET", "/home", HttpServerResponse.html("<html />")),
        HttpRouter.route(
          "GET",
          "/about",
          HttpServerResponse.html`<html>${Effect.succeed("<body />")}</html>`
        ),
        HttpRouter.route(
          "GET",
          "/stream",
          HttpServerResponse.htmlStream`<html>${Stream.make("<body />", 123, "hello")}</html>`
        )
      ]).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const home = yield* client.get("/home").pipe(Effect.flatMap((r) => r.text))
      expect(home).toEqual("<html />")
      const about = yield* client.get("/about").pipe(Effect.flatMap((r) => r.text))
      expect(about).toEqual("<html><body /></html>")
      const stream = yield* client.get("/stream").pipe(Effect.flatMap((r) => r.text))
      expect(stream).toEqual("<html><body />123hello</html>")
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("setCookie", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/home",
        HttpServerResponse.empty().pipe(
          HttpServerResponse.setCookieUnsafe("test", "value"),
          HttpServerResponse.setCookieUnsafe("test2", "value2", {
            httpOnly: true,
            secure: true,
            sameSite: "lax",
            partitioned: true,
            path: "/",
            domain: "example.com",
            expires: new Date(2022, 1, 1, 0, 0, 0, 0),
            maxAge: "5 minutes"
          })
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const res = yield* client.get("/home")
      assert.deepStrictEqual(
        res.cookies.toJSON(),
        Cookies.fromReadonlyRecord({
          test: Cookies.makeCookieUnsafe("test", "value"),
          test2: Cookies.makeCookieUnsafe("test2", "value2", {
            httpOnly: true,
            secure: true,
            sameSite: "lax",
            partitioned: true,
            path: "/",
            domain: "example.com",
            expires: new Date(2022, 1, 1, 0, 0, 0, 0),
            maxAge: Duration.minutes(5)
          })
        }).toJSON()
      )
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.live("uninterruptible routes", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/home",
        Effect.gen(function*() {
          const fiber = Fiber.getCurrent()!
          setTimeout(() => fiber.interruptUnsafe(fiber.id), 10)
          yield* Effect.sleep(50)
          return HttpServerResponse.empty()
        }),
        { uninterruptible: true }
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const client = yield* HttpClient.HttpClient
      const res = yield* client.get("/home")
      assert.strictEqual(res.status, 204)
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("completes a HEAD response once when close precedes the end callback", () =>
    Effect.gen(function*() {
      const scope = yield* Effect.scope
      const handler = yield* NodeHttpServer.makeHandler(
        Effect.succeed(HttpServerResponse.empty()),
        { scope }
      )
      const completed = Latch.makeUnsafe()
      let writableEnded = false
      const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", {
        get: () => writableEnded
      }) as Http.ServerResponse
      let closeListenerRemovals = 0
      nodeResponse.writeHead = () => nodeResponse
      nodeResponse.off = ((event: string | symbol, listener: (...args: Array<unknown>) => void) => {
        if (event === "close") {
          closeListenerRemovals++
        }
        return EventEmitter.prototype.off.call(nodeResponse, event, listener) as Http.ServerResponse
      }) as Http.ServerResponse["off"]
      nodeResponse.end = ((callback: () => void) => {
        writableEnded = true
        nodeResponse.emit("close")
        callback()
        completed.openUnsafe()
        return nodeResponse
      }) as Http.ServerResponse["end"]

      handler(
        { method: "HEAD", url: "/", headers: {}, socket: {} } as Http.IncomingMessage,
        nodeResponse
      )
      yield* completed.await

      assert.strictEqual(closeListenerRemovals, 1)
    }))

  it.live.each([
    { body: "Uint8Array", event: "close", response: () => HttpServerResponse.uint8Array(new Uint8Array(1024)) },
    { body: "Raw", event: "aborted", response: () => HttpServerResponse.raw(new Uint8Array(1024)) }
  ])("completes a $body response sent on $event after a disconnect", ({ event, response }) =>
    Effect.gen(function*() {
      const server = Http.createServer()
      const scope = yield* Scope.make()
      // A stuck uninterruptible request fiber would block the server's shutdown
      yield* Effect.addFinalizer(() => Effect.forkDetach(Scope.close(scope, Exit.void)))
      const received = yield* Latch.make()
      const sent = yield* Deferred.make<HttpServerResponse.HttpServerResponse, unknown>()
      yield* HttpRouter.add(
        "GET",
        "/",
        Effect.gen(function*() {
          const incoming = NodeHttpServerRequest.toIncomingMessage(yield* HttpServerRequest.HttpServerRequest)
          yield* received.open
          // Node emits the request's `aborted` before it marks the response destroyed
          yield* Effect.callback<void>((resume) => {
            if (incoming.socket.destroyed) return resume(Effect.void)
            ;(event === "aborted" ? incoming : incoming.socket).once(event, () => resume(Effect.void))
          })
          return response()
        }),
        { uninterruptible: true }
      ).pipe(
        (app) =>
          HttpRouter.serve(app, { middleware: (app) => Effect.onExit(app, (exit) => Deferred.done(sent, exit)) }),
        Layer.provide(NodeHttpServer.layer(() => server, { port: 0 })),
        Layer.buildWithScope(scope)
      )
      const socket = Net.connect({ port: tcpPort(server), host: "127.0.0.1" }, () => {
        socket.write("GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
      })
      socket.on("error", constVoid)
      yield* received.await
      socket.destroy()
      const res = yield* Deferred.await(sent).pipe(Effect.timeout("2 seconds"))
      assert.strictEqual(res.status, 200)
    }))

  it.effect("returns none when a late remoteAddress read finds no socket", () =>
    Effect.gen(function*() {
      const scope = yield* Effect.scope
      let request: HttpServerRequest.HttpServerRequest | undefined
      const handler = yield* NodeHttpServer.makeHandler(
        Effect.gen(function*() {
          request = yield* HttpServerRequest.HttpServerRequest
          return HttpServerResponse.empty()
        }),
        { scope }
      )
      const completed = Latch.makeUnsafe()
      let writableEnded = false
      const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", {
        get: () => writableEnded
      }) as Http.ServerResponse
      nodeResponse.writeHead = () => nodeResponse
      nodeResponse.end = (() => {
        writableEnded = true
        completed.openUnsafe()
        return nodeResponse
      }) as Http.ServerResponse["end"]
      const nodeRequest = {
        method: "GET",
        url: "/",
        headers: {},
        socket: { remoteAddress: "127.0.0.1" }
      } as unknown as Http.IncomingMessage

      handler(nodeRequest, nodeResponse)
      yield* completed.await
      assert(request !== undefined)
      ;(nodeRequest as unknown as { socket: null }).socket = null

      assert.deepStrictEqual(request.remoteAddress, Option.none())
    }))

  it.effect("coalesces streaming chunks from the same pull", () =>
    Effect.gen(function*() {
      const scope = yield* Effect.scope
      const handler = yield* NodeHttpServer.makeHandler(
        Effect.succeed(HttpServerResponse.stream(Stream.make(
          Buffer.Buffer.from("a"),
          Buffer.Buffer.from("b")
        ))),
        { scope }
      )
      const completed = Latch.makeUnsafe()
      const writes: Array<Uint8Array> = []
      let writableEnded = false
      const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", {
        get: () => writableEnded
      }) as Http.ServerResponse
      nodeResponse.writeHead = () => nodeResponse
      nodeResponse.write = ((chunk: Uint8Array) => {
        writes.push(chunk)
        return true
      }) as Http.ServerResponse["write"]
      nodeResponse.end = (() => {
        writableEnded = true
        completed.openUnsafe()
        return nodeResponse
      }) as Http.ServerResponse["end"]

      handler(
        { method: "GET", url: "/", headers: {}, socket: {} } as Http.IncomingMessage,
        nodeResponse
      )
      yield* completed.await

      assert.deepStrictEqual(writes.map((chunk) => Buffer.Buffer.from(chunk).toString()), ["ab"])
    }))

  it.effect("waits for drain after a streaming write applies backpressure", () =>
    Effect.gen(function*() {
      const scope = yield* Effect.scope
      const handler = yield* NodeHttpServer.makeHandler(
        Effect.succeed(HttpServerResponse.stream(Stream.make(
          Buffer.Buffer.from("a"),
          Buffer.Buffer.from("b")
        ))),
        { scope }
      )
      const writeObserved = Latch.makeUnsafe()
      const completed = Latch.makeUnsafe()
      let writableEnded = false
      const nodeResponse = Object.defineProperty(new EventEmitter(), "writableEnded", {
        get: () => writableEnded
      }) as Http.ServerResponse
      let writeCount = 0
      nodeResponse.writeHead = () => nodeResponse
      nodeResponse.write = (() => {
        writeCount++
        queueMicrotask(() => writeObserved.openUnsafe())
        return writeCount > 1
      }) as Http.ServerResponse["write"]
      nodeResponse.end = (() => {
        writableEnded = true
        completed.openUnsafe()
        return nodeResponse
      }) as Http.ServerResponse["end"]

      handler(
        { method: "GET", url: "/", headers: {}, socket: {} } as Http.IncomingMessage,
        nodeResponse
      )
      yield* writeObserved.await
      assert.strictEqual(nodeResponse.writableEnded, false)

      nodeResponse.emit("drain")
      yield* completed.await
      assert.strictEqual(writeCount, 1)
    }))

  it.live.each([
    { method: "GET", status: 204 },
    { method: "GET", status: 205 },
    { method: "GET", status: 304 },
    { method: "HEAD", status: 204 },
    { method: "HEAD", status: 205 }
  ])("sends no body for $method with status $status", ({ method, status }) =>
    Effect.gen(function*() {
      yield* HttpServer.serveEffect(Effect.succeed(HttpServerResponse.text("x", { status })))
      const server = yield* HttpServer.HttpServer
      const received = yield* rawGet((server.address as NetAddress.InetAddress).port, ["/"], method)
      assert.match(received, new RegExp(`^HTTP/1.1 ${status} `))
      assert.isTrue(received.endsWith("\r\n\r\n"))
      // 304 describes the representation, 204 and 205 have no content.
      if (status === 304) {
        assert.match(received, /\r\ncontent-length: 1\r\n/i)
      } else {
        assert.notMatch(received, /\r\ncontent-length:/i)
      }
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.live.each([
    {
      body: "Uint8Array",
      contentLength: 3,
      response: () => HttpServerResponse.text("hello").pipe(HttpServerResponse.setHeader("content-length", "3"))
    },
    {
      body: "Raw file Readable",
      contentLength: 3,
      response: () =>
        HttpServerResponse.raw(createReadStream(new URL("./fixtures/text.txt", import.meta.url)), { contentLength: 3 })
    },
    {
      body: "FormData",
      contentLength: 3,
      response: () => {
        const formData = new FormData()
        formData.append("text", "hello")
        return HttpServerResponse.formData(formData, { headers: { "content-length": "3" } })
      }
    },
    {
      body: "Stream",
      contentLength: 10,
      response: () => HttpServerResponse.stream(Stream.make(Buffer.Buffer.from("hello")), { contentLength: 10 })
    }
  ])(
    "aborts the connection when a $body body doesn't match content-length",
    ({ contentLength, response }) =>
      Effect.gen(function*() {
        yield* Layer.mergeAll(
          HttpRouter.add("GET", "/mismatch", Effect.sync(response)),
          HttpRouter.add("GET", "/next", HttpServerResponse.text("next"))
        ).pipe(
          HttpRouter.serve,
          Layer.build
        )
        const server = yield* HttpServer.HttpServer
        const received = yield* rawGet((server.address as NetAddress.InetAddress).port, ["/mismatch", "/next"])
        // The client must neither wait for the declared body nor read the next response as part of it.
        const headEnd = received.indexOf("\r\n\r\n")
        const body = headEnd === -1 ? "" : received.slice(headEnd + 4)
        assert.isBelow(body.length, contentLength)
      }).pipe(Effect.provide(NodeHttpServer.layerTest))
  )

  it.effect("aborts the response when a streamed body fails after it started", () =>
    Effect.gen(function*() {
      const received = yield* Latch.make()
      yield* HttpRouter.add(
        "GET",
        "/stream",
        HttpServerResponse.stream(
          Stream.make(Buffer.Buffer.from("partial")).pipe(
            Stream.concat(Stream.fromEffect(received.await.pipe(Effect.andThen(Effect.fail("boom")))))
          )
        )
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      const response = yield* HttpClient.get("/stream")
      assert.strictEqual(response.status, 200)
      let body = ""
      const exit = yield* response.stream.pipe(
        Stream.runForEach((chunk) => {
          body += Buffer.Buffer.from(chunk).toString()
          return received.open
        }),
        Effect.exit
      )
      assert.strictEqual(body, "partial")
      assert.isTrue(Exit.isFailure(exit))
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  describe("when writeHead throws", () => {
    it.live("answers rejected HTTP/1.1 headers with a 500 and reports the failure", () =>
      Effect.gen(function*() {
        const observed = yield* Deferred.make<Exit.Exit<unknown, unknown>>()
        yield* HttpServer.serve(
          Effect.succeed(HttpServerResponse.formData(new FormData(), { headers: { "x-invalid": "a\nb" } })),
          (app) => Effect.onExit(app, (exit) => Deferred.succeed(observed, exit))
        ).pipe(Layer.build)

        const response = yield* HttpClient.get("/")
        assert.strictEqual(response.status, 500)
        assert.strictEqual(yield* response.text, "")
        assert.isTrue(Exit.isFailure(yield* Deferred.await(observed)))
      }).pipe(Effect.timeout("2 seconds"), Effect.provide(NodeHttpServer.layerTest)))

    it.live.each(
      [
        { method: "GET", status: 103 },
        { method: "GET", status: 600 },
        { method: "HEAD", status: 600 }
      ] as const
    )(
      "answers a $method response with status $status with a 500 and destroys the raw body",
      ({ method, status }) =>
        Effect.gen(function*() {
          const body = Readable.from(["ok"])
          const observed = yield* Deferred.make<Exit.Exit<unknown, unknown>>()
          yield* HttpServer.serve(
            Effect.succeed(HttpServerResponse.raw(body, { status })),
            (app) => Effect.onExit(app, (exit) => Deferred.succeed(observed, exit))
          ).pipe(Layer.build)

          const response = yield* HttpClient.execute(HttpClientRequest.make(method)("/"))
          assert.strictEqual(response.status, 500)
          assert.isTrue(Exit.isFailure(yield* Deferred.await(observed)))
          assert.isTrue(body.destroyed)
        }).pipe(Effect.timeout("2 seconds"), Effect.provide(NodeHttpServer.layerTest))
    )
  })

  it.live("answers keep-alive requests while graceful shutdown drains an existing connection", () => {
    const requestStarted = Latch.makeUnsafe()
    const releaseResponse = Latch.makeUnsafe()
    const firstResponse = Latch.makeUnsafe()
    const secondResponse = Latch.makeUnsafe()
    const server = Http.createServer()
    server.keepAliveTimeout = 5000
    const runtime = ManagedRuntime.make(
      HttpServer.serve(Effect.gen(function*() {
        yield* requestStarted.open
        yield* releaseResponse.await
        return HttpServerResponse.text("ok")
      })).pipe(
        Layer.provide(NodeHttpServer.layer(() => server, {
          host: "127.0.0.1",
          port: 0,
          gracefulShutdownTimeout: "10 seconds"
        }))
      )
    )
    let socket: Net.Socket | undefined
    let responseData = ""

    return Effect.gen(function*() {
      yield* Effect.promise(() => runtime.context())
      const connection = Net.connect(tcpPort(server), "127.0.0.1")
      socket = connection
      connection.on("data", (chunk) => {
        responseData += chunk.toString()
        if (responseData.includes("\r\n\r\nok")) firstResponse.openUnsafe()
        if ((responseData.match(/HTTP\/1\.1 \d{3}/g) ?? []).length >= 2) secondResponse.openUnsafe()
      })
      yield* Effect.callback<void, Error>((resume) => {
        connection.once("connect", () => resume(Effect.void))
        connection.on("error", (error) => resume(Effect.fail(error)))
      }).pipe(Effect.timeout("2 seconds"))

      connection.write("GET /first HTTP/1.1\r\nHost: localhost\r\n\r\n")
      yield* requestStarted.await.pipe(Effect.timeout("2 seconds"))
      const disposed = runtime.dispose()
      // Wait for server.close() to stop listening, without letting the first request finish.
      yield* Effect.callback<void>((resume) => {
        const timer = setInterval(() => {
          if (!server.listening) resume(Effect.void)
        }, 1)
        return Effect.sync(() => clearInterval(timer))
      }).pipe(Effect.timeout("2 seconds"))

      releaseResponse.openUnsafe()
      yield* firstResponse.await.pipe(Effect.timeout("2 seconds"))
      assert.include(responseData, "HTTP/1.1 200")
      connection.write("GET /second HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
      const answered = yield* secondResponse.await.pipe(Effect.timeoutOption("1 second"))
      assert.strictEqual(
        answered._tag,
        "Some",
        "the second keep-alive request must receive a response before the shutdown timeout"
      )
      yield* Effect.promise(() => disposed).pipe(Effect.timeout("2 seconds"))
    }).pipe(
      Effect.ensuring(
        Effect.sync(() => {
          releaseResponse.openUnsafe()
          socket?.destroy()
          server.closeAllConnections()
        }).pipe(Effect.andThen(Effect.promise(() => runtime.dispose())))
      )
    )
  })

  it.live("closes idle HTTP/2 sessions on shutdown", () =>
    Effect.gen(function*() {
      const server = Http2.createServer()
      const scope = yield* Scope.make()
      yield* HttpServer.serve(Effect.succeed(HttpServerResponse.text("ok"))).pipe(
        Layer.provide(NodeHttpServer.layer(() => server, { port: 0, gracefulShutdownTimeout: "10 seconds" })),
        Layer.buildWithScope(scope)
      )
      const session = yield* Effect.acquireRelease(
        Effect.sync(() => Http2.connect(`http://127.0.0.1:${tcpPort(server)}`)),
        (session) => Effect.sync(() => session.destroy())
      )
      session.on("error", constVoid)
      yield* Effect.callback<void>((resume) => {
        session.once("connect", () => resume(Effect.void))
      }).pipe(Effect.timeout("2 seconds"))

      // Finalizers are uninterruptible, so the timeout applies to the join
      const closed = yield* Effect.forkDetach(Scope.close(scope, Exit.void))
      yield* Fiber.join(closed).pipe(Effect.timeout("2 seconds"))
    }))

  it.live("closes HTTP/2 sessions whose TLS handshake finishes during shutdown", () =>
    Effect.gen(function*() {
      const server = Http2.createSecureServer({ cert: tlsCert, key: tlsKey })
      const scope = yield* Scope.make()
      yield* HttpServer.serve(Effect.succeed(HttpServerResponse.text("ok"))).pipe(
        Layer.provide(NodeHttpServer.layer(() => server, { port: 0, gracefulShutdownTimeout: "10 seconds" })),
        Layer.buildWithScope(scope)
      )
      const port = tcpPort(server)
      const socket = yield* Effect.acquireRelease(
        Effect.sync(() => Net.connect(port, "127.0.0.1")),
        (socket) => Effect.sync(() => socket.destroy())
      )
      yield* Effect.callback<void>((resume) => {
        server.once("connection", () => resume(Effect.void))
      }).pipe(Effect.timeout("2 seconds"))

      // Finalizers are uninterruptible, so the timeout applies to the join
      const closed = yield* Effect.forkDetach(Scope.close(scope, Exit.void))
      yield* Effect.callback<void>((resume) => {
        const timer = setInterval(() => {
          if (!server.listening) resume(Effect.void)
        }, 1)
        return Effect.sync(() => clearInterval(timer))
      }).pipe(Effect.timeout("2 seconds"))
      const session = Http2.connect(`https://127.0.0.1:${port}`, {
        createConnection: () => Tls.connect({ socket, ca: tlsCert, ALPNProtocols: ["h2"] })
      })
      yield* Effect.addFinalizer(() => Effect.sync(() => session.destroy()))
      session.on("error", constVoid)
      // The server sends its settings once the session exists
      yield* Effect.callback<void>((resume) => {
        session.once("remoteSettings", () => resume(Effect.void))
      }).pipe(Effect.timeout("2 seconds"))

      yield* Fiber.join(closed).pipe(Effect.timeout("2 seconds"))
    }))

  it.live("disposes after a client aborts a handler awaiting an upstream request", () => {
    const upstreamStarted = Latch.makeUnsafe()
    const upstream = Http.createServer(() => {
      upstreamStarted.openUnsafe()
    })
    const server = Http.createServer()
    const router = HttpRouter.use((router) =>
      router.add(
        "GET",
        "/",
        Effect.gen(function*() {
          const request = yield* HttpServerRequest.HttpServerRequest
          if (request.method !== "HEAD") {
            return HttpServerResponse.text("ok")
          }
          yield* HttpClient.head(`http://localhost:${tcpPort(upstream)}`)
          return HttpServerResponse.empty()
        })
      )
    )
    const runtime = ManagedRuntime.make(
      Layer.effectDiscard(
        HttpRouter.serve(router).pipe(
          Layer.provide(NodeHttpServer.layer(() => server, {
            port: 0,
            gracefulShutdownTimeout: "100 millis"
          })),
          Layer.provide(FetchHttpClient.layer),
          Layer.launch,
          Effect.forkScoped,
          Effect.asVoid
        )
      )
    )

    return Effect.gen(function*() {
      yield* Effect.callback<void>((resume) => {
        upstream.listen(0, () => resume(Effect.void))
      })
      yield* Effect.promise(() => runtime.context())
      yield* Effect.callback<void>((resume) => {
        if (server.listening) {
          resume(Effect.void)
        } else {
          server.once("listening", () => resume(Effect.void))
        }
      })

      const downstreamClosed = Latch.makeUnsafe()
      const downstream = Http.request({ method: "HEAD", port: tcpPort(server), path: "/" })
      downstream.on("error", () => {})
      downstream.on("close", () => downstreamClosed.openUnsafe())
      downstream.end()
      yield* upstreamStarted.await
      downstream.destroy()
      yield* downstreamClosed.await

      const disposed = yield* Effect.promise(() => runtime.dispose()).pipe(
        Effect.timeoutOption("2 seconds")
      )
      assert.strictEqual(disposed._tag, "Some")
    }).pipe(
      Effect.ensuring(
        Effect.sync(() => {
          upstream.closeAllConnections()
          upstream.close()
        }).pipe(
          Effect.andThen(Effect.promise(() => runtime.dispose())),
          Effect.timeoutOrElse({ duration: "2 seconds", orElse: () => Effect.void })
        )
      )
    )
  })

  describe("HttpServerRespondable", () => {
    it.effect("error/schema", () =>
      Effect.gen(function*() {
        class CustomError extends Schema.Error<CustomError>("CustomError")({
          _tag: Schema.tag("CustomError"),
          name: Schema.String
        }) {
          [HttpServerRespondable.symbol]() {
            return HttpServerResponse.schemaJson(CustomError)(this, { status: 599 })
          }
        }
        yield* HttpRouter.add(
          "GET",
          "/home",
          new CustomError({ name: "test" })
        ).pipe(
          HttpRouter.serve,
          Layer.build
        )
        const client = yield* HttpClient.HttpClient
        const res = yield* client.get("/home")
        assert.strictEqual(res.status, 599)
        const err = yield* HttpClientResponse.schemaBodyJson(CustomError)(res)
        assert.deepStrictEqual(err, new CustomError({ name: "test" }))
      }).pipe(Effect.provide(NodeHttpServer.layerTest)))

    it.effect("httpapi error", () =>
      Effect.gen(function*() {
        yield* HttpRouter.add(
          "GET",
          "/home",
          new HttpApiError.BadRequest({})
        ).pipe(
          HttpRouter.serve,
          Layer.build
        )
        const client = yield* HttpClient.HttpClient
        const res = yield* client.get("/home")
        assert.strictEqual(res.status, 400)
      }).pipe(Effect.provide(NodeHttpServer.layerTest)))
  })

  it.effect("RouterConfig", () =>
    Effect.gen(function*() {
      yield* HttpRouter.add(
        "GET",
        "/:param",
        Effect.succeed(HttpServerResponse.empty())
      ).pipe(
        HttpRouter.serve,
        Layer.build
      )
      let res = yield* HttpClient.get("/123456")
      assert.strictEqual(res.status, 404)
      res = yield* HttpClient.get("/12345")
      assert.strictEqual(res.status, 204)
    }).pipe(
      Effect.provide([
        NodeHttpServer.layerTest,
        Layer.succeed(HttpRouter.RouterConfig)({ maxParamLength: 5 })
      ])
    ))

  it.effect("HttpRouter prefixed", () =>
    Effect.gen(function*() {
      const handler = HttpRouter.serve(HttpRouter.use(Effect.fnUntraced(function*(router_) {
        const router = router_.prefixed("/todos")
        yield* router.add(
          "GET",
          "/:id",
          Effect.flatMap(
            HttpRouter.schemaParams(IdParams),
            ({ id }) => todoResponse({ id, title: "test" })
          )
        )
        yield* router.addAll([
          HttpRouter.route("GET", "/", Effect.succeed(HttpServerResponse.text("root")))
        ])
      })))

      yield* Layer.build(handler)

      const todo = yield* HttpClient.get("/todos/1").pipe(
        Effect.flatMap(HttpClientResponse.schemaBodyJson(Todo))
      )
      expect(todo).toEqual({ id: 1, title: "test" })
      const root = yield* HttpClient.get("/todos").pipe(
        Effect.flatMap((r) => r.text)
      )
      expect(root).toEqual("root")
    }).pipe(Effect.provide(NodeHttpServer.layerTest)))

  it.effect("logs and traces status 101 after an upgraded WebSocket closes", () =>
    Effect.gen(function*() {
      const logged = yield* Deferred.make<unknown>()
      const logger = Logger.make((options) => {
        const annotations = options.fiber.getRef(References.CurrentLogAnnotations)
        if (annotations["http.url"] === "/ws") {
          Deferred.doneUnsafe(logged, Effect.succeed(annotations["http.status"]))
        }
      })
      const traced = yield* Deferred.make<unknown>()
      const tracer = Tracer.make({
        span(options) {
          const span = new Tracer.NativeSpan(options)
          if (options.kind === "server") {
            span.end = (endTime, exit) => {
              Tracer.NativeSpan.prototype.end.call(span, endTime, exit)
              Deferred.doneUnsafe(traced, Effect.succeed(span.attributes.get("http.response.status_code")))
            }
          }
          return span
        }
      })