From a18137288c4b5bd5468deba42e4e72c82b3578bb Mon Sep 17 00:00:00 2001 From: Aran Donohue Date: Wed, 18 Dec 2024 10:00:50 -0800 Subject: [PATCH] fixup tests --- lib/src/client/transport/xhr_transport.dart | 11 +++-- .../client_xhr_transport_test.dart | 47 +++++++++---------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/src/client/transport/xhr_transport.dart b/lib/src/client/transport/xhr_transport.dart index cb6af6d0..693088c2 100644 --- a/lib/src/client/transport/xhr_transport.dart +++ b/lib/src/client/transport/xhr_transport.dart @@ -31,7 +31,7 @@ import 'web_streams.dart'; const _contentTypeKey = 'Content-Type'; class XhrTransportStream implements GrpcTransportStream { - final XMLHttpRequest _request; + final IXMLHttpRequest _request; final ErrorHandler _onError; final Function(XhrTransportStream stream) _onDone; bool _headersReceived = false; @@ -163,6 +163,7 @@ abstract interface class IXMLHttpRequest { set responseType(String responseType); set withCredentials(bool withCredentials); + void abort(); void open( String method, String url, [ @@ -212,6 +213,11 @@ class XMLHttpRequestImpl implements IXMLHttpRequest { _xhr.withCredentials = withCredentials; } + @override + void abort() { + _xhr.abort(); + } + @override void open( String method, @@ -301,8 +307,7 @@ class XhrClientConnection implements ClientConnection { XhrTransportStream _createXhrTransportStream(IXMLHttpRequest request, ErrorHandler onError, void Function(XhrTransportStream stream) onDone) { - return XhrTransportStream(request.toXMLHttpRequest(), - onError: onError, onDone: onDone); + return XhrTransportStream(request, onError: onError, onDone: onDone); } void _removeStream(XhrTransportStream stream) { diff --git a/test/client_tests/client_xhr_transport_test.dart b/test/client_tests/client_xhr_transport_test.dart index 31fe48d2..f8758ae1 100644 --- a/test/client_tests/client_xhr_transport_test.dart +++ b/test/client_tests/client_xhr_transport_test.dart @@ -17,7 +17,7 @@ library; import 'dart:async'; import 'dart:js_interop'; - +import 'dart:typed_data'; import 'package:async/async.dart'; import 'package:grpc/src/client/call.dart'; import 'package:grpc/src/client/transport/xhr_transport.dart'; @@ -53,9 +53,9 @@ class MockHttpRequest extends Mock implements IXMLHttpRequest { @override final int status; - // Some test code expects to call this - set readyState(int state); - set responseText(String text); + @override + String get responseText => + super.noSuchMethod(Invocation.getter(#responseText), returnValue: ''); @override int get readyState => @@ -213,7 +213,7 @@ void main() { await stream.terminate(); final expectedData = frame(data); - verify(connection.latestRequest.send(expectedData.toJSBox)); + verify(connection.latestRequest.send(Uint8List.fromList(expectedData).toJS)); }); test('Stream handles headers properly', () async { @@ -229,16 +229,15 @@ void main() { (error, _) => fail(error.toString())); when(transport.latestRequest.responseHeaders).thenReturn(responseHeaders); - when(transport.latestRequest.response) - .thenReturn(String.fromCharCodes(frame([])).toJS); + when(transport.latestRequest.responseText) + .thenReturn(String.fromCharCodes(frame([]))); // Set expectation for request readyState and generate two readyStateChange // events, so that incomingMessages stream completes. final readyStates = [XMLHttpRequest.HEADERS_RECEIVED, XMLHttpRequest.DONE]; - transport.latestRequest.readyState = readyStates[0]; + when(transport.latestRequest.readyState).thenReturnInOrder(readyStates); transport.latestRequest.readyStateChangeController .add(readyStateChangeEvent); - transport.latestRequest.readyState = readyStates[1]; transport.latestRequest.readyStateChangeController .add(readyStateChangeEvent); @@ -271,15 +270,15 @@ void main() { final encodedString = String.fromCharCodes(encodedTrailers); when(connection.latestRequest.responseHeaders).thenReturn(requestHeaders); - when(connection.latestRequest.response).thenReturn(encodedString.toJS); + when(connection.latestRequest.responseText).thenReturn(encodedString); // Set expectation for request readyState and generate events so that // incomingMessages stream completes. - connection.latestRequest.readyState = XMLHttpRequest.HEADERS_RECEIVED; + when(connection.latestRequest.readyState).thenReturnInOrder( + [XMLHttpRequest.HEADERS_RECEIVED, XMLHttpRequest.DONE]); connection.latestRequest.readyStateChangeController .add(readyStateChangeEvent); connection.latestRequest.progressController.add(progressEvent); - connection.latestRequest.readyState = XMLHttpRequest.DONE; connection.latestRequest.readyStateChangeController .add(readyStateChangeEvent); @@ -306,14 +305,14 @@ void main() { final encodedString = String.fromCharCodes(encoded); when(connection.latestRequest.responseHeaders).thenReturn(requestHeaders); - when(connection.latestRequest.response).thenReturn(encodedString.toJS); + when(connection.latestRequest.responseText).thenReturn(encodedString); // Set expectation for request readyState and generate events so that // incomingMessages stream completes. - connection.latestRequest.readyState = XMLHttpRequest.HEADERS_RECEIVED; + when(connection.latestRequest.readyState).thenReturnInOrder( + [XMLHttpRequest.HEADERS_RECEIVED, XMLHttpRequest.DONE]); connection.latestRequest.readyStateChangeController .add(readyStateChangeEvent); connection.latestRequest.progressController.add(progressEvent); - connection.latestRequest.readyState = XMLHttpRequest.DONE; connection.latestRequest.readyStateChangeController .add(readyStateChangeEvent); @@ -338,16 +337,16 @@ void main() { requestHeaders, (error, _) => fail(error.toString())); final data = List.filled(10, 224); when(connection.latestRequest.responseHeaders).thenReturn(requestHeaders); - when(connection.latestRequest.response) - .thenReturn(String.fromCharCodes(frame(data)).toJS); + when(connection.latestRequest.responseText) + .thenReturn(String.fromCharCodes(frame(data))); // Set expectation for request readyState and generate events, so that // incomingMessages stream completes. - connection.latestRequest.readyState = XMLHttpRequest.HEADERS_RECEIVED; + when(connection.latestRequest.readyState).thenReturnInOrder( + [XMLHttpRequest.HEADERS_RECEIVED, XMLHttpRequest.DONE]); connection.latestRequest.readyStateChangeController .add(readyStateChangeEvent); connection.latestRequest.progressController.add(progressEvent); - connection.latestRequest.readyState = XMLHttpRequest.DONE; connection.latestRequest.readyStateChangeController .add(readyStateChangeEvent); @@ -369,8 +368,8 @@ void main() { const errorDetails = 'error details'; when(connection.latestRequest.responseHeaders) .thenReturn({'content-type': 'application/grpc+proto'}); - connection.latestRequest.readyState = XMLHttpRequest.DONE; - connection.latestRequest.responseText = errorDetails; + when(connection.latestRequest.readyState).thenReturn(XMLHttpRequest.DONE); + when(connection.latestRequest.responseText).thenReturn(errorDetails); connection.latestRequest.readyStateChangeController .add(readyStateChangeEvent); await errorReceived.future; @@ -403,12 +402,12 @@ void main() { // At first invocation the response should be the the first message, after // that first + last messages. var first = true; - when(connection.latestRequest.response).thenAnswer((_) { + when(connection.latestRequest.responseText).thenAnswer((_) { if (first) { first = false; - return encodedStrings[0].toJS; + return encodedStrings[0]; } - return (encodedStrings[0] + encodedStrings[1]).toJS; + return encodedStrings[0] + encodedStrings[1]; }); final readyStates = [XMLHttpRequest.HEADERS_RECEIVED, XMLHttpRequest.DONE];