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

Added the ability to retrieve url from BaseResponse #623

Closed
wants to merge 10 commits 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 @@ -136,6 +136,7 @@ abstract class BaseRequest {
contentLength: response.contentLength,
request: response.request,
headers: response.headers,
url: response.url,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/base_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class BaseResponse {
final Map<String, String> headers;

final bool isRedirect;
final String? url;

/// Whether the server requested that a persistent connection be maintained.
final bool persistentConnection;
Expand All @@ -39,6 +40,7 @@ abstract class BaseResponse {
this.request,
this.headers = const {},
this.isRedirect = false,
this.url,
this.persistentConnection = true,
this.reasonPhrase}) {
if (statusCode < 100) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class BrowserClient extends BaseClient {
contentLength: body.length,
request: request,
headers: xhr.responseHeaders,
url: xhr.responseUrl,
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 @@ -58,6 +58,9 @@ class IOClient extends BaseClient {
response.contentLength == -1 ? null : response.contentLength,
request: request,
headers: headers,
url: response.redirects.isEmpty
? request.url.toString()
: response.redirects.last.location.toString(),
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase,
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 @@ -24,12 +24,14 @@ class IOStreamedResponse extends StreamedResponse {
bool isRedirect = false,
bool persistentConnection = true,
String? reasonPhrase,
String? url,
HttpClientResponse? inner})
: _inner = inner,
super(stream, statusCode,
contentLength: contentLength,
request: request,
headers: headers,
url: url,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ class Response extends BaseResponse {
{BaseRequest? request,
Map<String, String> headers = const {},
bool isRedirect = false,
String? url,
bool persistentConnection = true,
String? reasonPhrase})
: this.bytes(_encodingForHeaders(headers).encode(body), statusCode,
request: request,
headers: headers,
url: url,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);
Expand All @@ -46,13 +48,15 @@ class Response extends BaseResponse {
{BaseRequest? request,
Map<String, String> headers = const {},
bool isRedirect = false,
String? url,
bool persistentConnection = true,
String? reasonPhrase})
: bodyBytes = toUint8List(bodyBytes),
super(statusCode,
contentLength: bodyBytes.length,
request: request,
headers: headers,
url: url,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);
Expand All @@ -64,6 +68,7 @@ class Response extends BaseResponse {
return Response.bytes(body, response.statusCode,
request: response.request,
headers: response.headers,
url: response.url,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.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,13 +23,15 @@ class StreamedResponse extends BaseResponse {
BaseRequest? request,
Map<String, String> headers = const {},
bool isRedirect = false,
String? url,
bool persistentConnection = true,
String? reasonPhrase})
: stream = toByteStream(stream),
super(statusCode,
contentLength: contentLength,
request: request,
headers: headers,
url: url,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: http
version: 0.13.4-dev
version: 0.13.4
homepage: https://github.com/dart-lang/http
description: A composable, multi-platform, Future-based API for HTTP requests.

Expand Down
3 changes: 3 additions & 0 deletions test/io/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void main() {
final response = await request.send();

expect(response.statusCode, equals(200));
expect(response.url, equals(serverUrl.toString()));
final bytesString = await response.stream.bytesToString();
expect(
bytesString,
Expand All @@ -44,13 +45,15 @@ void main() {
final response = await request.send();

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

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

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