Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Response.url #699

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/base_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ abstract class BaseRequest {
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
url: response.url,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
} catch (_) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/base_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ abstract class BaseResponse {

final bool isRedirect;

final Uri? url;

/// Whether the server requested that a persistent connection be maintained.
final bool persistentConnection;

Expand All @@ -39,6 +41,7 @@ abstract class BaseResponse {
this.request,
this.headers = const {},
this.isRedirect = false,
this.url,
this.persistentConnection = true,
this.reasonPhrase}) {
if (statusCode < 100) {
Expand Down
5 changes: 5 additions & 0 deletions lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ class BrowserClient extends BaseClient {

unawaited(xhr.onLoad.first.then((_) {
var body = (xhr.response as ByteBuffer).asUint8List();
var responseUrl = xhr.responseUrl;
var url = (responseUrl != null && responseUrl.isNotEmpty)
? Uri.parse(responseUrl)
: null;
completer.complete(StreamedResponse(
ByteStream.fromBytes(body), xhr.status!,
contentLength: body.length,
request: request,
url: url,
headers: xhr.responseHeaders,
reasonPhrase: xhr.statusText));
}));
Expand Down
3 changes: 3 additions & 0 deletions lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class IOClient extends BaseClient {
request: request,
headers: headers,
isRedirect: response.isRedirect,
url: response.redirects.isNotEmpty
? response.redirects.last.location
: request.url,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase,
inner: response);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/io_streamed_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class IOStreamedResponse extends StreamedResponse {
BaseRequest? request,
Map<String, String> headers = const {},
bool isRedirect = false,
Uri? url,
bool persistentConnection = true,
String? reasonPhrase,
HttpClientResponse? inner})
Expand All @@ -31,6 +32,7 @@ class IOStreamedResponse extends StreamedResponse {
request: request,
headers: headers,
isRedirect: isRedirect,
url: url,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);

Expand Down
2 changes: 2 additions & 0 deletions lib/src/mock_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MockClient extends BaseClient {
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
url: response.url,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
});
Expand All @@ -60,6 +61,7 @@ class MockClient extends BaseClient {
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
url: response.url,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
});
Expand Down
4 changes: 4 additions & 0 deletions lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class Response extends BaseResponse {
{BaseRequest? request,
Map<String, String> headers = const {},
bool isRedirect = false,
Uri? url,
bool persistentConnection = true,
String? reasonPhrase})
: this.bytes(_encodingForHeaders(headers).encode(body), statusCode,
request: request,
headers: headers,
isRedirect: isRedirect,
url: url,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);

Expand All @@ -46,6 +48,7 @@ class Response extends BaseResponse {
{BaseRequest? request,
Map<String, String> headers = const {},
bool isRedirect = false,
Uri? url,
bool persistentConnection = true,
String? reasonPhrase})
: bodyBytes = toUint8List(bodyBytes),
Expand All @@ -54,6 +57,7 @@ class Response extends BaseResponse {
request: request,
headers: headers,
isRedirect: isRedirect,
url: url,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);

Expand Down
2 changes: 2 additions & 0 deletions lib/src/streamed_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class StreamedResponse extends BaseResponse {
BaseRequest? request,
Map<String, String> headers = const {},
bool isRedirect = false,
Uri? url,
bool persistentConnection = true,
String? reasonPhrase})
: stream = toByteStream(stream),
Expand All @@ -31,6 +32,7 @@ class StreamedResponse extends BaseResponse {
request: request,
headers: headers,
isRedirect: isRedirect,
url: url,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);
}
2 changes: 2 additions & 0 deletions test/io/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void main() {
final response = await request.send();

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

test('with redirects', () async {
Expand All @@ -53,6 +54,7 @@ void main() {
expect(response.statusCode, equals(200));
final bytesString = await response.stream.bytesToString();
expect(bytesString, parse(containsPair('path', '/')));
expect(response.url, serverUrl.resolve('/'));
});

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