Skip to content

Fixes #293, added property url to BaseResponse. related work(#321) #692

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

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 @@ -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
4 changes: 4 additions & 0 deletions lib/src/base_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ abstract class BaseResponse {
// TODO(nweiz): make this a HttpHeaders object.
final Map<String, String> headers;

/// The url of the final response(possibly after redirections).
final String? url;

final bool isRedirect;

/// Whether the server requested that a persistent connection be maintained.
Expand All @@ -38,6 +41,7 @@ abstract class BaseResponse {
{this.contentLength,
this.request,
this.headers = const {},
this.url,
this.isRedirect = false,
this.persistentConnection = true,
this.reasonPhrase}) {
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 @@ -21,6 +21,7 @@ class IOStreamedResponse extends StreamedResponse {
{int? contentLength,
BaseRequest? request,
Map<String, String> headers = const {},
String? url,
bool isRedirect = false,
bool persistentConnection = true,
String? reasonPhrase,
Expand All @@ -30,6 +31,7 @@ class IOStreamedResponse extends StreamedResponse {
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 @@ -31,12 +31,14 @@ class Response extends BaseResponse {
Response(String body, int statusCode,
{BaseRequest? request,
Map<String, String> headers = const {},
String? url,
bool isRedirect = false,
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 @@ -45,6 +47,7 @@ class Response extends BaseResponse {
Response.bytes(List<int> bodyBytes, int statusCode,
{BaseRequest? request,
Map<String, String> headers = const {},
String? url,
bool isRedirect = false,
bool persistentConnection = true,
String? reasonPhrase})
Expand All @@ -53,6 +56,7 @@ class Response extends BaseResponse {
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 @@ -22,6 +22,7 @@ class StreamedResponse extends BaseResponse {
{int? contentLength,
BaseRequest? request,
Map<String, String> headers = const {},
String? url,
bool isRedirect = false,
bool persistentConnection = true,
String? reasonPhrase})
Expand All @@ -30,6 +31,7 @@ class StreamedResponse extends BaseResponse {
contentLength: contentLength,
request: request,
headers: headers,
url: url,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase);
Expand Down
2 changes: 2 additions & 0 deletions test/io/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,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
10 changes: 10 additions & 0 deletions test/response_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,15 @@ void main() {
var response = await http.Response.fromStream(streamResponse);
expect(response.bodyBytes, equals([104, 101, 108, 108, 111]));
});

test('sets url', () async {
var controller = StreamController<List<int>>(sync: true);
var streamResponse = http.StreamedResponse(controller.stream, 302,
contentLength: 5, url: 'https://example.com');
controller.add([104, 101, 108, 108, 111]);
unawaited(controller.close());
var response = await http.Response.fromStream(streamResponse);
expect(response.url, equals('https://example.com'));
});
});
}