Skip to content

Commit 490d6a0

Browse files
authored
Migrate asset_handler_test to null-safety (#1710)
1 parent b1a2742 commit 490d6a0

File tree

4 files changed

+29
-47
lines changed

4 files changed

+29
-47
lines changed

dwds/lib/src/dwds_vm_client.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final _logger = Logger('DwdsVmClient');
2222
class DwdsVmClient {
2323
final VmService client;
2424
final StreamController<Map<String, Object>> _requestController;
25-
final StreamController<Map<String, Object>> _responseController;
25+
final StreamController<Map<String, Object?>> _responseController;
2626

2727
static const int kFeatureDisabled = 100;
2828
static const String kFeatureDisabledMessage = 'Feature is disabled.';
@@ -44,7 +44,7 @@ class DwdsVmClient {
4444
DebugService debugService, DwdsStats dwdsStats) async {
4545
// Set up hot restart as an extension.
4646
final requestController = StreamController<Map<String, Object>>();
47-
final responseController = StreamController<Map<String, Object>>();
47+
final responseController = StreamController<Map<String, Object?>>();
4848
VmServerConnection(requestController.stream, responseController.sink,
4949
debugService.serviceExtensionRegistry, debugService.chromeProxyService);
5050
final client =
@@ -55,7 +55,7 @@ class DwdsVmClient {
5555
'$request');
5656
return;
5757
}
58-
requestController.sink.add(jsonDecode(request) as Map<String, Object>);
58+
requestController.sink.add(Map<String, Object>.from(jsonDecode(request)));
5959
});
6060
final chromeProxyService =
6161
debugService.chromeProxyService as ChromeProxyService;

dwds/lib/src/services/debug_service.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ int _clientsConnected = 0;
3333

3434
Logger _logger = Logger('DebugService');
3535

