Skip to content

Commit 2b1defe

Browse files
Fix URI, retry, and fragment handling in HTTP interceptor
Co-authored-by: me <me@codingale.dev>
1 parent 4cc2062 commit 2b1defe

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

BUGS_FIXED.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Bugs Fixed in HTTP Interceptor
2+
3+
## Bug #1: `maxRetryAttempts` cannot be overridden in subclasses
4+
5+
**File**: `lib/models/retry_policy.dart`
6+
**Issue**: The `maxRetryAttempts` field was declared as `final int maxRetryAttempts = 1;` which prevented subclasses from overriding it.
7+
**Fix**: Changed it to `int get maxRetryAttempts => 1;` to allow subclasses to override the getter.
8+
**Impact**: This allows proper inheritance for retry policies, as demonstrated in the example where `ExpiredTokenRetryPolicy` overrides `maxRetryAttempts` to return 2.
9+
10+
## Bug #2: `_attemptRequest` recursive calls missing `isStream` parameter
11+
12+
**File**: `lib/http/intercepted_client.dart`
13+
**Issue**: When retrying requests, the recursive calls to `_attemptRequest` didn't pass the `isStream` parameter, which could cause issues when retrying streamed requests.
14+
**Fix**: Added `isStream: isStream` parameter to both recursive calls in the retry logic (lines 295 and 304).
15+
**Impact**: This ensures that streamed requests are properly handled during retries, maintaining the correct response type.
16+
17+
## Bug #3: URI fragment (#) is lost in `addParameters` method
18+
19+
**File**: `lib/extensions/uri.dart`
20+
**Issue**: The `addParameters` method used `origin + path` to build the URL but ignored the fragment part of the URI, causing fragments to be lost.
21+
**Fix**: Added logic to preserve the fragment by appending `#$fragment` to the final URL if a fragment exists.
22+
**Impact**: This ensures that URI fragments are preserved when adding query parameters, maintaining the complete URL structure.
23+
24+
All three bugs have been successfully fixed and the code should now work correctly in all scenarios.

lib/extensions/uri.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ extension AddParameters on Uri {
1919
newParameters[key] = value;
2020
});
2121

22-
return buildUrlString(paramUrl, newParameters).toUri();
22+
String finalUrl = buildUrlString(paramUrl, newParameters);
23+
24+
// Preserve the fragment if it exists
25+
if (fragment.isNotEmpty) {
26+
finalUrl += '#$fragment';
27+
}
28+
29+
return finalUrl.toUri();
2330
}
2431
}

lib/http/intercepted_client.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class InterceptedClient extends BaseClient {
293293
_retryCount += 1;
294294
await Future.delayed(retryPolicy!
295295
.delayRetryAttemptOnResponse(retryAttempt: _retryCount));
296-
return _attemptRequest(request);
296+
return _attemptRequest(request, isStream: isStream);
297297
}
298298
} on Exception catch (error) {
299299
if (retryPolicy != null &&
@@ -302,7 +302,7 @@ class InterceptedClient extends BaseClient {
302302
_retryCount += 1;
303303
await Future.delayed(retryPolicy!
304304
.delayRetryAttemptOnException(retryAttempt: _retryCount));
305-
return _attemptRequest(request);
305+
return _attemptRequest(request, isStream: isStream);
306306
} else {
307307
rethrow;
308308
}

lib/models/retry_policy.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ abstract class RetryPolicy {
4545
FutureOr<bool> shouldAttemptRetryOnResponse(BaseResponse response) => false;
4646

4747
/// Number of maximum request attempts that can be retried.
48-
final int maxRetryAttempts = 1;
48+
int get maxRetryAttempts => 1;
4949

5050
Duration delayRetryAttemptOnException({required int retryAttempt}) =>
5151
Duration.zero;

0 commit comments

Comments
 (0)