Skip to content

Commit afe16ee

Browse files
committed
Fix more tests
1 parent a38f45a commit afe16ee

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

lib/src/authentication/client.dart

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,14 @@ class _AuthenticatedClient extends http.BaseClient {
4848
await _credential!.getAuthorizationHeaderValue();
4949
}
5050

51-
try {
52-
final response = await _inner.send(request);
53-
if (response.statusCode == 401) {
54-
_detectInvalidCredentials = true;
55-
_throwAuthException(response);
56-
}
57-
return response;
58-
} on PubHttpResponseException catch (e) {
59-
if (e.response.statusCode == 403) {
60-
_throwAuthException(e.response);
61-
}
62-
rethrow;
51+
final response = await _inner.send(request);
52+
if (response.statusCode == 401) {
53+
_detectInvalidCredentials = true;
54+
}
55+
if (response.statusCode == 401 || response.statusCode == 403) {
56+
_throwAuthException(response);
6357
}
58+
return response;
6459
}
6560

6661
/// Throws [AuthenticationException] that includes response status code and

lib/src/http.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,28 @@ Future<T> retryForHttp<T>(String operation, FutureOr<T> Function() fn) async {
293293
}
294294

295295
extension Throwing on http.BaseResponse {
296+
/// See https://api.flutter.dev/flutter/dart-io/HttpClientRequest/followRedirects.html
297+
static const _redirectStatusCodes = [
298+
HttpStatus.movedPermanently,
299+
HttpStatus.movedTemporarily,
300+
HttpStatus.seeOther,
301+
HttpStatus.temporaryRedirect,
302+
HttpStatus.permanentRedirect
303+
];
304+
296305
/// Throws [PubHttpResponseException], calls [fail], or does nothing depending
297306
/// on the status code.
298307
///
299-
/// If the code is in the 200 range, nothing is done. If the code is 408, 429,
300-
/// or in the 500 range, [PubHttpResponseException] is thrown with
301-
/// "isIntermittent" set to `true`. Otherwise, [PubHttpResponseException] is
302-
/// thrown with "isIntermittent" set to `false`.
308+
/// If the code is in the 200 range or if its a 300 range redirect code,
309+
/// nothing is done. If the code is 408, 429, or in the 500 range,
310+
/// [PubHttpResponseException] is thrown with "isIntermittent" set to `true`.
311+
/// Otherwise, [PubHttpResponseException] is thrown with "isIntermittent" set
312+
/// to `false`.
303313
void throwIfNotOk() {
304314
if (statusCode >= 200 && statusCode <= 299) {
305315
return;
316+
} else if (_redirectStatusCodes.contains(statusCode)) {
317+
return;
306318
} else if (statusCode == HttpStatus.notAcceptable &&
307319
request?.headers['Accept'] == pubApiHeaders['Accept']) {
308320
fail('Pub ${sdk.version} is incompatible with the current version of '

test/hosted/fail_gracefully_on_url_resolve_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ void main() {
2020
]).create();
2121

2222
await pubCommand(command,
23-
error: 'Could not resolve URL "https://invalid-url.foo".',
23+
error: 'Got socket error trying to find package foo at '
24+
'https://invalid-url.foo.',
2425
exitCode: exit_codes.UNAVAILABLE,
2526
environment: {
2627
'PUB_MAX_HTTP_RETRIES': '2',

0 commit comments

Comments
 (0)