Skip to content

Commit

Permalink
http testing modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej committed Aug 4, 2024
1 parent 5382449 commit 7c7757f
Show file tree
Hide file tree
Showing 13 changed files with 650 additions and 150 deletions.
24 changes: 24 additions & 0 deletions packages/platform-bun/src/BunHttpTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @since 0.41.3
*/
import type * as Etag from "@effect/platform/Etag"
import type * as HttpClient from "@effect/platform/HttpClient"
import type * as HttpPlatform from "@effect/platform/HttpPlatform"
import type * as HttpServer from "@effect/platform/HttpServer"
import type * as HttpServerError from "@effect/platform/HttpServerError"
import type * as Layer from "effect/Layer"
import type * as BunContext from "./BunContext.js"
import * as internal from "./internal/bunHttpTest.js"

/**
* @since 0.41.3
* @category layers
*/
export const layer: Layer.Layer<
| HttpClient.HttpClient.Default
| HttpServer.HttpServer
| HttpPlatform.HttpPlatform
| Etag.Generator
| BunContext.BunContext,
HttpServerError.ServeError
> = internal.layer
5 changes: 5 additions & 0 deletions packages/platform-bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export * as BunHttpServer from "./BunHttpServer.js"
*/
export * as BunHttpServerRequest from "./BunHttpServerRequest.js"

/**
* @since 0.41.3
*/
export * as BunHttpTest from "./BunHttpTest.js"

/**
* @since 1.0.0
*/
Expand Down
10 changes: 10 additions & 0 deletions packages/platform-bun/src/internal/bunHttpTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as HttpClient from "@effect/platform/HttpClient"
import * as HttpTest from "@effect/platform/HttpTest"
import * as Layer from "effect/Layer"
import * as BunHttpServer from "../BunHttpServer.js"

/** @internal */
export const layer = HttpTest.clientLayer.pipe(
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.fetch)),
Layer.provideMerge(BunHttpServer.layer({ port: 0 }))
)
19 changes: 19 additions & 0 deletions packages/platform-bun/test/BunHttpTest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HttpClientRequest, HttpRouter, HttpServerResponse, HttpTest } from "@effect/platform"
import { BunHttpTest } from "@effect/platform-bun"
import { expect, it } from "bun:test"
import { Effect } from "effect"

it("BunHttpTest", () =>
Effect.gen(function*(_) {
const router = HttpRouter.empty.pipe(
HttpRouter.get("/", HttpServerResponse.text("Hello, World!"))
)
const client = yield* HttpTest.serve(router)

const response1 = yield* client(HttpClientRequest.get("/"))
expect(response1.status).toEqual(200)
expect(yield* response1.text).toEqual("Hello, World!")

const response2 = yield* client(HttpClientRequest.get("/non-existing"))
expect(response2.status).toEqual(404)
}).pipe(Effect.provide(BunHttpTest.layer), Effect.scoped, Effect.runPromise))
2 changes: 1 addition & 1 deletion packages/platform-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export * as NodeHttpServer from "./NodeHttpServer.js"
export * as NodeHttpServerRequest from "./NodeHttpServerRequest.js"

/**
* @since 1.0.0
* @since 0.56.3
*/
export * as NodeHttpTest from "./NodeHttpTest.js"

Expand Down
25 changes: 2 additions & 23 deletions packages/platform-node/src/internal/nodeHttpTest.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
import * as HttpClient from "@effect/platform/HttpClient"
import * as HttpClientRequest from "@effect/platform/HttpClientRequest"
import * as HttpServer from "@effect/platform/HttpServer"
import * as Effect from "effect/Effect"
import * as HttpTest from "@effect/platform/HttpTest"
import * as Layer from "effect/Layer"
import { createServer } from "node:http"
import * as NodeHttpClient from "../NodeHttpClient.js"
import * as NodeHttpServer from "../NodeHttpServer.js"

/** @internal */
const makeClient = Effect.flatMap(
Effect.zip(HttpServer.HttpServer, HttpClient.HttpClient),
([server, client]) => {
const port = server.address._tag === "TcpAddress" ? server.address.port : null

if (port === null) {
return Effect.dieMessage("BUG: Port not allocated")
}

return Effect.succeed(HttpClient.mapRequest(
client,
HttpClientRequest.prependUrl(`http://127.0.0.1:${port}`)
))
}
)

/** @internal */
export const layer = makeClient.pipe(
Layer.effect(HttpClient.HttpClient),
export const layer = HttpTest.clientLayer.pipe(
Layer.provide(NodeHttpClient.layerWithoutAgent),
Layer.provide(NodeHttpClient.makeAgentLayer({ keepAlive: false })),
Layer.provideMerge(NodeHttpServer.layer(createServer, { port: 0 }))
Expand Down
Loading

0 comments on commit 7c7757f

Please sign in to comment.