36-
void Function(WebSocketChannel, String) _createNewConnectionHandler(
36+
void Function(WebSocketChannel) _createNewConnectionHandler(
3737
ChromeProxyService chromeProxyService,
3838
ServiceExtensionRegistry serviceExtensionRegistry, {
39-
void Function(Map<String, dynamic>)? onRequest,
40-
void Function(Map<String, dynamic>)? onResponse,
39+
void Function(Map<String, Object>)? onRequest,
40+
void Function(Map<String, Object?>)? onResponse,
4141
}) {
42-
return (webSocket, protocol) {
43-
final responseController = StreamController<Map<String, Object>>();
42+
return (webSocket) {
43+
final responseController = StreamController<Map<String, Object?>>();
4444
webSocket.sink.addStream(responseController.stream.map((response) {
4545
if (onResponse != null) onResponse(response);
4646
return jsonEncode(response);
@@ -53,7 +53,7 @@ void Function(WebSocketChannel, String) _createNewConnectionHandler(
5353
'Got value with unexpected type ${value.runtimeType} from web '
5454
'socket, expected a List<int> or String.');
5555
}
56-
final request = jsonDecode(value as String) as Map<String, Object>;
56+
final request = Map<String, Object>.from(jsonDecode(value));
5757
if (onRequest != null) onRequest(request);
5858
return request;
5959
});
@@ -76,12 +76,12 @@ Future<void> _handleSseConnections(
7676
SseHandler handler,
7777
ChromeProxyService chromeProxyService,
7878
ServiceExtensionRegistry serviceExtensionRegistry, {
79-
void Function(Map<String, dynamic>)? onRequest,
80-
void Function(Map<String, dynamic>)? onResponse,
79+
void Function(Map<String, Object>)? onRequest,
80+
void Function(Map<String, Object?>)? onResponse,
8181
}) async {
8282
while (await handler.connections.hasNext) {
8383
final connection = await handler.connections.next;
84-
final responseController = StreamController<Map<String, Object>>();
84+
final responseController = StreamController<Map<String, Object?>>();
8585
final sub = responseController.stream.map((response) {
8686
if (onResponse != null) onResponse(response);
8787
return jsonEncode(response);
@@ -214,8 +214,8 @@ class DebugService {
214214
LoadStrategy loadStrategy,
215215
AppConnection appConnection,
216216
UrlEncoder? urlEncoder, {
217-
void Function(Map<String, dynamic>)? onRequest,
218-
void Function(Map<String, dynamic>)? onResponse,
217+
void Function(Map<String, Object>)? onRequest,
218+
void Function(Map<String, Object?>)? onResponse,
219219
bool spawnDds = true,
220220
bool useSse = false,
221221
ExpressionCompiler? expressionCompiler,

dwds/test/fixtures/context.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class TestContext {
7878
WebkitDebugger get webkitDebugger => _webkitDebugger!;
7979
late WebkitDebugger? _webkitDebugger;
8080

81+
Handler get assetHandler => _assetHandler!;
82+
late Handler? _assetHandler;
83+
8184
Client get client => _client!;
8285
late Client? _client;
8386

@@ -203,7 +206,6 @@ class TestContext {
203206

204207
ExpressionCompiler? expressionCompiler;
205208
AssetReader assetReader;
206-
Handler assetHandler;
207209
Stream<BuildResults> buildResults;
208210
RequireStrategy requireStrategy;
209211
String basePath = '';
@@ -237,7 +239,7 @@ class TestContext {
237239
.timeout(const Duration(seconds: 60));
238240

239241
final assetServerPort = daemonPort(workingDirectory);
240-
assetHandler = proxyHandler(
242+
_assetHandler = proxyHandler(
241243
'http://localhost:$assetServerPort/$pathToServe/',
242244
client: client);
243245
assetReader =
@@ -291,7 +293,7 @@ class TestContext {
291293

292294
basePath = webRunner.devFS.assetServer.basePath;
293295
assetReader = webRunner.devFS.assetServer;
294-
assetHandler = webRunner.devFS.assetServer.handleRequest;
296+
_assetHandler = webRunner.devFS.assetServer.handleRequest;
295297

296298
requireStrategy = FrontendServerRequireStrategyProvider(
297299
reloadConfiguration, assetReader, () async => {}, basePath)
@@ -353,11 +355,14 @@ class TestContext {
353355
: 'http://localhost:$port/$basePath/$path';
354356

355357
await _webDriver?.get(appUrl);
356-
final tab = await (connection.getTab((t) => t.url == appUrl)
357-
as FutureOr<ChromeTab>);
358-
_tabConnection = await tab.connect();
359-
await tabConnection.runtime.enable();
360-
await tabConnection.debugger.enable();
358+
final tab = await connection.getTab((t) => t.url == appUrl);
359+
if (tab != null) {
360+
_tabConnection = await tab.connect();
361+
await tabConnection.runtime.enable();
362+
await tabConnection.debugger.enable();
363+
} else {
364+
throw StateError('Unable to connect to tab.');
365+
}
361366

362367
if (enableDebugExtension) {
363368
final extensionTab = await _fetchDartDebugExtensionTab(connection);

dwds/test/handlers/asset_handler_test.dart

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,41 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
7-
import 'dart:io';
8-
9-
import 'package:http/http.dart' as http;
10-
import 'package:http/io_client.dart';
115
import 'package:shelf/shelf.dart';
12-
import 'package:shelf_proxy/shelf_proxy.dart';
136
import 'package:test/test.dart';
147

158
import '../fixtures/context.dart';
169
import '../fixtures/logging.dart';
17-
import '../fixtures/utilities.dart';
1810

1911
void main() {
2012
group('Asset handler', () {
2113
final context = TestContext();
22-
Handler assetHandler;
23-
http.Client client;
2414

2515
setUpAll(() async {
2616
setCurrentLogWriter();
2717
await context.setUp(
2818
enableExpressionEvaluation: true,
2919
verboseCompiler: false,
3020
);
31-
32-
client = IOClient(HttpClient()
33-
..maxConnectionsPerHost = 200
34-
..idleTimeout = const Duration(seconds: 30)
35-
..connectionTimeout = const Duration(seconds: 30));
36-
37-
final assetServerPort = daemonPort(context.workingDirectory);
38-
final pathToServe = context.pathToServe;
39-
40-
assetHandler = proxyHandler(
41-
'http://localhost:$assetServerPort/$pathToServe/',
42-
client: client);
4321
});
4422

4523
tearDownAll(() async {
46-
client.close();
4724
await context.tearDown();
4825
});
4926

5027
setUp(setCurrentLogWriter);
5128

5229
Future<void> readAsString(String path) async {
5330
final request = Request('GET', Uri.parse('http://foo:0000/$path'));
54-
final response = await assetHandler(request);
31+
final response = await context.assetHandler(request);
5532
final result = await response.readAsString();
5633
expect(result, isNotNull,
5734
reason: 'Failed to read $path: ${response.statusCode}');
5835
}
5936

6037
Future<void> readAsBytes(String path) async {
6138
final request = Request('GET', Uri.parse('http://foo:0000/$path'));
62-
final response = await assetHandler(request);
39+
final response = await context.assetHandler(request);
6340
final result = await response.read().toList();
6441
expect(result, isNotNull,
6542
reason: 'Failed to read $path: ${response.statusCode}');

0 commit comments

Comments
 (0)