Skip to content

Dart's "server-shelf" template tests won't work on Windows our of the box #447

Open
@miquelbeltran

Description

@miquelbeltran

Hello, I am not sure if this issue should go to the Dart SDK repository, since I don't know who is in charge of updating the templates, please feel free to move it if that's the case.

When creating a sample project for flutter/samples (flutter/samples#2446), we used a Dart shelf server for our example, and we found that the tests weren't working on the Windows CI job.

The issue is that the created test/server_test.dart uses the IP 0.0.0.0 to connect to the local server in order to test the endpoints, and Windows doesn't seem to like that.

e.g.

  final port = '8080';
  final host = 'http://0.0.0.0:$port';
  late Process p;

  setUp(() async {
    p = await Process.start(
      'dart',
      ['run', 'bin/server.dart'],
      environment: {'PORT': port},
    );
    // Wait for server to start and print to stdout.
    await p.stdout.first;
  });

  tearDown(() => p.kill());

  test('Root', () async {
    final response = await get(Uri.parse('$host/'));
    expect(response.statusCode, 200);
    expect(response.body, 'Hello, World!\n');
  });

This can be reproduced with the following steps:

  1. Use a Windows machine (this was done on Windows 11)
  2. Create a new Dart project with Shelf using the template: dart create -t server-shelf .
  3. Run tests with dart test

You will get the following errors:

Building package executable... (7.5s)
Built test:test.
00:01 +0 -1: test\server_test.dart: Root [E]
  ClientException with SocketException: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
   (OS Error: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
  , errno = 10057), address = 0.0.0.0, port = 65397, uri=http://0.0.0.0:8080/
  package:http/src/io_client.dart 154:7    IOClient.send
  ===== asynchronous gap ===========================
  package:http/src/base_client.dart 93:32  BaseClient._sendUnstreamed
  ===== asynchronous gap ===========================
  package:http/http.dart 167:12            _withClient
  ===== asynchronous gap ===========================
  test\server_test.dart 24:22              main.<fn>

This can be solved by changing 0.0.0.0 to 127.0.0.1.

I think it would be good if the template worked out of the box without having to do changes, otherwise it may create confusion to new developers. Specially important since newbies may be more likely to use Windows than more experienced devs.

My setup:

dart --version
Dart SDK version: 3.5.3 (stable) (Wed Sep 11 16:22:47 2024 +0000) on "windows_x64"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions