Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkgs/oauth2/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class Client extends http.BaseClient {
Credentials get credentials => _credentials;
Credentials _credentials;

/// Indicates whether the client is closed or not.
bool get isClosed => _httpClient == null;

/// Callback to be invoked whenever the credentials refreshed.
final CredentialsRefreshedCallback? _onCredentialsRefreshed;

Expand Down Expand Up @@ -110,8 +113,13 @@ class Client extends http.BaseClient {
await refreshCredentials();
}

final httpClient = _httpClient;
if (httpClient == null) {
throw http.ClientException('Client is already closed.');
}

request.headers['authorization'] = 'Bearer ${credentials.accessToken}';
var response = await _httpClient!.send(request);
var response = await httpClient.send(request);

if (response.statusCode != 401) return response;
if (!response.headers.containsKey('www-authenticate')) return response;
Expand Down
20 changes: 20 additions & 0 deletions pkgs/oauth2/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ void main() {

expect(client.refreshCredentials(), throwsA(isStateError));
});

test("won't send a request with closed client", () {
var credentials = oauth2.Credentials('access token');

var client = oauth2.Client(
credentials,
identifier: 'identifier',
secret: 'secret',
httpClient: httpClient,
);

expect(client.isClosed, equals(false));
client.close();
expect(client.isClosed, equals(true));

expect(
client.read(requestUri),
throwsA(const TypeMatcher<http.ClientException>()),
);
});
});

group('with invalid credentials', () {
Expand Down
Loading