diff --git a/pkgs/cronet_http/lib/src/cronet_client.dart b/pkgs/cronet_http/lib/src/cronet_client.dart index ec6258affd..151d24e0b9 100644 --- a/pkgs/cronet_http/lib/src/cronet_client.dart +++ b/pkgs/cronet_http/lib/src/cronet_client.dart @@ -163,13 +163,23 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks( case final contentLengthHeader?: contentLength = int.parse(contentLengthHeader); } + + final statusText = + responseInfo.getHttpStatusText().toDartString(releaseOriginal: true); + if (statusText.isEmpty) { + // responseInfo.getHttpStatusText() will be empty for any failure to + // parse the Status-Line but responseInfo.getHttpStatusCode() will + // return 200. + responseCompleter.completeError(ClientException( + 'Invalid Status-Line', + request.url, + )); + } responseCompleter.complete(StreamedResponse( responseStream!.stream, responseInfo.getHttpStatusCode(), contentLength: contentLength, - reasonPhrase: responseInfo - .getHttpStatusText() - .toDartString(releaseOriginal: true), + reasonPhrase: statusText, request: request, isRedirect: false, headers: responseHeaders, diff --git a/pkgs/http_client_conformance_tests/lib/src/response_status_line_tests.dart b/pkgs/http_client_conformance_tests/lib/src/response_status_line_tests.dart index 922236ccc1..48fd99b5fe 100644 --- a/pkgs/http_client_conformance_tests/lib/src/response_status_line_tests.dart +++ b/pkgs/http_client_conformance_tests/lib/src/response_status_line_tests.dart @@ -23,6 +23,17 @@ void testResponseStatusLine(Client client) async { host = 'localhost:${await httpServerQueue.next}'; }); + test( + 'without HTTP version', + () async { + httpServerChannel.sink.add('200 OK'); + await expectLater( + client.get(Uri.http(host, '')), + throwsA(isA()), + ); + }, + ); + test( 'without status code', () async { @@ -32,8 +43,17 @@ void testResponseStatusLine(Client client) async { throwsA(isA()), ); }, - skip: - 'Enable after https://github.com/dart-lang/http/issues/1013 is fixed', + ); + + test( + 'without reason phrase', + () async { + httpServerChannel.sink.add('HTTP/1.1 200'); + await expectLater( + client.get(Uri.http(host, '')), + throwsA(isA()), + ); + }, ); }); }