Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Jan 9, 2024
1 parent 83b360b commit 40d5722
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkgs/http/lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export 'src/multipart_request.dart';
export 'src/request.dart';
export 'src/response.dart';
export 'src/streamed_request.dart';
export 'src/streamed_response.dart';
export 'src/streamed_response.dart' hide StreamedResponseV2;

/// Sends an HTTP HEAD request with the given headers to the given URL.
///
Expand Down
26 changes: 19 additions & 7 deletions pkgs/http/lib/src/base_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,25 @@ abstract class BaseRequest {
try {
var response = await client.send(this);
var stream = onDone(response.stream, client.close);
return StreamedResponse(ByteStream(stream), response.statusCode,
contentLength: response.contentLength,
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);

if (response is BaseResponseV2) {
return StreamedResponseV2(ByteStream(stream), response.statusCode,
contentLength: response.contentLength,
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
url: (response as BaseResponseV2).url,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
} else {
return StreamedResponse(ByteStream(stream), response.statusCode,
contentLength: response.contentLength,
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
}
} catch (_) {
client.close();
rethrow;
Expand Down
13 changes: 1 addition & 12 deletions pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ BaseClient createClient() {
return BrowserClient();
}

class _StreamedResponseV2 extends StreamedResponse with BaseResponseV2 {
@override
final Uri? url;
_StreamedResponseV2(super.stream, super.statusCode,
{super.contentLength,
super.request,
super.headers,
this.url,
super.reasonPhrase});
}

/// A `package:web`-based HTTP client that runs in the browser and is backed by
/// [XMLHttpRequest].
///
Expand Down Expand Up @@ -93,7 +82,7 @@ class BrowserClient extends BaseClient {
var body = (xhr.response as JSArrayBuffer).toDart.asUint8List();
var responseUrl = xhr.responseURL;
var url = responseUrl.isNotEmpty ? Uri.parse(responseUrl) : null;
completer.complete(_StreamedResponseV2(
completer.complete(StreamedResponseV2(
ByteStream.fromBytes(body), xhr.status,
contentLength: body.length,
request: request,
Expand Down
16 changes: 16 additions & 0 deletions pkgs/http/lib/src/streamed_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ class StreamedResponse extends BaseResponse {
super.reasonPhrase})
: stream = toByteStream(stream);
}

/// This class is private to `package:http` and will be removed when
/// `package:http` v2 is released.
class StreamedResponseV2 extends StreamedResponse with BaseResponseV2 {
@override
final Uri? url;

StreamedResponseV2(super.stream, super.statusCode,
{super.contentLength,
super.request,
super.headers,
super.isRedirect,
this.url,
super.persistentConnection,
super.reasonPhrase});
}
6 changes: 3 additions & 3 deletions pkgs/http/test/io/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ void main() {
final response = await request.send();

expect(response.statusCode, equals(302));
expect((response as BaseResponseV2).url, serverUrl.resolve('/redirect'));
expect(
(response as http.BaseResponseV2).url, serverUrl.resolve('/redirect'));
});

test('with redirects', () async {
final request = http.Request('GET', serverUrl.resolve('/redirect'));
final response = await request.send();

expect(response.statusCode, equals(200));
final bytesString = await response.stream.bytesToString();
expect(bytesString, parse(containsPair('path', '/')));
expect((response as BaseResponseV2).url, serverUrl.resolve('/'));
expect((response as http.BaseResponseV2).url, serverUrl.resolve('/'));
});

test('exceeding max redirects', () async {
Expand Down

0 comments on commit 40d5722

Please sign in to comment.