Modernize the style.
R=rnystrom@google.com
Review URL: https://codereview.chromium.org//1311323002 .
diff --git a/lib/src/authorization_code_grant.dart b/lib/src/authorization_code_grant.dart
index 9ec4a27..e3bb645 100644
--- a/lib/src/authorization_code_grant.dart
+++ b/lib/src/authorization_code_grant.dart
@@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library authorization_code_grant;
+library oauth2.authorization_code_grant;
import 'dart:async';
@@ -13,8 +13,9 @@
import 'handle_access_token_response.dart';
import 'utils.dart';
-/// A class for obtaining credentials via an [authorization code grant][]. This
-/// method of authorization involves sending the resource owner to the
+/// A class for obtaining credentials via an [authorization code grant][].
+///
+/// This method of authorization involves sending the resource owner to the
/// authorization server where they will authorize the client. They're then
/// redirected back to your server, along with an authorization code. This is
/// used to obtain [Credentials] and create a fully-authorized [Client].
@@ -27,32 +28,22 @@
///
/// [authorization code grant]: http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1
class AuthorizationCodeGrant {
- /// An enum value for [_state] indicating that [getAuthorizationUrl] has not
- /// yet been called for this grant.
- static const _INITIAL_STATE = 0;
-
- // An enum value for [_state] indicating that [getAuthorizationUrl] has been
- // called but neither [handleAuthorizationResponse] nor
- // [handleAuthorizationCode] has been called.
- static const _AWAITING_RESPONSE_STATE = 1;
-
- // An enum value for [_state] indicating that [getAuthorizationUrl] and either
- // [handleAuthorizationResponse] or [handleAuthorizationCode] have been
- // called.
- static const _FINISHED_STATE = 2;
-
- /// The client identifier for this client. The authorization server will issue
- /// each client a separate client identifier and secret, which allows the
- /// server to tell which client is accessing it. Some servers may also have an
- /// anonymous identifier/secret pair that any client may use.
+ /// The client identifier for this client.
+ ///
+ /// The authorization server will issue each client a separate client
+ /// identifier and secret, which allows the server to tell which client is
+ /// accessing it. Some servers may also have an anonymous identifier/secret
+ /// pair that any client may use.
///
/// This is usually global to the program using this library.
final String identifier;
- /// The client secret for this client. The authorization server will issue
- /// each client a separate client identifier and secret, which allows the
- /// server to tell which client is accessing it. Some servers may also have an
- /// anonymous identifier/secret pair that any client may use.
+ /// The client secret for this client.
+ ///
+ /// The authorization server will issue each client a separate client
+ /// identifier and secret, which allows the server to tell which client is
+ /// accessing it. Some servers may also have an anonymous identifier/secret
+ /// pair that any client may use.
///
/// This is usually global to the program using this library.
///
@@ -64,13 +55,17 @@
/// A URL provided by the authorization server that serves as the base for the
/// URL that the resource owner will be redirected to to authorize this
- /// client. This will usually be listed in the authorization server's
- /// OAuth2 API documentation.
+ /// client.
+ ///
+ /// This will usually be listed in the authorization server's OAuth2 API
+ /// documentation.
final Uri authorizationEndpoint;
/// A URL provided by the authorization server that this library uses to
- /// obtain long-lasting credentials. This will usually be listed in the
- /// authorization server's OAuth2 API documentation.
+ /// obtain long-lasting credentials.
+ ///
+ /// This will usually be listed in the authorization server's OAuth2 API
+ /// documentation.
final Uri tokenEndpoint;
/// The HTTP client used to make HTTP requests.
@@ -87,9 +82,8 @@
/// included in the response query parameters.
String _stateString;
- /// The current state of the grant object. One of [_INITIAL_STATE],
- /// [_AWAITING_RESPONSE_STATE], or [_FINISHED_STATE].
- int _state = _INITIAL_STATE;
+ /// The current state of the grant object.
+ _State _state = _State.initial;
/// Creates a new grant.
///
@@ -104,9 +98,11 @@
: _httpClient = httpClient == null ? new http.Client() : httpClient;
/// Returns the URL to which the resource owner should be redirected to
- /// authorize this client. The resource owner will then be redirected to
- /// [redirect], which should point to a server controlled by the client. This
- /// redirect will have additional query parameters that should be passed to
+ /// authorize this client.
+ ///
+ /// The resource owner will then be redirected to [redirect], which should
+ /// point to a server controlled by the client. This redirect will have
+ /// additional query parameters that should be passed to
/// [handleAuthorizationResponse].
///
/// The specific permissions being requested from the authorization server may
@@ -122,10 +118,10 @@
/// It is a [StateError] to call this more than once.
Uri getAuthorizationUrl(Uri redirect,
{List<String> scopes: const <String>[], String state}) {
- if (_state != _INITIAL_STATE) {
+ if (_state != _State.initial) {
throw new StateError('The authorization URL has already been generated.');
}
- _state = _AWAITING_RESPONSE_STATE;
+ _state = _State.awaitingResponse;
this._redirectEndpoint = redirect;
this._scopes = scopes;
@@ -143,9 +139,11 @@
}
/// Processes the query parameters added to a redirect from the authorization
- /// server. Note that this "response" is not an HTTP response, but rather the
- /// data passed to a server controlled by the client as query parameters on
- /// the redirect URL.
+ /// server.
+ ///
+ /// Note that this "response" is not an HTTP response, but rather the data
+ /// passed to a server controlled by the client as query parameters on the
+ /// redirect URL.
///
/// It is a [StateError] to call this more than once, to call it before
/// [getAuthorizationUrl] is called, or to call it after
@@ -159,14 +157,14 @@
/// Throws [AuthorizationException] if the authorization fails.
Future<Client> handleAuthorizationResponse(Map<String, String> parameters)
async {
- if (_state == _INITIAL_STATE) {
+ if (_state == _State.initial) {
throw new StateError(
'The authorization URL has not yet been generated.');
- } else if (_state == _FINISHED_STATE) {
+ } else if (_state == _State.finished) {
throw new StateError(
'The authorization code has already been received.');
}
- _state = _FINISHED_STATE;
+ _state = _State.finished;
if (_stateString != null) {
if (!parameters.containsKey('state')) {
@@ -194,11 +192,12 @@
return await _handleAuthorizationCode(parameters['code']);
}
- /// Processes an authorization code directly. Usually
- /// [handleAuthorizationResponse] is preferable to this method, since it
- /// validates all of the query parameters. However, some authorization servers
- /// allow the user to copy and paste an authorization code into a command-line
- /// application, in which case this method must be used.
+ /// Processes an authorization code directly.
+ ///
+ /// Usually [handleAuthorizationResponse] is preferable to this method, since
+ /// it validates all of the query parameters. However, some authorization
+ /// servers allow the user to copy and paste an authorization code into a
+ /// command-line application, in which case this method must be used.
///
/// It is a [StateError] to call this more than once, to call it before
/// [getAuthorizationUrl] is called, or to call it after
@@ -209,14 +208,14 @@
///
/// Throws [AuthorizationException] if the authorization fails.
Future<Client> handleAuthorizationCode(String authorizationCode) async {
- if (_state == _INITIAL_STATE) {
+ if (_state == _State.initial) {
throw new StateError(
'The authorization URL has not yet been generated.');
- } else if (_state == _FINISHED_STATE) {
+ } else if (_state == _State.finished) {
throw new StateError(
'The authorization code has already been received.');
}
- _state = _FINISHED_STATE;
+ _state = _State.finished;
return await _handleAuthorizationCode(authorizationCode);
}
@@ -252,3 +251,26 @@
_httpClient = null;
}
}
+
+/// States that [AuthorizationCodeGrant] can be in.
+class _State {
+ /// [AuthorizationCodeGrant.getAuthorizationUrl] has not yet been called for
+ /// this grant.
+ static const initial = const _State("initial");
+
+ // [AuthorizationCodeGrant.getAuthorizationUrl] has been called but neither
+ // [AuthorizationCodeGrant.handleAuthorizationResponse] nor
+ // [AuthorizationCodeGrant.handleAuthorizationCode] has been called.
+ static const awaitingResponse = const _State("awaiting response");
+
+ // [AuthorizationCodeGrant.getAuthorizationUrl] and either
+ // [AuthorizationCodeGrant.handleAuthorizationResponse] or
+ // [AuthorizationCodeGrant.handleAuthorizationCode] have been called.
+ static const finished = const _State("finished");
+
+ final String _name;
+
+ const _State(this._name);
+
+ String toString() => _name;
+}
diff --git a/lib/src/authorization_exception.dart b/lib/src/authorization_exception.dart
index 2a19cd1..838d001 100644
--- a/lib/src/authorization_exception.dart
+++ b/lib/src/authorization_exception.dart
@@ -2,20 +2,26 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library authorization_exception;
+library oauth2.authorization_exception;
/// An exception raised when OAuth2 authorization fails.
class AuthorizationException implements Exception {
- /// The name of the error. Possible names are enumerated in [the spec][].
+ /// The name of the error.
+ ///
+ /// Possible names are enumerated in [the spec][].
///
/// [the spec]: http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-5.2
final String error;
- /// The description of the error, provided by the server. Defaults to null.
+ /// The description of the error, provided by the server.
+ ///
+ /// May be `null` if the server provided no description.
final String description;
- /// A URI for a page that describes the error in more detail, provided by the
- /// server. Defaults to null.
+ /// A URL for a page that describes the error in more detail, provided by the
+ /// server.
+ ///
+ /// May be `null` if the server provided no URL.
final Uri uri;
/// Creates an AuthorizationException.
diff --git a/lib/src/client.dart b/lib/src/client.dart
index 60495a0..371eed3 100644
--- a/lib/src/client.dart
+++ b/lib/src/client.dart
@@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library oauth2_client;
+library oauth2.client;
import 'dart:async';
@@ -15,8 +15,10 @@
// TODO(nweiz): Add an onCredentialsRefreshed event once we have some event
// infrastructure.
-/// An OAuth2 client. This acts as a drop-in replacement for an [http.Client],
-/// while sending OAuth2 authorization credentials along with each request.
+/// An OAuth2 client.
+///
+/// This acts as a drop-in replacement for an [http.Client], while sending
+/// OAuth2 authorization credentials along with each request.
///
/// The client also automatically refreshes its credentials if possible. When it
/// makes a request, if its credentials are expired, it will first refresh them.
@@ -34,18 +36,22 @@
/// authorize. At the time of writing, the only authorization method this
/// library supports is [AuthorizationCodeGrant].
class Client extends http.BaseClient {
- /// The client identifier for this client. The authorization server will issue
- /// each client a separate client identifier and secret, which allows the
- /// server to tell which client is accessing it. Some servers may also have an
- /// anonymous identifier/secret pair that any client may use.
+ /// The client identifier for this client.
+ ///
+ /// The authorization server will issue each client a separate client
+ /// identifier and secret, which allows the server to tell which client is
+ /// accessing it. Some servers may also have an anonymous identifier/secret
+ /// pair that any client may use.
///
/// This is usually global to the program using this library.
final String identifier;
- /// The client secret for this client. The authorization server will issue
- /// each client a separate client identifier and secret, which allows the
- /// server to tell which client is accessing it. Some servers may also have an
- /// anonymous identifier/secret pair that any client may use.
+ /// The client secret for this client.
+ ///
+ /// The authorization server will issue each client a separate client
+ /// identifier and secret, which allows the server to tell which client is
+ /// accessing it. Some servers may also have an anonymous identifier/secret
+ /// pair that any client may use.
///
/// This is usually global to the program using this library.
///
@@ -56,16 +62,19 @@
final String secret;
/// The credentials this client uses to prove to the resource server that it's
- /// authorized. This may change from request to request as the credentials
- /// expire and the client refreshes them automatically.
+ /// authorized.
+ ///
+ /// This may change from request to request as the credentials expire and the
+ /// client refreshes them automatically.
Credentials get credentials => _credentials;
Credentials _credentials;
/// The underlying HTTP client.
http.Client _httpClient;
- /// Creates a new client from a pre-existing set of credentials. When
- /// authorizing a client for the first time, you should use
+ /// Creates a new client from a pre-existing set of credentials.
+ ///
+ /// When authorizing a client for the first time, you should use
/// [AuthorizationCodeGrant] instead of constructing a [Client] directly.
///
/// [httpClient] is the underlying client that this forwards requests to after
@@ -77,9 +86,10 @@
{http.Client httpClient})
: _httpClient = httpClient == null ? new http.Client() : httpClient;
- /// Sends an HTTP request with OAuth2 authorization credentials attached. This
- /// will also automatically refresh this client's [Credentials] before sending
- /// the request if necessary.
+ /// Sends an HTTP request with OAuth2 authorization credentials attached.
+ ///
+ /// This will also automatically refresh this client's [Credentials] before
+ /// sending the request if necessary.
Future<http.StreamedResponse> send(http.BaseRequest request) async {
if (credentials.isExpired) {
if (!credentials.canRefresh) throw new ExpirationException(credentials);
@@ -96,7 +106,7 @@
try {
authenticate = new AuthenticateHeader.parse(
response.headers['www-authenticate']);
- } on FormatException catch (e) {
+ } on FormatException catch (_) {
return response;
}
diff --git a/lib/src/credentials.dart b/lib/src/credentials.dart
index 88b1f5d..37e7114 100644
--- a/lib/src/credentials.dart
+++ b/lib/src/credentials.dart
@@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library credentials;
+library oauth2.credentials;
import 'dart:async';
import 'dart:convert';
@@ -10,11 +10,12 @@
import 'package:http/http.dart' as http;
import 'handle_access_token_response.dart';
-import 'utils.dart';
/// Credentials that prove that a client is allowed to access a resource on the
-/// resource owner's behalf. These credentials are long-lasting and can be
-/// safely persisted across multiple runs of the program.
+/// resource owner's behalf.
+///
+/// These credentials are long-lasting and can be safely persisted across
+/// multiple runs of the program.
///
/// Many authorization servers will attach an expiration date to a set of
/// credentials, along with a token that can be used to refresh the credentials
@@ -30,26 +31,34 @@
final String accessToken;
/// The token that is sent to the authorization server to refresh the
- /// credentials. This is optional.
+ /// credentials.
+ ///
+ /// This may be `null`, indicating that the credentials can't be refreshed.
final String refreshToken;
/// The URL of the authorization server endpoint that's used to refresh the
- /// credentials. This is optional.
+ /// credentials.
+ ///
+ /// This may be `null`, indicating that the credentials can't be refreshed.
final Uri tokenEndpoint;
/// The specific permissions being requested from the authorization server.
+ ///
/// The scope strings are specific to the authorization server and may be
/// found in its documentation.
final List<String> scopes;
- /// The date at which these credentials will expire. This is likely to be a
- /// few seconds earlier than the server's idea of the expiration date.
+ /// The date at which these credentials will expire.
+ ///
+ /// This is likely to be a few seconds earlier than the server's idea of the
+ /// expiration date.
final DateTime expiration;
- /// Whether or not these credentials have expired. Note that it's possible the
- /// credentials will expire shortly after this is called. However, since the
- /// client's expiration date is kept a few seconds earlier than the server's,
- /// there should be enough leeway to rely on this.
+ /// Whether or not these credentials have expired.
+ ///
+ /// Note that it's possible the credentials will expire shortly after this is
+ /// called. However, since the client's expiration date is kept a few seconds
+ /// earlier than the server's, there should be enough leeway to rely on this.
bool get isExpired => expiration != null &&
new DateTime.now().isAfter(expiration);
@@ -69,10 +78,11 @@
this.scopes,
this.expiration]);
- /// Loads a set of credentials from a JSON-serialized form. Throws
- /// [FormatException] if the JSON is incorrectly formatted.
+ /// Loads a set of credentials from a JSON-serialized form.
+ ///
+ /// Throws a [FormatException] if the JSON is incorrectly formatted.
factory Credentials.fromJson(String json) {
- void validate(bool condition, String message) {
+ validate(condition, message) {
if (condition) return;
throw new FormatException(
"Failed to load credentials: $message.\n\n$json");
@@ -81,7 +91,7 @@
var parsed;
try {
parsed = JSON.decode(json);
- } on FormatException catch (e) {
+ } on FormatException catch (_) {
validate(false, 'invalid JSON');
}
@@ -122,9 +132,10 @@
expiration);
}
- /// Serializes a set of credentials to JSON. Nothing is guaranteed about the
- /// output except that it's valid JSON and compatible with
- /// [Credentials.toJson].
+ /// Serializes a set of credentials to JSON.
+ ///
+ /// Nothing is guaranteed about the output except that it's valid JSON and
+ /// compatible with [Credentials.toJson].
String toJson() => JSON.encode({
'accessToken': accessToken,
'refreshToken': refreshToken,
@@ -133,8 +144,10 @@
'expiration': expiration == null ? null : expiration.millisecondsSinceEpoch
});
- /// Returns a new set of refreshed credentials. See [Client.identifier] and
- /// [Client.secret] for explanations of those parameters.
+ /// Returns a new set of refreshed credentials.
+ ///
+ /// See [Client.identifier] and [Client.secret] for explanations of those
+ /// parameters.
///
/// You may request different scopes than the default by passing in
/// [newScopes]. These must be a subset of [scopes].
diff --git a/lib/src/expiration_exception.dart b/lib/src/expiration_exception.dart
index 9829de1..f684e6a 100644
--- a/lib/src/expiration_exception.dart
+++ b/lib/src/expiration_exception.dart
@@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library expiration_exception;
+library oauth2.expiration_exception;
import 'credentials.dart';
@@ -16,5 +16,5 @@
/// Provides a string description of the ExpirationException.
String toString() =>
- "OAuth2 credentials have expired and can't be refreshed.";
+ "OAuth2 credentials have expired and can't be refreshed.";
}
diff --git a/lib/src/handle_access_token_response.dart b/lib/src/handle_access_token_response.dart
index 5766362..0065f0c 100644
--- a/lib/src/handle_access_token_response.dart
+++ b/lib/src/handle_access_token_response.dart
@@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library handle_access_token_response;
+library oauth2.handle_access_token_response;
import 'dart:convert';
@@ -12,14 +12,17 @@
import 'credentials.dart';
import 'authorization_exception.dart';
-/// The amount of time, in seconds, to add as a "grace period" for credential
-/// expiration. This allows credential expiration checks to remain valid for a
-/// reasonable amount of time.
-const _EXPIRATION_GRACE = 10;
+/// The amount of time to add as a "grace period" for credential expiration.
+///
+/// This allows credential expiration checks to remain valid for a reasonable
+/// amount of time.
+const _expirationGrace = const Duration(seconds: 10);
/// Handles a response from the authorization server that contains an access
-/// token. This response format is common across several different components of
-/// the OAuth2 flow.
+/// token.
+///
+/// This response format is common across several different components of the
+/// OAuth2 flow.
Credentials handleAccessTokenResponse(
http.Response response,
Uri tokenEndpoint,
@@ -27,8 +30,8 @@
List<String> scopes) {
if (response.statusCode != 200) _handleErrorResponse(response, tokenEndpoint);
- void validate(bool condition, String message) =>
- _validate(response, tokenEndpoint, condition, message);
+ validate(condition, message) =>
+ _validate(response, tokenEndpoint, condition, message);
var contentType = response.headers['content-type'];
if (contentType != null) contentType = new MediaType.parse(contentType);
@@ -43,7 +46,7 @@
var parameters;
try {
parameters = JSON.decode(response.body);
- } on FormatException catch (e) {
+ } on FormatException catch (_) {
validate(false, 'invalid JSON');
}
@@ -74,7 +77,7 @@
if (scope != null) scopes = scope.split(" ");
var expiration = expiresIn == null ? null :
- startTime.add(new Duration(seconds: expiresIn - _EXPIRATION_GRACE));
+ startTime.add(new Duration(seconds: expiresIn) - _expirationGrace);
return new Credentials(
parameters['access_token'],
@@ -87,8 +90,8 @@
/// Throws the appropriate exception for an error response from the
/// authorization server.
void _handleErrorResponse(http.Response response, Uri tokenEndpoint) {
- void validate(bool condition, String message) =>
- _validate(response, tokenEndpoint, condition, message);
+ validate(condition, message) =>
+ _validate(response, tokenEndpoint, condition, message);
// OAuth2 mandates a 400 or 401 response code for access token error
// responses. If it's not a 400 reponse, the server is either broken or
@@ -110,7 +113,7 @@
var parameters;
try {
parameters = JSON.decode(response.body);
- } on FormatException catch (e) {
+ } on FormatException catch (_) {
validate(false, 'invalid JSON');
}
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index b292625..734c58e 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -2,57 +2,18 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library utils;
-
-import 'dart:async';
+library oauth2.utils;
/// Adds additional query parameters to [url], overwriting the original
/// parameters if a name conflict occurs.
-Uri addQueryParameters(Uri url, Map<String, String> parameters) {
- var queryMap = queryToMap(url.query);
- queryMap.addAll(parameters);
- return url.resolve("?${mapToQuery(queryMap)}");
-}
-
-/// Convert a URL query string (or `application/x-www-form-urlencoded` body)
-/// into a [Map] from parameter names to values.
-Map<String, String> queryToMap(String queryList) {
- var map = {};
- for (var pair in queryList.split("&")) {
- var split = split1(pair, "=");
- if (split.isEmpty) continue;
- var key = urlDecode(split[0]);
- var value = split.length > 1 ? urlDecode(split[1]) : "";
- map[key] = value;
- }
- return map;
-}
-
-/// Convert a [Map] from parameter names to values to a URL query string.
-String mapToQuery(Map<String, String> map) {
- var pairs = <List<String>>[];
- map.forEach((key, value) {
- key = Uri.encodeQueryComponent(key);
- value = (value == null || value.isEmpty)
- ? null
- : Uri.encodeQueryComponent(value);
- pairs.add([key, value]);
- });
- return pairs.map((pair) {
- if (pair[1] == null) return pair[0];
- return "${pair[0]}=${pair[1]}";
- }).join("&");
-}
-
-/// Decode a URL-encoded string. Unlike [Uri.decodeComponent], this includes
-/// replacing `+` with ` `.
-String urlDecode(String encoded) =>
- Uri.decodeComponent(encoded.replaceAll("+", " "));
+Uri addQueryParameters(Uri url, Map<String, String> parameters) => url.replace(
+ queryParameters: new Map.from(url.queryParameters)..addAll(parameters));
/// Like [String.split], but only splits on the first occurrence of the pattern.
+///
/// This will always return a list of two elements or fewer.
List<String> split1(String toSplit, String pattern) {
- if (toSplit.isEmpty) return <String>[];
+ if (toSplit.isEmpty) return [];
var index = toSplit.indexOf(pattern);
if (index == -1) return [toSplit];
diff --git a/test/authorization_code_grant_test.dart b/test/authorization_code_grant_test.dart
index 24ff59c..deaad5c 100644
--- a/test/authorization_code_grant_test.dart
+++ b/test/authorization_code_grant_test.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library authorization_code_grant_test;
-
import 'dart:async';
import 'dart:convert';
@@ -15,24 +13,20 @@
final redirectUrl = Uri.parse('http://example.com/redirect');
-ExpectClient client;
-
-oauth2.AuthorizationCodeGrant grant;
-
-void createGrant() {
- client = new ExpectClient();
- grant = new oauth2.AuthorizationCodeGrant(
- 'identifier',
- 'secret',
- Uri.parse('https://example.com/authorization'),
- Uri.parse('https://example.com/token'),
- httpClient: client);
-}
-
void main() {
- group('.getAuthorizationUrl', () {
- setUp(createGrant);
+ var client;
+ var grant;
+ setUp(() {
+ client = new ExpectClient();
+ grant = new oauth2.AuthorizationCodeGrant(
+ 'identifier',
+ 'secret',
+ Uri.parse('https://example.com/authorization'),
+ Uri.parse('https://example.com/token'),
+ httpClient: client);
+ });
+ group('.getAuthorizationUrl', () {
test('builds the correct URL', () {
expect(grant.getAuthorizationUrl(redirectUrl).toString(),
equals('https://example.com/authorization'
@@ -87,8 +81,6 @@
});
group('.handleAuthorizationResponse', () {
- setUp(createGrant);
-
test("can't be called before .getAuthorizationUrl", () {
expect(grant.handleAuthorizationResponse({}), throwsStateError);
});
@@ -153,8 +145,6 @@
});
group('.handleAuthorizationCode', () {
- setUp(createGrant);
-
test("can't be called before .getAuthorizationUrl", () {
expect(grant.handleAuthorizationCode('auth code'), throwsStateError);
});
diff --git a/test/client_test.dart b/test/client_test.dart
index 7115095..969787a 100644
--- a/test/client_test.dart
+++ b/test/client_test.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library client_test;
-
import 'dart:async';
import 'dart:convert';
@@ -17,16 +15,11 @@
final Uri tokenEndpoint = Uri.parse('http://example.com/token');
-ExpectClient httpClient;
-
-void createHttpClient() {
- httpClient = new ExpectClient();
-}
-
void main() {
- group('with expired credentials', () {
- setUp(createHttpClient);
+ var httpClient;
+ setUp(() => httpClient = new ExpectClient());
+ group('with expired credentials', () {
test("that can't be refreshed throws an ExpirationException on send", () {
var expiration = new DateTime.now().subtract(new Duration(hours: 1));
var credentials = new oauth2.Credentials(
@@ -70,8 +63,6 @@
});
group('with valid credentials', () {
- setUp(createHttpClient);
-
test("sends a request with bearer authorization", () {
var credentials = new oauth2.Credentials('access token');
var client = new oauth2.Client('identifier', 'secret', credentials,
@@ -117,8 +108,6 @@
});
group('with invalid credentials', () {
- setUp(createHttpClient);
-
test('throws an AuthorizationException for a 401 response', () {
var credentials = new oauth2.Credentials('access token');
var client = new oauth2.Client('identifier', 'secret', credentials,
diff --git a/test/credentials_test.dart b/test/credentials_test.dart
index 3bd44d3..d72614f 100644
--- a/test/credentials_test.dart
+++ b/test/credentials_test.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library credentials_test;
-
import 'dart:async';
import 'dart:convert';
@@ -15,9 +13,8 @@
final Uri tokenEndpoint = Uri.parse('http://example.com/token');
-ExpectClient httpClient;
-
void main() {
+ var httpClient;
setUp(() => httpClient = new ExpectClient());
test('is not expired if no expiration exists', () {
diff --git a/test/utils.dart b/test/utils.dart
index bee6bf3..5bdf78a 100644
--- a/test/utils.dart
+++ b/test/utils.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library utils;
-
import 'dart:async';
import 'dart:collection' show Queue;
diff --git a/test/utils_test.dart b/test/utils_test.dart
index 0dd0348..54c2da5 100644
--- a/test/utils_test.dart
+++ b/test/utils_test.dart
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library utils_test;
-
import 'package:oauth2/src/utils.dart';
import 'package:test/test.dart';