Skip to content

Commit

Permalink
[http2] Fix Http2Adapter not redirecting for relative ULRs
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhnroyal committed Jan 21, 2024
1 parent adc6842 commit 13bd6bd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
27 changes: 25 additions & 2 deletions plugins/http2_adapter/lib/src/http2_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,34 @@ class Http2Adapter implements HttpClientAdapter {
// Handle redirection.
if (needRedirect) {
final url = responseHeaders.value('location');
if (url == null) {
throw DioException.connectionError(
requestOptions: options,
reason: 'Received redirect without location header.',
);
}
final uri = Uri.parse(url);
redirects.add(
RedirectRecord(statusCode, options.method, Uri.parse(url ?? '')),
RedirectRecord(
statusCode,
options.method,
uri,
),
);
final String path;
if (uri.hasScheme) {
/// This is a full URL which has to be redirected to as is.
path = uri.toString();
} else {
/// This is relative with or without leading slash and is
/// resolved against the URL of the original request.
path = options.uri.resolveUri(uri).path;
}
return _fetch(
options.copyWith(path: url, maxRedirects: --options.maxRedirects),
options.copyWith(
path: path,
maxRedirects: --options.maxRedirects,
),
list != null ? Stream.fromIterable(list) : null,
cancelFuture,
redirects,
Expand Down
41 changes: 38 additions & 3 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,48 @@ void main() {
await dio.get('/drip?delay=1&numbytes=1');
});

test('request with redirect', () async {
group('redirects', () {
final dio = Dio()
..options.baseUrl = 'https://httpbun.com/'
..httpClientAdapter = Http2Adapter(ConnectionManager());

final res = await dio.get('absolute-redirect/2');
expect(res.statusCode, 200);
test('single', () async {
final response = await dio.get(
'/redirect',
queryParameters: {'url': 'https://httpbun.com/get'},
onReceiveProgress: (received, total) {
// ignore progress
},
);
expect(response.isRedirect, isTrue);

// Redirects are not supported in web.
// Rhe browser will follow the redirects automatically.
expect(response.redirects.length, 1);
final ri = response.redirects.first;
expect(ri.statusCode, 302);
expect(ri.location.path, '/get');
expect(ri.method, 'GET');
});

test('multiple', () async {
final response = await dio.get(
'/redirect/3',
);
expect(response.isRedirect, isTrue);

// Redirects are not supported in web.
// The browser will follow the redirects automatically.
expect(response.redirects.length, 3);
final ri = response.redirects.first;
expect(ri.statusCode, 302);
expect(ri.method, 'GET');
});

test('request with redirect', () async {
final res = await dio.get('absolute-redirect/2');
expect(res.statusCode, 200);
});
});

test('header value types implicit support', () async {
Expand Down

0 comments on commit 13bd6bd

Please sign in to comment.