Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Unified Diff: pkg/oauth2/test/client_test.dart

Issue 11420025: Add a package for authenticating via OAuth2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Refresh token fixes and code review changes Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/oauth2/test/authorization_code_grant_test.dart ('k') | pkg/oauth2/test/credentials_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/oauth2/test/client_test.dart
diff --git a/pkg/oauth2/test/client_test.dart b/pkg/oauth2/test/client_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..94b792038bff8bf407be500848c10cfd312e16af
--- /dev/null
+++ b/pkg/oauth2/test/client_test.dart
@@ -0,0 +1,120 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// 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:io';
+import 'dart:json';
+import 'dart:uri';
+
+import '../../unittest/lib/unittest.dart';
+import '../../http/lib/http.dart' as http;
+import '../lib/oauth2.dart' as oauth2;
+import 'utils.dart';
+
+final Uri requestUri = new Uri.fromString("http://example.com/resource");
+
+final Uri tokenEndpoint = new Uri.fromString('http://example.com/token');
+
+ExpectClient httpClient;
+
+void createHttpClient() {
+ httpClient = new ExpectClient();
+}
+
+void main() {
+ group('with expired credentials', () {
+ setUp(createHttpClient);
+
+ test("that can't be refreshed throws an ExpirationException on send", () {
+ var expiration = new Date.now().subtract(new Duration(hours: 1));
+ var credentials = new oauth2.Credentials(
+ 'access token', null, null, [], expiration);
+ var client = new oauth2.Client('identifier', 'secret', credentials,
+ httpClient: httpClient);
+
+ expect(client.get(requestUri), throwsExpirationException);
+ });
+
+ test("that can be refreshed refreshes the credentials and sends the "
+ "request", () {
+ var expiration = new Date.now().subtract(new Duration(hours: 1));
+ var credentials = new oauth2.Credentials(
+ 'access token', 'refresh token', tokenEndpoint, [], expiration);
+ var client = new oauth2.Client('identifier', 'secret', credentials,
+ httpClient: httpClient);
+
+ httpClient.expectRequest((request) {
+ expect(request.method, equals('POST'));
+ expect(request.url.toString(), equals(tokenEndpoint.toString()));
+ return new Future.immediate(new http.Response(JSON.stringify({
+ 'access_token': 'new access token',
+ 'token_type': 'bearer'
+ }), 200, headers: {'content-type': 'application/json'}));
+ });
+
+ httpClient.expectRequest((request) {
+ expect(request.method, equals('GET'));
+ expect(request.url.toString(), equals(requestUri.toString()));
+ expect(request.headers['authorization'],
+ equals('Bearer new access token'));
+
+ return new Future.immediate(new http.Response('good job', 200));
+ });
+
+ expect(client.read(requestUri).transform((_) {
+ expect(client.credentials.accessToken, equals('new access token'));
+ }), completes);
+ });
+ });
+
+ 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,
+ httpClient: httpClient);
+
+ httpClient.expectRequest((request) {
+ expect(request.method, equals('GET'));
+ expect(request.url.toString(), equals(requestUri.toString()));
+ expect(request.headers['authorization'],
+ equals('Bearer access token'));
+
+ return new Future.immediate(new http.Response('good job', 200));
+ });
+
+ expect(client.read(requestUri), completion(equals('good job')));
+ });
+
+ test("can manually refresh the credentials", () {
+ var credentials = new oauth2.Credentials(
+ 'access token', 'refresh token', tokenEndpoint);
+ var client = new oauth2.Client('identifier', 'secret', credentials,
+ httpClient: httpClient);
+
+ httpClient.expectRequest((request) {
+ expect(request.method, equals('POST'));
+ expect(request.url.toString(), equals(tokenEndpoint.toString()));
+ return new Future.immediate(new http.Response(JSON.stringify({
+ 'access_token': 'new access token',
+ 'token_type': 'bearer'
+ }), 200, headers: {'content-type': 'application/json'}));
+ });
+
+ expect(client.refreshCredentials().transform((_) {
+ expect(client.credentials.accessToken, equals('new access token'));
+ }), completes);
+ });
+
+ test("without a refresh token can't manually refresh the credentials", () {
+ var credentials = new oauth2.Credentials('access token');
+ var client = new oauth2.Client('identifier', 'secret', credentials,
+ httpClient: httpClient);
+
+ expect(client.refreshCredentials(), throwsStateError);
+ });
+ });
+}
« no previous file with comments | « pkg/oauth2/test/authorization_code_grant_test.dart ('k') | pkg/oauth2/test/credentials_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698