dart - HttpClientRequest failing on adding data

2019-08-30 07:03发布

问题:

I'm trying to create some simple unittests, here's the code:

import 'dart:io';

import 'package:unittest/unittest.dart';

void main() {
  test('check if sent data equals expected data', () {

    HttpServer.bind('127.0.0.1', 8080)
      .then(expectAsync1((HttpServer server) {
        server.listen((HttpRequest request) {
          expect(request.runtimeType.toString(), equals('_HttpRequest'));
          request.fold(new BytesBuilder(), (prev, next) {
            prev.addAll(next);
            return prev;
          })
          .then(expectAsync1((BytesBuilder bb) {
            var bytes = bb.takeBytes(),
                helloString = new String.fromCharCodes(bytes);
            expect(helloString, equals('hello'));
            request.response.write(helloString);
            request.response.close();
            server.close();
          }));
        });
      }));

    HttpClient client = new HttpClient();
    client.get('127.0.0.1', 8080, '/')
      .then((HttpClientRequest request) {
        request.add('hello'.codeUnits);
        return request.close();
      });

  });
}

Gives me an error: Breaking on exception: 'dart:async/stream_impl.dart': Failed assertion: line 149 pos 12: '! _isClosed' is not true.

After some workaround, I found out that the responsible one is request.add('hello'.codeUnits)

So now I have no way to add data to HttpClientRequest. Any workaround?

回答1:

What version of Dart are you using? I have not been able to reproduce the error you get. See http://dartbug.com/13293 for progress on this issue.

However there are a number of issues with the code:

  1. BytesBuilder has no addAll method
  2. When using client.get the content length of the request is set to 0
  3. Seems that the client might connect before the server is bound.

Rewriting the test like below makes it run:

import 'dart:io';
import 'package:unittest/unittest.dart';

void main() {
  test('check if sent data equals expected data', () {
    expect(HttpServer.bind('127.0.0.1', 8081).then((HttpServer server) {
      HttpClient client = new HttpClient();
      server.listen((HttpRequest request) {
        expect(request.runtimeType.toString(), equals('_HttpRequest'));
        request.fold(new BytesBuilder(), (prev, next) {
          prev.add(next);
          return prev;
        }).then((BytesBuilder bb) {
          var bytes = bb.takeBytes();
          var helloString = new String.fromCharCodes(bytes);
          expect(helloString, equals('hello'));
          request.response.write(helloString);
          request.response.close();
          server.close();
        });
      });

      return client.post('127.0.0.1', 8081, '/').then((HttpClientRequest request) {
        request.add('hello'.codeUnits);
        return request.close().then((HttpClientRequest response) {
          return response.statusCode;
        });
      }).whenComplete(client.close());
    }), completion(equals(HttpStatus.OK)));
  });
}


标签: dart