Skip to content

Commit

Permalink
test: Relic server test description improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
klkucaj committed Jan 8, 2025
1 parent 641869c commit d20003e
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions test/relic_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,46 @@ void main() {

tearDown(() => server.close());

test('serves HTTP requests with the mounted handler', () async {
server.mountAndStart(syncHandler);
expect(await http.read(server.url), equals('Hello from /'));
});

test('Handles malformed requests gracefully.', () async {
server.mountAndStart(syncHandler);
final rs = await http
.get(Uri.parse('${server.url}/%D0%C2%BD%A8%CE%C4%BC%FE%BC%D0.zip'));
expect(rs.statusCode, 400);
expect(rs.body, 'Bad Request');
});
group('Given a server', () {
test(
'when a valid HTTP request is made '
'then it serves the request using the mounted handler', () async {
server.mountAndStart(syncHandler);
final response = await http.read(server.url);
expect(response, equals('Hello from /'));
});

test('delays HTTP requests until a handler is mounted', () async {
expect(http.read(server.url), completion(equals('Hello from /')));
await Future<void>.delayed(Duration.zero);
test(
'when a malformed HTTP request is made '
'then it returns a 400 Bad Request response', () async {
server.mountAndStart(syncHandler);
final rs = await http
.get(Uri.parse('${server.url}/%D0%C2%BD%A8%CE%C4%BC%FE%BC%D0.zip'));
expect(rs.statusCode, 400);
expect(rs.body, 'Bad Request');
});

server.mountAndStart(asyncHandler);
});
test(
'when no handler is mounted initially '
'then it delays requests until a handler is mounted', () async {
final delayedResponse = http.read(server.url);
await Future<void>.delayed(Duration.zero);
server.mountAndStart(asyncHandler);
expect(delayedResponse, completion(equals('Hello from /')));
});

test('disallows more than one handler from being mounted', () async {
server.mountAndStart((_) => throw UnimplementedError());
expect(
() => server.mountAndStart((_) => throw UnimplementedError()),
throwsStateError,
);
expect(
() => server.mountAndStart((_) => throw UnimplementedError()),
throwsStateError,
);
test(
'when a handler is already mounted '
'then mounting another handler throws a StateError', () async {
server.mountAndStart((_) => throw UnimplementedError());
expect(
() => server.mountAndStart((_) => throw UnimplementedError()),
throwsStateError,
);
expect(
() => server.mountAndStart((_) => throw UnimplementedError()),
throwsStateError,
);
});
});
}

0 comments on commit d20003e

Please sign in to comment.