File
Blob: tests/worker/helpers/http.ts
| 1 | import { exports } from "cloudflare:workers"; |
| 2 | |
| 3 | export function controlRequest(pathOrUrl: string, init: RequestInit = {}): Request { |
| 4 | const url = pathOrUrl.startsWith("http") ? pathOrUrl : `https://dav.example.com${pathOrUrl}`; |
| 5 | const headers = new Headers(init.headers); |
| 6 | return new Request(url, { ...init, headers }); |
| 7 | } |
| 8 | |
| 9 | export function subjectRequest(hostLabel: string, path: string, init: RequestInit = {}): Request { |
| 10 | const headers = new Headers(init.headers); |
| 11 | return new Request(`https://${hostLabel}.dav.example.com${path}`, { ...init, headers }); |
| 12 | } |
| 13 | |
| 14 | export function fetchWorker(request: Request): Promise<Response> { |
| 15 | return exports.default.fetch(request); |
| 16 | } |
| 17 | |
| 18 | export function setCookieHeaders(response: Response): string[] { |
| 19 | return response.headers.getSetCookie?.() ?? []; |
| 20 | } |
| 21 | |
| 22 | export function cookiePair(setCookie: string): string { |
| 23 | return setCookie.split(";")[0] ?? setCookie; |
| 24 | } |
| 25 | |
| 26 | export function cookiePairFor(response: Response, name: string): string { |
| 27 | const cookie = setCookieHeaders(response).find((header) => header.startsWith(`${name}=`)); |
| 28 | if (!cookie) throw new Error(`Missing Set-Cookie for ${name}`); |
| 29 | return cookiePair(cookie); |
| 30 | } |
| 31 | |
| 32 | export function cookieHeaderFrom(response: Response): string { |
| 33 | return setCookieHeaders(response).map(cookiePair).join("; "); |
| 34 | } |