(
target: TargetShape,
identity: Identity,
path: string,
init: RequestInit = {},
expectedStatus = 200,
)
| 156 | }; |
| 157 | |
| 158 | const request = async <T>( |
| 159 | target: TargetShape, |
| 160 | identity: Identity, |
| 161 | path: string, |
| 162 | init: RequestInit = {}, |
| 163 | expectedStatus = 200, |
| 164 | ): Promise<{ readonly body: T; readonly text: string }> => { |
| 165 | const headers = new Headers(init.headers); |
| 166 | headers.set("origin", new URL(target.baseUrl).origin); |
| 167 | for (const [name, value] of Object.entries(await authHeaders(target, identity))) { |
| 168 | headers.set(name, value); |
| 169 | } |
| 170 | if (init.body !== undefined && !headers.has("content-type")) { |
| 171 | headers.set("content-type", "application/json"); |
| 172 | } |
| 173 | const response = await fetch(new URL(path, target.baseUrl), { ...init, headers }); |
| 174 | const text = await response.text(); |
| 175 | expect(response.status, `${init.method ?? "GET"} ${path}: ${text}`).toBe(expectedStatus); |
| 176 | return { body: (text.length > 0 ? JSON.parse(text) : null) as T, text }; |
| 177 | }; |
| 178 | |
| 179 | const postJson = <T>( |
| 180 | target: TargetShape, |
no test coverage detected