Skip to content

Commit

Permalink
fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aran committed Dec 18, 2024
1 parent a7cf974 commit a181372
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
11 changes: 8 additions & 3 deletions lib/src/client/transport/xhr_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -163,6 +163,7 @@ abstract interface class IXMLHttpRequest {
set responseType(String responseType);
set withCredentials(bool withCredentials);

void abort();
void open(
String method,
String url, [
Expand Down Expand Up @@ -212,6 +213,11 @@ class XMLHttpRequestImpl implements IXMLHttpRequest {
_xhr.withCredentials = withCredentials;
}

@override
void abort() {
_xhr.abort();
}

@override
void open(
String method,
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 23 additions & 24 deletions test/client_tests/client_xhr_transport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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 {
Expand All @@ -229,16 +229,15 @@ void main() {
(error, _) => fail(error.toString()));

when(transport.latestRequest.responseHeaders).thenReturn(responseHeaders);
when(transport.latestRequest.response)
.thenReturn(String.fromCharCodes(frame(<int>[])).toJS);
when(transport.latestRequest.responseText)
.thenReturn(String.fromCharCodes(frame(<int>[])));

// 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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -338,16 +337,16 @@ void main() {
requestHeaders, (error, _) => fail(error.toString()));
final data = List<int>.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);

Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit a181372

Please sign in to comment.