Skip to content

Commit 19e3781

Browse files
committed
[oauth2] Make underlying HTTP client non-nullable to inherit its close behavior
1 parent 50b4514 commit 19e3781

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

pkgs/oauth2/lib/src/client.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Client extends http.BaseClient {
7373
final bool _basicAuth;
7474

7575
/// The underlying HTTP client.
76-
http.Client? _httpClient;
76+
final http.Client _httpClient;
7777

7878
/// Creates a new client from a pre-existing set of credentials.
7979
///
@@ -111,7 +111,7 @@ class Client extends http.BaseClient {
111111
}
112112

113113
request.headers['authorization'] = 'Bearer ${credentials.accessToken}';
114-
var response = await _httpClient!.send(request);
114+
var response = await _httpClient.send(request);
115115

116116
if (response.statusCode != 401) return response;
117117
if (!response.headers.containsKey('www-authenticate')) return response;
@@ -181,7 +181,6 @@ class Client extends http.BaseClient {
181181
/// Closes this client and its underlying HTTP client.
182182
@override
183183
void close() {
184-
_httpClient?.close();
185-
_httpClient = null;
184+
_httpClient.close();
186185
}
187186
}

pkgs/oauth2/test/client_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ 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+
client.close();
204+
205+
expect(
206+
client.read(requestUri),
207+
throwsA(const TypeMatcher<http.ClientException>()),
208+
);
209+
});
192210
});
193211

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

0 commit comments

Comments
 (0)