The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as BrowserHttpClient from "@effect/platform-browser/BrowserHttpClient" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import * as Effect from "effect/Effect" |
| 4 | import * as Cookies from "effect/http/Cookies" |
| 5 | import * as HttpBody from "effect/http/HttpBody" |
| 6 | import * as HttpClient from "effect/http/HttpClient" |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest" |
| 8 | import * as Layer from "effect/Layer" |
| 9 | import * as Stream from "effect/Stream" |
| 10 | import * as MXHR from "mock-xmlhttprequest" |
| 11 | |
| 12 | const layer = (routes: Parameters<typeof MXHR.newServer>[0], responseURL = "") => |
| 13 | Layer.unwrap(Effect.sync(() => { |
| 14 | const server = MXHR.newServer(routes) |
| 15 | return BrowserHttpClient.layerXMLHttpRequest.pipe( |
| 16 | Layer.provide(Layer.succeed(BrowserHttpClient.XMLHttpRequest, () => { |
| 17 | const xhr = server.xhrFactory() |
| 18 | xhr.responseURL = responseURL |
| 19 | return xhr |
| 20 | })) |
| 21 | ) |
| 22 | })) |
| 23 | |
| 24 | describe("BrowserHttpClient", () => { |
| 25 | it.effect("json", () => |
| 26 | Effect.gen(function*() { |
| 27 | const response = yield* HttpClient.get("http://localhost:8080/my/url") |
| 28 | assert.strictEqual(response.url, "http://localhost:8080/final?value=1") |
| 29 | assert.deepStrictEqual(yield* response.json, { message: "Success!" }) |
| 30 | }).pipe(Effect.provide(layer({ |
| 31 | get: ["http://localhost:8080/my/url", { |
| 32 | headers: { "Content-Type": "application/json" }, |
| 33 | body: "{ \"message\": \"Success!\" }" |
| 34 | }] |
| 35 | }, "http://localhost:8080/final?value=1#fragment")))) |
| 36 | |
| 37 | it.effect("stream", () => |
| 38 | Effect.gen(function*() { |
| 39 | const response = yield* HttpClient.get("http://localhost:8080/my/url?existing=1", { |
| 40 | urlParams: { value: "a#b" }, |
| 41 | hash: "fragment" |
| 42 | }) |
| 43 | assert.strictEqual(response.url, "http://localhost:8080/my/url?existing=1&value=a%23b") |
| 44 | const body = yield* response.stream.pipe(Stream.decodeText(), Stream.mkString) |
| 45 | assert.deepStrictEqual(body, "{ \"message\": \"Success!\" }") |
| 46 | }).pipe(Effect.provide(layer({ |
| 47 | get: ["http://localhost:8080/my/url?existing=1&value=a%23b#fragment", { |
| 48 | headers: { "Content-Type": "application/json" }, |
| 49 | body: "{ \"message\": \"Success!\" }" |
| 50 | }] |
| 51 | })))) |
| 52 | |
| 53 | it.effect("streamed request body", () => |
| 54 | Effect.gen(function*() { |
| 55 | const response = yield* HttpClient.post("http://localhost:8080/upload", { |
| 56 | body: HttpBody.stream(Stream.make(new Uint8Array([1, 2]), new Uint8Array([3]))) |
| 57 | }) |
| 58 | assert.deepStrictEqual(yield* response.json, [1, 2, 3]) |
| 59 | }).pipe(Effect.provide(layer({ |
| 60 | post: ["http://localhost:8080/upload", (request) => { |
| 61 | request.respond(200, { "Content-Type": "application/json" }, JSON.stringify(Array.from(request.body))) |
| 62 | }] |
| 63 | })))) |
| 64 | |
| 65 | it.effect("cookies", () => |
| 66 | Effect.gen(function*() { |
| 67 | const cookies = yield* HttpClient.get("http://localhost:8080/my/url").pipe( |
| 68 | Effect.map((response) => response.cookies) |
| 69 | ) |
| 70 | assert.deepStrictEqual(Cookies.toRecord(cookies), { |
| 71 | foo: "bar" |
| 72 | }) |
| 73 | }).pipe(Effect.provide(layer({ |
| 74 | get: ["http://localhost:8080/my/url", { |
| 75 | headers: { "Content-Type": "application/json", "Set-Cookie": "foo=bar; HttpOnly; Secure" }, |
| 76 | body: "{ \"message\": \"Success!\" }" |
| 77 | }] |
| 78 | })))) |
| 79 | |
| 80 | it.effect("arrayBuffer", () => |
| 81 | Effect.gen(function*() { |
| 82 | const body = yield* HttpClient.get("http://localhost:8080/my/url").pipe( |
| 83 | Effect.flatMap((response) => response.arrayBuffer), |
| 84 | BrowserHttpClient.withXHRArrayBuffer |
| 85 | ) |
| 86 | assert.strictEqual(new TextDecoder().decode(body), "{ \"message\": \"Success!\" }") |
| 87 | }).pipe(Effect.provide(layer({ |
| 88 | get: ["http://localhost:8080/my/url", { |
| 89 | headers: { "Content-Type": "application/json" }, |
| 90 | body: "{ \"message\": \"Success!\" }" |
| 91 | }] |
| 92 | })))) |
| 93 | |
| 94 | it.effect("arrayBuffer without withXHRArrayBuffer", () => |
| 95 | Effect.gen(function*() { |
| 96 | const body = yield* HttpClient.get("http://localhost:8080/my/url").pipe( |
| 97 | Effect.flatMap((response) => response.arrayBuffer) |
| 98 | ) |
| 99 | assert.strictEqual(new TextDecoder().decode(body), "{ \"message\": \"Success!\" }") |
| 100 | }).pipe(Effect.provide(layer({ |
| 101 | get: ["http://localhost:8080/my/url", { |
| 102 | headers: { "Content-Type": "application/json" }, |
| 103 | body: "{ \"message\": \"Success!\" }" |
| 104 | }] |
| 105 | })))) |
| 106 | |
| 107 | it.effect("readers in ArrayBuffer mode", () => |
| 108 | Effect.gen(function*() { |
| 109 | const response = yield* HttpClient.get("http://localhost:8080/my/url").pipe( |
| 110 | BrowserHttpClient.withXHRArrayBuffer |
| 111 | ) |
| 112 | assert.strictEqual(yield* response.text, "{ \"message\": \"café\" }") |
| 113 | assert.deepStrictEqual(yield* response.json, { message: "café" }) |
| 114 | assert.strictEqual( |
| 115 | yield* response.stream.pipe(Stream.decodeText(), Stream.mkString), |
| 116 | "{ \"message\": \"café\" }" |
| 117 | ) |
| 118 | }).pipe(Effect.provide(layer({ |
| 119 | get: ["http://localhost:8080/my/url", { |
| 120 | headers: { "Content-Type": "application/json" }, |
| 121 | body: "{ \"message\": \"café\" }" |
| 122 | }] |
| 123 | })))) |
| 124 | |
| 125 | it.effect("urlParamsBody preserves leading question marks", () => |
| 126 | Effect.gen(function*() { |
| 127 | const response = yield* HttpClient.get("http://localhost/form") |
| 128 | assert.deepStrictEqual([...yield* response.urlParamsBody], [["??name", "value"], ["next", "ok"]]) |
| 129 | }).pipe(Effect.provide(layer({ |
| 130 | get: ["http://localhost/form", { |
| 131 | headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 132 | body: "??name=value&next=ok" |
| 133 | }] |
| 134 | })))) |
| 135 | |
| 136 | it.effect("decodes an XHR response as FormData", () => |
| 137 | Effect.gen(function*() { |
| 138 | const formData = yield* HttpClient.get("http://localhost/form").pipe( |
| 139 | Effect.flatMap((response) => response.formData) |
| 140 | ) |
| 141 | assert.strictEqual(formData.get("value"), "test") |
| 142 | }).pipe(Effect.provide(layer({ |
| 143 | get: ["http://localhost/form", { |
| 144 | headers: { "content-type": "multipart/form-data; boundary=x" }, |
| 145 | body: "--x\r\nContent-Disposition: form-data; name=\"value\"\r\n\r\ntest\r\n--x--\r\n" |
| 146 | }] |
| 147 | })))) |
| 148 | |
| 149 | it.effect("rejects an upgrade request before sending it", () => { |
| 150 | let hits = 0 |
| 151 | return Effect.gen(function*() { |
| 152 | const error = yield* HttpClient.execute( |
| 153 | HttpClientRequest.post("http://localhost:8080/attach").pipe(HttpClientRequest.upgrade("tcp")) |
| 154 | ).pipe(Effect.flip) |
| 155 | assert.strictEqual(error.reason._tag, "TransportError") |
| 156 | assert.strictEqual(hits, 0) |
| 157 | }).pipe(Effect.provide(layer({ |
| 158 | post: ["http://localhost:8080/attach", (request) => { |
| 159 | hits++ |
| 160 | request.respond(200) |
| 161 | }] |
| 162 | }))) |
| 163 | }) |
| 164 | }) |