Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

4.0 KB
Raw Blame
/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import dns from 'dns';
import http from 'http';
import { browserTest as test, expect } from '../config/browserTest';
import { utils } from '../../packages/playwright-core/lib/coreBundle.js';

test.skip(({ mode }) => mode !== 'default');

test('NE-128 httpRequest should honor socketTimeout while connecting', async () => {
  // 203.0.113.1 is TEST-NET-3 (RFC 5737), guaranteed to be unroutable, so the
  // TCP connection stalls instead of failing. Node's default agents come with
  // a 5s socket timeout, while `request.setTimeout()` only takes effect after
  // the socket connects - make sure the requested timeout governs from the start.
  const socketTimeout = 8000;
  const start = Date.now();
  const error: Error = await new Promise(resolve => {
    utils.httpRequest({
      url: 'http://203.0.113.1/index.html',
      socketTimeout,
    }, () => resolve(new Error('unexpected response')), resolve);
  });
  const elapsed = Date.now() - start;
  if (!error.message.includes('timed out after')) {
    // Some networks actively refuse TEST-NET addresses instead of blackholing them.
    test.skip(true, `environment does not stall connections to TEST-NET: ${error.message}`);
    return;
  }
  expect(error.message).toContain(`timed out after ${socketTimeout}ms`);
  expect(elapsed).toBeGreaterThanOrEqual(socketTimeout - 1000);
});

test('NE-129 httpRequest should honor socketTimeout for stalled responses', async () => {
  const server = http.createServer(() => {});
  await new Promise<void>(f => server.listen(0, '127.0.0.1', () => f()));
  const port = (server.address() as import('node:net').AddressInfo).port;
  const socketTimeout = 1500;
  const start = Date.now();
  try {
    const error: Error = await new Promise(resolve => {
      utils.httpRequest({
        url: `http://127.0.0.1:${port}/index.html`,
        socketTimeout,
      }, () => resolve(new Error('unexpected response')), resolve);
    });
    const elapsed = Date.now() - start;
    expect(error.message).toContain(`timed out after ${socketTimeout}ms`);
    expect(elapsed).toBeGreaterThanOrEqual(socketTimeout - 500);
    expect(elapsed).toBeLessThan(socketTimeout * 3);
  } finally {
    server.close();
  }
});

test('NE-130 httpRequest should honor ipv4first result order', async () => {
  const server = http.createServer((req, res) => res.end('ok'));
  await new Promise<void>(f => server.listen(0, '127.0.0.1', () => f()));
  const port = (server.address() as import('node:net').AddressInfo).port;
  const originalLookup = dns.promises.lookup;
  const originalOrder = dns.getDefaultResultOrder();
  // Emulate a host whose IPv6 address is not routable: connecting to it stalls.
  (dns.promises as any).lookup = async (hostname: string, options: dns.LookupOptions) => {
    if (hostname !== 'dual-stack.test')
      return originalLookup(hostname, options as dns.LookupAllOptions);
    return options.family === 6 ? [{ address: '203.0.113.1', family: 4 }] : [{ address: '127.0.0.1', family: 4 }];
  };
  dns.setDefaultResultOrder('ipv4first');
  try {
    const start = Date.now();
    const status = await new Promise<number | Error>(resolve => {
      utils.httpRequest({
        url: `http://dual-stack.test:${port}/`,
        socketTimeout: 30000,
      }, response => resolve(response.statusCode!), resolve);
    });
    expect(status).toBe(200);
    expect(Date.now() - start).toBeLessThan(2000);
  } finally {
    dns.setDefaultResultOrder(originalOrder);
    dns.promises.lookup = originalLookup;
    server.close();
  }
});