process.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { HttpServer } from "effect/unstable/http"
  4. import { it } from "../../core/test/lib/effect"
  5. import { ServerProcess } from "../src/process"
  6. it.live("allows browser preflight requests without credentials", () =>
  7. Effect.gen(function* () {
  8. const server = yield* ServerProcess.start<never, never>({
  9. hostname: "127.0.0.1",
  10. port: 0,
  11. password: "secret",
  12. database: { path: ":memory:" },
  13. })
  14. const response = yield* Effect.promise(() =>
  15. fetch(new URL("/api/health", HttpServer.formatAddress(server.address)), {
  16. method: "OPTIONS",
  17. headers: {
  18. origin: "http://localhost:3000",
  19. "access-control-request-method": "GET",
  20. "access-control-request-headers": "authorization",
  21. },
  22. }),
  23. )
  24. expect(response.status).toBe(204)
  25. expect(response.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
  26. expect(response.headers.get("access-control-allow-headers")).toBe("authorization")
  27. const health = yield* Effect.promise(() =>
  28. fetch(new URL("/api/health", HttpServer.formatAddress(server.address)), {
  29. headers: {
  30. authorization: `Basic ${btoa("opencode:secret")}`,
  31. origin: "http://localhost:3000",
  32. },
  33. }),
  34. )
  35. expect(health.status).toBe(200)
  36. expect(health.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
  37. }),
  38. )