Repository: microsoft/playwright
File: tests/page/page-event-request.spec.ts (TypeScript)

Code before:
      });
      res.end();
      return;
    }
    res.writeHead(200, { 'Content-type': 'text/plain', 'Access-Control-Allow-Origin': '*' });
    res.end('Hello there!');
  });
  await page.route('**/*', route => route.continue());
  const clientRequests = [];
  page.on('request', request => {
    clientRequests.push(`${request.method()} ${request.url()}`);
  });
  const response = await page.evaluate(async url => {
    const response = await fetch(url, {
      method: 'POST',
      body: '',
      headers: {
        'Content-Type': 'application/json',
        'X-Custom-Header': 'test-value'
      }
    });
    return await response.text();
  }, server.CROSS_PROCESS_PREFIX + '/cors').catch(() => {});
  expect(response).toBe('Hello there!');
  expect.soft(serverRequests).toEqual([
    ...(browserName !== 'chromium' && !isBidi ? ['OPTIONS /cors'] : []),
    'POST /cors',
  ]);
  expect.soft(clientRequests).toEqual([
    `POST ${server.CROSS_PROCESS_PREFIX}/cors`,
  ]);
});

it('should return last requests', async ({ page, server }) => {
  await page.goto(server.PREFIX + '/title.html');
  for (let i = 0; i < 200; ++i)
    server.setRoute('/fetch?' + i, (req, res) => res.end('url:' + server.PREFIX + req.url));

  // #0 is the navigation request, so start with #1.
  for (let i = 0; i < 99; ++i)
    await page.evaluate(url => fetch(url), server.PREFIX + '/fetch?' + i);
  const first99Requests = await page.requests();
  first99Requests.shift();
  for (let i = 99; i < 199; ++i)
    await page.evaluate(url => fetch(url), server.PREFIX + '/fetch?' + i);
  const last100Requests = await page.requests();
  const allRequests = [...first99Requests, ...last100Requests];

  // All 199 requests are fully functional.
  const received = await Promise.all(allRequests.map(async request => {
    const response = await request.response();

Review comment (suggestion):
Let's make it readable:
```suggestion
    ...(browserName === 'chromium' || isBidi ? [] : ['OPTIONS /cors']),
```

Code after:
      });
      res.end();
      return;
    }
    res.writeHead(200, { 'Content-type': 'text/plain', 'Access-Control-Allow-Origin': '*' });
    res.end('Hello there!');
  });
  await page.route('**/*', route => route.continue());
  const clientRequests = [];
  page.on('request', request => {
    clientRequests.push(`${request.method()} ${request.url()}`);
  });
  const response = await page.evaluate(async url => {
    const response = await fetch(url, {
      method: 'POST',
      body: '',
      headers: {
        'Content-Type': 'application/json',
        'X-Custom-Header': 'test-value'
      }
    });
    return await response.text();
  }, server.CROSS_PROCESS_PREFIX + '/cors').catch(() => {});
  expect(response).toBe('Hello there!');
  expect.soft(serverRequests).toEqual([
    ...(browserName === 'chromium' || isBidi ? [] : ['OPTIONS /cors']),
    'POST /cors',
  ]);
  expect.soft(clientRequests).toEqual([
    `POST ${server.CROSS_PROCESS_PREFIX}/cors`,
  ]);
});

it('should return last requests', async ({ page, server }) => {
  await page.goto(server.PREFIX + '/title.html');
  for (let i = 0; i < 200; ++i)
    server.setRoute('/fetch?' + i, (req, res) => res.end('url:' + server.PREFIX + req.url));

  // #0 is the navigation request, so start with #1.
  for (let i = 0; i < 99; ++i)
    await page.evaluate(url => fetch(url), server.PREFIX + '/fetch?' + i);
  const first99Requests = await page.requests();
  first99Requests.shift();
  for (let i = 99; i < 199; ++i)
    await page.evaluate(url => fetch(url), server.PREFIX + '/fetch?' + i);
  const last100Requests = await page.requests();
  const allRequests = [...first99Requests, ...last100Requests];

  // All 199 requests are fully functional.
  const received = await Promise.all(allRequests.map(async request => {
    const response = await request.response();