-
-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathHttpApp.test.ts
More file actions
190 lines (177 loc) · 6.85 KB
/
HttpApp.test.ts
File metadata and controls
190 lines (177 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { HttpApp, HttpServerResponse } from "@effect/platform"
import { describe, test } from "@effect/vitest"
import { deepStrictEqual, strictEqual } from "@effect/vitest/utils"
import { Context, Effect, FiberRef, Runtime, Stream } from "effect"
import * as Layer from "effect/Layer"
describe("Http/App", () => {
describe("toWebHandler", () => {
test("json", async () => {
const handler = HttpApp.toWebHandler(HttpServerResponse.json({ foo: "bar" }))
const response = await handler(new Request("http://localhost:3000/"))
deepStrictEqual(await response.json(), {
foo: "bar"
})
})
test("cookies", async () => {
const handler = HttpApp.toWebHandler(
HttpServerResponse.unsafeJson({ foo: "bar" }).pipe(
HttpServerResponse.unsafeSetCookie("foo", "bar"),
HttpServerResponse.unsafeSetCookie("test", "123", { secure: true, httpOnly: true, sameSite: "strict" })
)
)
const response = await handler(new Request("http://localhost:3000/"))
deepStrictEqual(response.headers.getSetCookie(), [
"foo=bar",
"test=123; HttpOnly; Secure; SameSite=Strict"
])
deepStrictEqual(await response.json(), {
foo: "bar"
})
})
test("stream", async () => {
const handler = HttpApp.toWebHandler(HttpServerResponse.stream(Stream.make("foo", "bar").pipe(Stream.encodeText)))
const response = await handler(new Request("http://localhost:3000/"))
strictEqual(await response.text(), "foobar")
})
test("stream scope", async () => {
let order = 0
let streamFinalized = 0
let handlerFinalized = 0
const handler = HttpApp.toWebHandler(Effect.gen(function*() {
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
handlerFinalized = order
order += 1
})
)
const stream = Stream.make("foo", "bar").pipe(
Stream.encodeText,
Stream.ensuring(Effect.sync(() => {
streamFinalized = order
order += 1
}))
)
return HttpServerResponse.stream(stream)
}))
const response = await handler(new Request("http://localhost:3000/"))
strictEqual(await response.text(), "foobar")
strictEqual(streamFinalized < handlerFinalized, true)
})
test("stream runtime", async () => {
const handler = HttpApp.toWebHandlerRuntime(
Runtime.defaultRuntime.pipe(
Runtime.setFiberRef(FiberRef.currentConcurrency, 420)
)
)(HttpServerResponse.stream(
FiberRef.get(FiberRef.currentConcurrency).pipe(Stream.map(String), Stream.encodeText)
))
const response = await handler(new Request("http://localhost:3000/"))
strictEqual(await response.text(), "420")
})
test("stream layer", async () => {
const { handler } = HttpApp.toWebHandlerLayer(
HttpServerResponse.stream(
FiberRef.get(FiberRef.currentConcurrency).pipe(Stream.map(String), Stream.encodeText)
),
Layer.locallyScoped(FiberRef.currentConcurrency, 420)
)
const response = await handler(new Request("http://localhost:3000/"))
strictEqual(await response.text(), "420")
})
})
test("custom context", async () => {
class Env extends Context.Reference<Env>()("Env", {
defaultValue: () => ({ foo: "bar" })
}) {}
const handler = HttpApp.toWebHandler(Effect.gen(function*() {
const env = yield* Env
return yield* HttpServerResponse.json(env)
}))
const response = await handler(new Request("http://localhost:3000/"), Env.context({ foo: "baz" }))
deepStrictEqual(await response.json(), {
foo: "baz"
})
})
describe("fromWebHandler", () => {
test("basic GET request", async () => {
const webHandler = async (request: Request) => {
return new Response(`Hello from ${request.url}`, {
status: 200,
headers: { "Content-Type": "text/plain" }
})
}
const app = HttpApp.fromWebHandler(webHandler)
const handler = HttpApp.toWebHandler(app)
const response = await handler(new Request("http://localhost:3000/hello"))
strictEqual(response.status, 200)
strictEqual(await response.text(), "Hello from http://localhost:3000/hello")
})
test("POST with JSON body", async () => {
const webHandler = async (request: Request) => {
const body = await request.json()
return Response.json({ received: body })
}
const app = HttpApp.fromWebHandler(webHandler)
const handler = HttpApp.toWebHandler(app)
const response = await handler(
new Request("http://localhost:3000/", {
method: "POST",
body: JSON.stringify({ message: "hello" }),
headers: { "Content-Type": "application/json" }
})
)
deepStrictEqual(await response.json(), {
received: { message: "hello" }
})
})
test("preserves request headers", async () => {
const webHandler = async (request: Request) => {
return Response.json({
authorization: request.headers.get("Authorization"),
custom: request.headers.get("X-Custom-Header")
})
}
const app = HttpApp.fromWebHandler(webHandler)
const handler = HttpApp.toWebHandler(app)
const response = await handler(
new Request("http://localhost:3000/", {
headers: {
"Authorization": "Bearer token123",
"X-Custom-Header": "custom-value"
}
})
)
deepStrictEqual(await response.json(), {
authorization: "Bearer token123",
custom: "custom-value"
})
})
test("preserves response status and headers", async () => {
const webHandler = async (_request: Request) => {
return new Response("Not Found", {
status: 404,
statusText: "Not Found",
headers: {
"X-Error-Code": "RESOURCE_NOT_FOUND",
"Content-Type": "text/plain"
}
})
}
const app = HttpApp.fromWebHandler(webHandler)
const handler = HttpApp.toWebHandler(app)
const response = await handler(new Request("http://localhost:3000/missing"))
strictEqual(response.status, 404)
strictEqual(response.headers.get("X-Error-Code"), "RESOURCE_NOT_FOUND")
strictEqual(await response.text(), "Not Found")
})
test("round-trip with toWebHandler", async () => {
// Create an Effect app, convert to web handler, then back to Effect app
const originalApp = HttpServerResponse.json({ source: "effect" })
const webHandler = HttpApp.toWebHandler(originalApp)
const wrappedApp = HttpApp.fromWebHandler(webHandler)
const finalHandler = HttpApp.toWebHandler(wrappedApp)
const response = await finalHandler(new Request("http://localhost:3000/"))
deepStrictEqual(await response.json(), { source: "effect" })
})
})
})