16
16
library ;
17
17
18
18
import 'dart:async' ;
19
- import 'dart:html ' ;
19
+ import 'dart:js_interop ' ;
20
20
21
21
import 'package:async/async.dart' ;
22
22
import 'package:grpc/src/client/call.dart' ;
@@ -26,12 +26,13 @@ import 'package:grpc/src/shared/status.dart';
26
26
import 'package:mockito/mockito.dart' ;
27
27
import 'package:stream_transform/stream_transform.dart' ;
28
28
import 'package:test/test.dart' ;
29
+ import 'package:web/web.dart' ;
29
30
30
31
final readyStateChangeEvent =
31
- Event ('readystatechange' , canBubble : false , cancelable: false );
32
+ Event ('readystatechange' , EventInit (bubbles : false , cancelable: false ) );
32
33
final progressEvent = ProgressEvent ('onloadstart' );
33
34
34
- class MockHttpRequest extends Mock implements HttpRequest {
35
+ class MockHttpRequest extends Mock implements IXMLHttpRequest {
35
36
MockHttpRequest ({int ? code}) : status = code ?? 200 ;
36
37
// ignore: close_sinks
37
38
StreamController <Event > readyStateChangeController =
@@ -52,6 +53,10 @@ class MockHttpRequest extends Mock implements HttpRequest {
52
53
@override
53
54
final int status;
54
55
56
+ // Some test code expects to call this
57
+ set readyState (int state);
58
+ set responseText (String text);
59
+
55
60
@override
56
61
int get readyState =>
57
62
super .noSuchMethod (Invocation .getter (#readyState), returnValue: - 1 );
@@ -71,7 +76,7 @@ class MockXhrClientConnection extends XhrClientConnection {
71
76
final int _statusCode;
72
77
73
78
@override
74
- HttpRequest createHttpRequest () {
79
+ IXMLHttpRequest createHttpRequest () {
75
80
final request = MockHttpRequest (code: _statusCode);
76
81
latestRequest = request;
77
82
return request;
@@ -208,8 +213,7 @@ void main() {
208
213
await stream.terminate ();
209
214
210
215
final expectedData = frame (data);
211
- expect (verify (connection.latestRequest.send (captureAny)).captured.single,
212
- expectedData);
216
+ verify (connection.latestRequest.send (expectedData.toJSBox));
213
217
});
214
218
215
219
test ('Stream handles headers properly' , () async {
@@ -226,15 +230,15 @@ void main() {
226
230
227
231
when (transport.latestRequest.responseHeaders).thenReturn (responseHeaders);
228
232
when (transport.latestRequest.response)
229
- .thenReturn (String .fromCharCodes (frame (< int > [])));
233
+ .thenReturn (String .fromCharCodes (frame (< int > [])).toJS );
230
234
231
235
// Set expectation for request readyState and generate two readyStateChange
232
236
// events, so that incomingMessages stream completes.
233
- final readyStates = [HttpRequest .HEADERS_RECEIVED , HttpRequest .DONE ];
234
- when (transport.latestRequest.readyState)
235
- .thenAnswer ((_) => readyStates.removeAt (0 ));
237
+ final readyStates = [XMLHttpRequest .HEADERS_RECEIVED , XMLHttpRequest .DONE ];
238
+ transport.latestRequest.readyState = readyStates[0 ];
236
239
transport.latestRequest.readyStateChangeController
237
240
.add (readyStateChangeEvent);
241
+ transport.latestRequest.readyState = readyStates[1 ];
238
242
transport.latestRequest.readyStateChangeController
239
243
.add (readyStateChangeEvent);
240
244
@@ -267,16 +271,15 @@ void main() {
267
271
final encodedString = String .fromCharCodes (encodedTrailers);
268
272
269
273
when (connection.latestRequest.responseHeaders).thenReturn (requestHeaders);
270
- when (connection.latestRequest.response).thenReturn (encodedString);
274
+ when (connection.latestRequest.response).thenReturn (encodedString.toJS );
271
275
272
276
// Set expectation for request readyState and generate events so that
273
277
// incomingMessages stream completes.
274
- final readyStates = [HttpRequest .HEADERS_RECEIVED , HttpRequest .DONE ];
275
- when (connection.latestRequest.readyState)
276
- .thenAnswer ((_) => readyStates.removeAt (0 ));
278
+ connection.latestRequest.readyState = XMLHttpRequest .HEADERS_RECEIVED ;
277
279
connection.latestRequest.readyStateChangeController
278
280
.add (readyStateChangeEvent);
279
281
connection.latestRequest.progressController.add (progressEvent);
282
+ connection.latestRequest.readyState = XMLHttpRequest .DONE ;
280
283
connection.latestRequest.readyStateChangeController
281
284
.add (readyStateChangeEvent);
282
285
@@ -303,16 +306,14 @@ void main() {
303
306
final encodedString = String .fromCharCodes (encoded);
304
307
305
308
when (connection.latestRequest.responseHeaders).thenReturn (requestHeaders);
306
- when (connection.latestRequest.response).thenReturn (encodedString);
307
-
309
+ when (connection.latestRequest.response).thenReturn (encodedString.toJS);
308
310
// Set expectation for request readyState and generate events so that
309
311
// incomingMessages stream completes.
310
- final readyStates = [HttpRequest .HEADERS_RECEIVED , HttpRequest .DONE ];
311
- when (connection.latestRequest.readyState)
312
- .thenAnswer ((_) => readyStates.removeAt (0 ));
312
+ connection.latestRequest.readyState = XMLHttpRequest .HEADERS_RECEIVED ;
313
313
connection.latestRequest.readyStateChangeController
314
314
.add (readyStateChangeEvent);
315
315
connection.latestRequest.progressController.add (progressEvent);
316
+ connection.latestRequest.readyState = XMLHttpRequest .DONE ;
316
317
connection.latestRequest.readyStateChangeController
317
318
.add (readyStateChangeEvent);
318
319
@@ -338,16 +339,15 @@ void main() {
338
339
final data = List <int >.filled (10 , 224 );
339
340
when (connection.latestRequest.responseHeaders).thenReturn (requestHeaders);
340
341
when (connection.latestRequest.response)
341
- .thenReturn (String .fromCharCodes (frame (data)));
342
+ .thenReturn (String .fromCharCodes (frame (data)).toJS );
342
343
343
344
// Set expectation for request readyState and generate events, so that
344
345
// incomingMessages stream completes.
345
- final readyStates = [HttpRequest .HEADERS_RECEIVED , HttpRequest .DONE ];
346
- when (connection.latestRequest.readyState)
347
- .thenAnswer ((_) => readyStates.removeAt (0 ));
346
+ connection.latestRequest.readyState = XMLHttpRequest .HEADERS_RECEIVED ;
348
347
connection.latestRequest.readyStateChangeController
349
348
.add (readyStateChangeEvent);
350
349
connection.latestRequest.progressController.add (progressEvent);
350
+ connection.latestRequest.readyState = XMLHttpRequest .DONE ;
351
351
connection.latestRequest.readyStateChangeController
352
352
.add (readyStateChangeEvent);
353
353
@@ -369,8 +369,8 @@ void main() {
369
369
const errorDetails = 'error details' ;
370
370
when (connection.latestRequest.responseHeaders)
371
371
.thenReturn ({'content-type' : 'application/grpc+proto' });
372
- when ( connection.latestRequest.readyState). thenReturn ( HttpRequest . DONE ) ;
373
- when ( connection.latestRequest.responseText). thenReturn ( errorDetails) ;
372
+ connection.latestRequest.readyState = XMLHttpRequest . DONE ;
373
+ connection.latestRequest.responseText = errorDetails;
374
374
connection.latestRequest.readyStateChangeController
375
375
.add (readyStateChangeEvent);
376
376
await errorReceived.future;
@@ -398,20 +398,20 @@ void main() {
398
398
399
399
when (connection.latestRequest.responseHeaders).thenReturn (metadata);
400
400
when (connection.latestRequest.readyState)
401
- .thenReturn (HttpRequest .HEADERS_RECEIVED );
401
+ .thenReturn (XMLHttpRequest .HEADERS_RECEIVED );
402
402
403
403
// At first invocation the response should be the the first message, after
404
404
// that first + last messages.
405
405
var first = true ;
406
406
when (connection.latestRequest.response).thenAnswer ((_) {
407
407
if (first) {
408
408
first = false ;
409
- return encodedStrings[0 ];
409
+ return encodedStrings[0 ].toJS ;
410
410
}
411
- return encodedStrings[0 ] + encodedStrings[1 ];
411
+ return ( encodedStrings[0 ] + encodedStrings[1 ]).toJS ;
412
412
});
413
413
414
- final readyStates = [HttpRequest .HEADERS_RECEIVED , HttpRequest .DONE ];
414
+ final readyStates = [XMLHttpRequest .HEADERS_RECEIVED , XMLHttpRequest .DONE ];
415
415
when (connection.latestRequest.readyState)
416
416
.thenAnswer ((_) => readyStates.removeAt (0 ));
417
417
0 commit comments