Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

testing - How to set up tests for a Dart HTTP server

I'm trying to build a Dart HTTP server and I want to test the API. I'm not able to set up the tests, though.

Here is what I have so far in my_server_test.dart:

import 'dart:io';

import 'package:my_server/my_server.dart';
import 'package:test/test.dart';

void main() {
  HttpServer server;
  setUp(() async {
    final server = await createServer();
    await handleRequests(server);
  });

  tearDown(() async {
    await server.close(force: true);
    server = null;
  });

  test('First try', () async {
    
    final client = HttpClient();
    final request = await client.get(InternetAddress.loopbackIPv4.host, 4040, '/');
    final response = await request.close();
    print(response);
    
  });
}

And here is the server code in my_server.dart:

import 'dart:io';

import 'package:hundetgel_server/routes/handle_get.dart';

Future<HttpServer> createServer() async {
  final address = InternetAddress.loopbackIPv4;
  const port = 4040;
  return await HttpServer.bind(address, port);
}

Future<void> handleRequests(HttpServer server) async {
  await for (HttpRequest request in server) {
    switch (request.method) {
      case 'GET':
        handleGet(request);
        break;
      default:
        handleDefault(request);
    }
  }
}

void handleGet(HttpRequest request) {
  request.response
    ..write('Hello')
    ..close();
}

void handleDefault(HttpRequest request) {
  request.response
    ..statusCode = HttpStatus.methodNotAllowed
    ..write('Unsupported request: ${request.method}.')
    ..close();
}

When I run the test I just get a timeout:

TimeoutException after 0:00:30.000000: Test timed out after 30 seconds. See https://pub.dev/packages/test#timeouts
dart:isolate  _RawReceivePortImpl._handleMessage
NoSuchMethodError: The method 'close' was called on null.
Receiver: null
Tried calling: close(force: true)
dart:core                              Object.noSuchMethod
2
main.<fn>
test/my_server_test.dart:15
===== asynchronous gap ===========================
dart:async                             _completeOnAsyncError
test/my_server_test.dart        main.<fn>
test/my_server_test.dart:1
main.<fn>
test/my_server_test.dart:14
2

? First try
Exited (1)

How do I set up the server so I can start testing it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems that you are await handleRequests in your setUp. Your handleRequests is a forever loop waiting for incoming requests. So it never halts. So you setup never finishes. That is the problem.

Thus, try to change

await handleRequests(server);

to

handleRequests(server); // NO await

Thus the handleRequests will run in the "background".

EDIT about the null exception of server variable:

change

final server = await createServer();

to

server = await createServer();

because the old code shadows the outside server variable - that variable is never assigned a value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...