Skip to content
File

Blob: tests/worker/helpers/http.ts

typescript35 lines
1import { exports } from "cloudflare:workers";
2 
3export 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 
9export 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 
14export function fetchWorker(request: Request): Promise<Response> {
15 return exports.default.fetch(request);
16}
17 
18export function setCookieHeaders(response: Response): string[] {
19 return response.headers.getSetCookie?.() ?? [];
20}
21 
22export function cookiePair(setCookie: string): string {
23 return setCookie.split(";")[0] ?? setCookie;
24}
25 
26export 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 
32export function cookieHeaderFrom(response: Response): string {
33 return setCookieHeaders(response).map(cookiePair).join("; ");
34}