(
isDev: boolean,
origin: string,
status: number,
path: string,
expectedText: string | Function | RegExp,
expectedHeaders = {},
fetchOpts: FetchOptions = {}
)
| 347 | } |
| 348 | |
| 349 | export async function testPath( |
| 350 | isDev: boolean, |
| 351 | origin: string, |
| 352 | status: number, |
| 353 | path: string, |
| 354 | expectedText: string | Function | RegExp, |
| 355 | expectedHeaders = {}, |
| 356 | fetchOpts: FetchOptions = {} |
| 357 | ) { |
| 358 | const opts: FetchOptions = { |
| 359 | retries: isCI ? 5 : 0, |
| 360 | ...fetchOpts, |
| 361 | redirect: 'manual', |
| 362 | status, |
| 363 | }; |
| 364 | const url = `${origin}${path}`; |
| 365 | const res = await fetchWithRetry(url, opts); |
| 366 | const msg = `Testing response from ${fetchOpts.method || 'GET'} ${url}`; |
| 367 | |
| 368 | console.log(msg); |
| 369 | expect(res.status, getEnvironmentMessage(isDev)).toBe(status); |
| 370 | validateResponseHeaders(res); |
| 371 | |
| 372 | if (typeof expectedText === 'string') { |
| 373 | const actualText = await res.text(); |
| 374 | expect(actualText.trim(), getEnvironmentMessage(isDev)).toBe( |
| 375 | expectedText.trim() |
| 376 | ); |
| 377 | } else if (typeof expectedText === 'function') { |
| 378 | const actualText = await res.text(); |
| 379 | await expectedText(actualText, res, isDev); |
| 380 | } else if (expectedText instanceof RegExp) { |
| 381 | const actualText = await res.text(); |
| 382 | expectedText.lastIndex = 0; // reset since we test twice |
| 383 | expect(actualText, getEnvironmentMessage(isDev)).toMatch(expectedText); |
| 384 | } |
| 385 | |
| 386 | if (expectedHeaders) { |
| 387 | Object.entries(expectedHeaders).forEach(([key, expectedValue]) => { |
| 388 | const actualValue = res.headers.get(key); |
| 389 | expect(actualValue, getEnvironmentMessage(isDev)).toBe(expectedValue); |
| 390 | }); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | function getEnvironmentMessage(isDev: boolean): string { |
| 395 | if (isDev) { |
no test coverage detected