| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library client_test; | 5 library client_test; |
| 6 | 6 |
| 7 import 'dart:async'; |
| 7 import 'dart:io'; | 8 import 'dart:io'; |
| 8 import 'dart:json'; | 9 import 'dart:json' as JSON; |
| 9 import 'dart:uri'; | 10 import 'dart:uri'; |
| 10 | 11 |
| 11 import '../../unittest/lib/unittest.dart'; | 12 import '../../unittest/lib/unittest.dart'; |
| 12 import '../../http/lib/http.dart' as http; | 13 import '../../http/lib/http.dart' as http; |
| 13 import '../lib/oauth2.dart' as oauth2; | 14 import '../lib/oauth2.dart' as oauth2; |
| 14 import 'utils.dart'; | 15 import 'utils.dart'; |
| 15 | 16 |
| 16 final Uri requestUri = new Uri.fromString("http://example.com/resource"); | 17 final Uri requestUri = new Uri.fromString("http://example.com/resource"); |
| 17 | 18 |
| 18 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token'); | 19 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token'); |
| 19 | 20 |
| 20 ExpectClient httpClient; | 21 ExpectClient httpClient; |
| 21 | 22 |
| 22 void createHttpClient() { | 23 void createHttpClient() { |
| 23 httpClient = new ExpectClient(); | 24 httpClient = new ExpectClient(); |
| 24 } | 25 } |
| 25 | 26 |
| 27 void expectFutureThrows(future, predicate) { |
| 28 future.catchError(expectAsync1((AsyncError e) { |
| 29 expect(predicate(e.error), isTrue); |
| 30 })); |
| 31 } |
| 32 |
| 26 void main() { | 33 void main() { |
| 27 group('with expired credentials', () { | 34 group('with expired credentials', () { |
| 28 setUp(createHttpClient); | 35 setUp(createHttpClient); |
| 29 | 36 |
| 30 test("that can't be refreshed throws an ExpirationException on send", () { | 37 test("that can't be refreshed throws an ExpirationException on send", () { |
| 31 var expiration = new Date.now().subtract(new Duration(hours: 1)); | 38 var expiration = new Date.now().subtract(new Duration(hours: 1)); |
| 32 var credentials = new oauth2.Credentials( | 39 var credentials = new oauth2.Credentials( |
| 33 'access token', null, null, [], expiration); | 40 'access token', null, null, [], expiration); |
| 34 var client = new oauth2.Client('identifier', 'secret', credentials, | 41 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 35 httpClient: httpClient); | 42 httpClient: httpClient); |
| 36 | 43 |
| 37 expect(client.get(requestUri), throwsExpirationException); | 44 expectFutureThrows(client.get(requestUri), |
| 45 (e) => e is oauth2.ExpirationException); |
| 38 }); | 46 }); |
| 39 | 47 |
| 40 test("that can be refreshed refreshes the credentials and sends the " | 48 test("that can be refreshed refreshes the credentials and sends the " |
| 41 "request", () { | 49 "request", () { |
| 42 var expiration = new Date.now().subtract(new Duration(hours: 1)); | 50 var expiration = new Date.now().subtract(new Duration(hours: 1)); |
| 43 var credentials = new oauth2.Credentials( | 51 var credentials = new oauth2.Credentials( |
| 44 'access token', 'refresh token', tokenEndpoint, [], expiration); | 52 'access token', 'refresh token', tokenEndpoint, [], expiration); |
| 45 var client = new oauth2.Client('identifier', 'secret', credentials, | 53 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 46 httpClient: httpClient); | 54 httpClient: httpClient); |
| 47 | 55 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 expect(client.refreshCredentials().then((_) { | 115 expect(client.refreshCredentials().then((_) { |
| 108 expect(client.credentials.accessToken, equals('new access token')); | 116 expect(client.credentials.accessToken, equals('new access token')); |
| 109 }), completes); | 117 }), completes); |
| 110 }); | 118 }); |
| 111 | 119 |
| 112 test("without a refresh token can't manually refresh the credentials", () { | 120 test("without a refresh token can't manually refresh the credentials", () { |
| 113 var credentials = new oauth2.Credentials('access token'); | 121 var credentials = new oauth2.Credentials('access token'); |
| 114 var client = new oauth2.Client('identifier', 'secret', credentials, | 122 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 115 httpClient: httpClient); | 123 httpClient: httpClient); |
| 116 | 124 |
| 117 expect(client.refreshCredentials(), throwsStateError); | 125 expectFutureThrows(client.refreshCredentials(), |
| 126 (e) => e is StateError); |
| 118 }); | 127 }); |
| 119 }); | 128 }); |
| 120 | 129 |
| 121 group('with invalid credentials', () { | 130 group('with invalid credentials', () { |
| 122 setUp(createHttpClient); | 131 setUp(createHttpClient); |
| 123 | 132 |
| 124 test('throws an AuthorizationException for a 401 response', () { | 133 test('throws an AuthorizationException for a 401 response', () { |
| 125 var credentials = new oauth2.Credentials('access token'); | 134 var credentials = new oauth2.Credentials('access token'); |
| 126 var client = new oauth2.Client('identifier', 'secret', credentials, | 135 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 127 httpClient: httpClient); | 136 httpClient: httpClient); |
| 128 | 137 |
| 129 httpClient.expectRequest((request) { | 138 httpClient.expectRequest((request) { |
| 130 expect(request.method, equals('GET')); | 139 expect(request.method, equals('GET')); |
| 131 expect(request.url.toString(), equals(requestUri.toString())); | 140 expect(request.url.toString(), equals(requestUri.toString())); |
| 132 expect(request.headers['authorization'], | 141 expect(request.headers['authorization'], |
| 133 equals('Bearer access token')); | 142 equals('Bearer access token')); |
| 134 | 143 |
| 135 var authenticate = 'Bearer error="invalid_token", error_description=' | 144 var authenticate = 'Bearer error="invalid_token", error_description=' |
| 136 '"Something is terribly wrong."'; | 145 '"Something is terribly wrong."'; |
| 137 return new Future.immediate(new http.Response('bad job', 401, | 146 return new Future.immediate(new http.Response('bad job', 401, |
| 138 headers: {'www-authenticate': authenticate})); | 147 headers: {'www-authenticate': authenticate})); |
| 139 }); | 148 }); |
| 140 | 149 |
| 141 expect(client.read(requestUri), throwsAuthorizationException); | 150 expectFutureThrows(client.read(requestUri), |
| 151 (e) => e is oauth2.AuthorizationException); |
| 142 }); | 152 }); |
| 143 | 153 |
| 144 test('passes through a 401 response without www-authenticate', () { | 154 test('passes through a 401 response without www-authenticate', () { |
| 145 var credentials = new oauth2.Credentials('access token'); | 155 var credentials = new oauth2.Credentials('access token'); |
| 146 var client = new oauth2.Client('identifier', 'secret', credentials, | 156 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 147 httpClient: httpClient); | 157 httpClient: httpClient); |
| 148 | 158 |
| 149 httpClient.expectRequest((request) { | 159 httpClient.expectRequest((request) { |
| 150 expect(request.method, equals('GET')); | 160 expect(request.method, equals('GET')); |
| 151 expect(request.url.toString(), equals(requestUri.toString())); | 161 expect(request.url.toString(), equals(requestUri.toString())); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 return new Future.immediate(new http.Response('bad job', 401, | 226 return new Future.immediate(new http.Response('bad job', 401, |
| 217 headers: {'www-authenticate': 'Bearer'})); | 227 headers: {'www-authenticate': 'Bearer'})); |
| 218 }); | 228 }); |
| 219 | 229 |
| 220 expect( | 230 expect( |
| 221 client.get(requestUri).then((response) => response.statusCode), | 231 client.get(requestUri).then((response) => response.statusCode), |
| 222 completion(equals(401))); | 232 completion(equals(401))); |
| 223 }); | 233 }); |
| 224 }); | 234 }); |
| 225 } | 235 } |
| OLD | NEW |