Skip to content

Commit

Permalink
🐛 Avoid Interceptors.clear removes ImplyContentTypeInterceptor (#…
Browse files Browse the repository at this point in the history
…1975)

Resolves #1973

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

The PR only addresses `clear`. However, lists can be modified in various
ways which might also remove/replace the interceptor. We might need to
rethink an extra internal interceptors list.
  • Loading branch information
AlexV525 authored Sep 22, 2023
1 parent 22cfd6b commit 0687488
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
- Reduce cases in which browsers would trigger a CORS preflight request.
- Add warnings in debug mode when using `sendTimeout` and `onSendProgress` with an empty request body.
- Fix `receiveTimeout` not working correctly on web.
- Fix `ImplyContentTypeInterceptor` can be removed by `Interceptors.clear()` by default.

## 5.3.2

Expand Down
11 changes: 11 additions & 0 deletions dio/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ class Interceptors extends ListMixin<Interceptor> {
}
}

/// The default [ImplyContentTypeInterceptor] will be removed only if
/// [keepImplyContentTypeInterceptor] is false.
@override
void clear({bool keepImplyContentTypeInterceptor = true}) {
if (keepImplyContentTypeInterceptor) {
_list.removeWhere((e) => e is! ImplyContentTypeInterceptor);
} else {
super.clear();
}
}

/// Remove the default imply content type interceptor.
void removeImplyContentTypeInterceptor() {
_list.removeWhere((e) => e is ImplyContentTypeInterceptor);
Expand Down
4 changes: 4 additions & 0 deletions dio/test/interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ void main() {
expect(tokenRequestCounts, 1);
expect(result, 3);
expect(myInter.requestCount, predicate((int e) => e > 0));
// The `ImplyContentTypeInterceptor` will be replaced.
dio.interceptors[0] = myInter;
dio.interceptors.clear();
expect(dio.interceptors.isEmpty, true);
Expand Down Expand Up @@ -659,6 +660,9 @@ void main() {
expect(interceptors.length, equals(2));
expect(interceptors, isNotEmpty);
interceptors.clear();
expect(interceptors.length, equals(1));
expect(interceptors.single, isA<ImplyContentTypeInterceptor>());
interceptors.clear(keepImplyContentTypeInterceptor: false);
expect(interceptors.length, equals(0));
expect(interceptors, isEmpty);
});
Expand Down

0 comments on commit 0687488

Please sign in to comment.