(
path: string,
init: { readonly method: "POST" | "DELETE"; readonly body?: unknown },
timeoutSeconds = 30,
)
| 116 | const shellQuote = (value: string): string => `'${value.replaceAll("'", "'\\''")}'`; |
| 117 | |
| 118 | const guestRequest = async <T>( |
| 119 | path: string, |
| 120 | init: { readonly method: "POST" | "DELETE"; readonly body?: unknown }, |
| 121 | timeoutSeconds = 30, |
| 122 | ): Promise<CurlResult<T>> => { |
| 123 | const body = init.body === undefined ? undefined : JSON.stringify(init.body); |
| 124 | const bodyFlag = body === undefined ? "" : ` --data-binary ${shellQuote(body)}`; |
| 125 | const command = [ |
| 126 | "curl", |
| 127 | "-sS", |
| 128 | "--max-time", |
| 129 | String(timeoutSeconds), |
| 130 | "-w", |
| 131 | shellQuote("\\n__HTTP_STATUS__:%{http_code}"), |
| 132 | "-X", |
| 133 | init.method, |
| 134 | "-H", |
| 135 | shellQuote(`Authorization: Bearer ${token()}`), |
| 136 | "-H", |
| 137 | shellQuote("content-type: application/json"), |
| 138 | bodyFlag, |
| 139 | shellQuote(`http://127.0.0.1:4789${path}`), |
| 140 | ].join(" "); |
| 141 | const result = await ssh(command); |
| 142 | expect( |
| 143 | result.code, |
| 144 | `${init.method} ${path} exits cleanly\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`, |
| 145 | ).toBe(0); |
| 146 | const marker = "\n__HTTP_STATUS__:"; |
| 147 | const markerIndex = result.stdout.lastIndexOf(marker); |
| 148 | expect(markerIndex, `${init.method} ${path} printed an HTTP status`).toBeGreaterThanOrEqual(0); |
| 149 | const text = result.stdout.slice(0, markerIndex); |
| 150 | const status = Number(result.stdout.slice(markerIndex + marker.length).trim()); |
| 151 | return { status, body: (text.length > 0 ? JSON.parse(text) : null) as T, text }; |
| 152 | }; |
| 153 | |
| 154 | const expectGuestRequest = async <T>( |
| 155 | path: string, |
nothing calls this directly
no test coverage detected