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

[http] ✨ Add redirects in IOStreamedResponse #1076

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.3-wip

* Add `redirects` in `IOStreamedResponse`.

## 1.1.2

* Allow `web: '>=0.3.0 <0.5.0'`.
Expand Down
1 change: 1 addition & 0 deletions pkgs/http/lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class IOClient extends BaseClient {
request: request,
headers: headers,
isRedirect: response.isRedirect,
redirects: response.redirects,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase,
inner: response);
Expand Down
7 changes: 7 additions & 0 deletions pkgs/http/lib/src/io_streamed_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ class IOStreamedResponse extends StreamedResponse {
super.isRedirect,
super.persistentConnection,
super.reasonPhrase,
this.redirects = const [],
HttpClientResponse? inner})
: _inner = inner;

/// Returns the series of redirects this connection has been through.
/// The list will be empty if no redirects were followed.
/// [redirects] will be updated both in the case of
/// an automatic and a manual redirect.
final List<RedirectInfo> redirects;

/// Detaches the underlying socket from the HTTP server.
///
/// Will throw if `inner` was not set or `null` when `this` was created.
Expand Down
13 changes: 13 additions & 0 deletions pkgs/http/test/io/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
@TestOn('vm')
library;

import 'dart:io';

import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as http_io;
import 'package:test/test.dart';

import '../utils.dart';
Expand Down Expand Up @@ -65,4 +68,14 @@ void main() {
throwsA(isA<http.ClientException>()
.having((e) => e.message, 'message', 'Redirect limit exceeded')));
});

test('contains redirects', () async {
var ioClient = HttpClient();
var client = http_io.IOClient(ioClient);
var request = http.Request('GET', serverUrl.resolve('/redirect'));
var response = await client.send(request);
expect(response.statusCode, equals(200));
expect(response.redirects.length, equals(1));
expect(response.redirects.first.location, serverUrl.resolve('/'));
});
}