0

I'm trying to write tests for my shelf server. And it returns 503 when I make a request in my unit tests, even though the server is started in my test setup. I also tested the route I make a call to in my unit test, in my browser, in the duration that unit test runs. And it works. But in my unit test, I get 503.

Is there any way I can test my shelf server? Any alternative solutions will be appreaciated.

void main() {
  late WebServer server;
  setUp(() async {
    server = WebServer();
    await server.run();
  });

  tearDown(() async {
    await server.stop(force: true);
  });

  test('calling /games return hardcoded text', () async {
    final uri = 'http://localhost:8081/games/1';
    print(Uri.parse(uri));
    final response = await http.get(Uri.parse(uri));
    expect(response.statusCode, HttpStatus.ok);
    expect(response.body, 'creating a new game');
  });
}

And here is the code for my server

class WebServer {
  late HttpServer _httpServer;

  Future<HttpServer> run() async {
    final shelfRouter = setupRoutes();
    _httpServer = await io.serve(shelfRouter, 'localhost', 8081);
    return _httpServer;
  }

  Future<void> stop({bool force = true}) => _httpServer.close(force: force);
}

here is the error I get

Expected: <200>
  Actual: <503>

package:matcher                 expect
test/web_server_test.dart 22:5  main.<fn>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
alireza easazade
  • 3,324
  • 4
  • 27
  • 35
  • It would be helpful if you could share the code for the `games/` route in `setupRoutes()`. And if possible a description or the code of the games route handler. The error which is causing the 503 could be in one of these two classes. – jxstxn __ Jul 27 '23 at 08:58
  • 1
    the problem was i was using v2ray. which is sort of a VPN. that caused it. I turned it off for tests and it worked fine – alireza easazade Jul 27 '23 at 13:16

0 Answers0