|
1 | 1 | import { expect, it } from "@effect/vitest"; |
| 2 | +import * as Cause from "effect/Cause"; |
2 | 3 | import * as Effect from "effect/Effect"; |
3 | 4 | import * as Layer from "effect/Layer"; |
4 | 5 | import * as Tracer from "effect/Tracer"; |
5 | | -import { HttpClient, HttpClientResponse } from "effect/http"; |
| 6 | +import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http"; |
6 | 7 |
|
7 | 8 | import * as RemoteImage from "./RemoteImage.ts"; |
8 | 9 |
|
9 | | -function fixture(respond: (url: string) => Response) { |
| 10 | +function fixture(respond: (url: string) => Response | Error) { |
10 | 11 | const requests: Array<{ url: string; headers: Readonly<Record<string, string>> }> = []; |
11 | 12 | const http = HttpClient.make((request) => { |
12 | 13 | requests.push({ url: request.url, headers: request.headers }); |
13 | | - return Effect.succeed(HttpClientResponse.fromWeb(request, respond(request.url))); |
| 14 | + const response = respond(request.url); |
| 15 | + return response instanceof Error |
| 16 | + ? Effect.fail( |
| 17 | + new HttpClientError.HttpClientError({ |
| 18 | + reason: new HttpClientError.TransportError({ request, cause: response }), |
| 19 | + }), |
| 20 | + ) |
| 21 | + : Effect.succeed(HttpClientResponse.fromWeb(request, response)); |
14 | 22 | }); |
15 | 23 | return { |
16 | 24 | requests, |
17 | 25 | layer: RemoteImage.layer.pipe(Layer.provide(Layer.succeed(HttpClient.HttpClient, http))), |
18 | 26 | }; |
19 | 27 | } |
20 | 28 |
|
21 | | -it.effect( |
22 | | - "reads an image without browser CORS headers and follows signed redirects without credentials", |
23 | | - () => { |
24 | | - const clientSpans: Array<Tracer.Span> = []; |
| 29 | +it.effect.each(["success", "transport", "body"] as const)( |
| 30 | + "reads signed redirects without credentials or URL-bearing traces: %s", |
| 31 | + (mode) => { |
| 32 | + const spans: Array<Tracer.Span> = []; |
25 | 33 | const tracer = Tracer.make({ |
26 | 34 | span: (options) => { |
27 | 35 | const span = new Tracer.NativeSpan(options); |
28 | | - if (options.kind === "client") clientSpans.push(span); |
| 36 | + spans.push(span); |
29 | 37 | return span; |
30 | 38 | }, |
31 | 39 | }); |
32 | | - const test = fixture((url) => |
33 | | - url === "https://images.example.com/image" |
34 | | - ? new Response(null, { |
35 | | - status: 302, |
36 | | - headers: { location: "https://cdn.example.com/shot.png?signature=example" }, |
37 | | - }) |
38 | | - : new Response("png", { headers: { "content-type": "image/png" } }), |
39 | | - ); |
| 40 | + const signedUrl = "https://cdn.example.com/shot.png?jwt=private-image-token"; |
| 41 | + const test = fixture((url) => { |
| 42 | + if (url === "https://images.example.com/image") { |
| 43 | + return new Response(null, { status: 302, headers: { location: signedUrl } }); |
| 44 | + } |
| 45 | + if (mode === "transport") return new Error("Connection failed"); |
| 46 | + return new Response( |
| 47 | + mode === "body" |
| 48 | + ? new ReadableStream({ |
| 49 | + start: (controller) => controller.error(new Error("Body failed")), |
| 50 | + }) |
| 51 | + : "png", |
| 52 | + { headers: { "content-type": "image/png" } }, |
| 53 | + ); |
| 54 | + }); |
40 | 55 | return Effect.gen(function* () { |
41 | 56 | const images = yield* RemoteImage.RemoteImage; |
42 | | - const result = yield* images.read("https://images.example.com/image"); |
43 | | - expect(clientSpans).toEqual([]); |
44 | | - expect(result.mimeType).toBe("image/png"); |
45 | | - expect(new TextDecoder().decode(result.bytes)).toBe("png"); |
| 57 | + const result = yield* images.read("https://images.example.com/image").pipe(Effect.exit); |
| 58 | + if (mode === "success") { |
| 59 | + expect(result._tag).toBe("Success"); |
| 60 | + if (result._tag !== "Success") return; |
| 61 | + expect(result.value.mimeType).toBe("image/png"); |
| 62 | + expect(new TextDecoder().decode(result.value.bytes)).toBe("png"); |
| 63 | + } else { |
| 64 | + expect(result._tag).toBe("Failure"); |
| 65 | + const readSpan = spans.find((span) => span.name === "RemoteImage.read"); |
| 66 | + expect(readSpan?.status._tag).toBe("Ended"); |
| 67 | + if (readSpan?.status._tag !== "Ended" || readSpan.status.exit._tag !== "Failure") { |
| 68 | + throw new Error("Expected a failed image read span"); |
| 69 | + } |
| 70 | + expect(Cause.pretty(readSpan.status.exit.cause)).not.toContain("private-image-token"); |
| 71 | + } |
| 72 | + expect(spans.filter((span) => span.kind === "client")).toEqual([]); |
46 | 73 | expect(test.requests.map(({ url }) => url)).toEqual([ |
47 | 74 | "https://images.example.com/image", |
48 | | - "https://cdn.example.com/shot.png?signature=example", |
| 75 | + signedUrl, |
49 | 76 | ]); |
50 | 77 | for (const { headers } of test.requests) { |
51 | 78 | expect(headers.authorization).toBeUndefined(); |
|
0 commit comments