Skip to content

Commit 56aac36

Browse files
committed
[oauth2] Add isClosed and exception handling for closed state to oauth2.Client
1 parent 36f5c9f commit 56aac36

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pkgs/oauth2/lib/src/client.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class Client extends http.BaseClient {
6666
Credentials get credentials => _credentials;
6767
Credentials _credentials;
6868

69+
/// Indicates whether the client is closed or not.
70+
bool get isClosed => _httpClient == null;
71+
6972
/// Callback to be invoked whenever the credentials refreshed.
7073
final CredentialsRefreshedCallback? _onCredentialsRefreshed;
7174

@@ -110,8 +113,13 @@ class Client extends http.BaseClient {
110113
await refreshCredentials();
111114
}
112115

116+
final httpClient = _httpClient;
117+
if (httpClient == null) {
118+
throw http.ClientException('Client is already closed.');
119+
}
120+
113121
request.headers['authorization'] = 'Bearer ${credentials.accessToken}';
114-
var response = await _httpClient!.send(request);
122+
var response = await httpClient.send(request);
115123

116124
if (response.statusCode != 401) return response;
117125
if (!response.headers.containsKey('www-authenticate')) return response;

pkgs/oauth2/test/client_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,26 @@ void main() {
189189

190190
expect(client.refreshCredentials(), throwsA(isStateError));
191191
});
192+
193+
test("won't send a request with closed client", () {
194+
var credentials = oauth2.Credentials('access token');
195+
196+
var client = oauth2.Client(
197+
credentials,
198+
identifier: 'identifier',
199+
secret: 'secret',
200+
httpClient: httpClient,
201+
);
202+
203+
expect(client.isClosed, equals(false));
204+
client.close();
205+
expect(client.isClosed, equals(true));
206+
207+
expect(
208+
client.read(requestUri),
209+
throwsA(const TypeMatcher<http.ClientException>()),
210+
);
211+
});
192212
});
193213

194214
group('with invalid credentials', () {

0 commit comments

Comments
 (0)