Chromium Code Reviews| 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:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:json' as JSON; | 9 import 'dart:json' as JSON; |
| 10 import 'dart:uri'; | 10 import 'dart:uri'; |
| 11 | 11 |
| 12 import 'package:http/http.dart' as http; | 12 import 'package:http/http.dart' as http; |
| 13 import 'package:oauth2/oauth2.dart' as oauth2; | 13 import 'package:oauth2/oauth2.dart' as oauth2; |
| 14 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 15 | 15 |
| 16 import 'utils.dart'; | 16 import 'utils.dart'; |
| 17 | 17 |
| 18 final Uri requestUri = Uri.parse("http://example.com/resource"); | 18 final Uri requestUri = Uri.parse("http://example.com/resource"); |
| 19 | 19 |
| 20 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); | 20 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); |
| 21 | 21 |
| 22 ExpectClient httpClient; | 22 ExpectClient httpClient; |
| 23 | 23 |
| 24 void createHttpClient() { | 24 void createHttpClient() { |
| 25 httpClient = new ExpectClient(); | 25 httpClient = new ExpectClient(); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void expectFutureThrows(future, predicate) { | |
| 29 future.catchError(expectAsync1((error) { | |
| 30 expect(predicate(error), isTrue); | |
| 31 })); | |
| 32 } | |
| 33 | |
| 34 void main() { | 28 void main() { |
| 35 group('with expired credentials', () { | 29 group('with expired credentials', () { |
| 36 setUp(createHttpClient); | 30 setUp(createHttpClient); |
| 37 | 31 |
| 38 test("that can't be refreshed throws an ExpirationException on send", () { | 32 test("that can't be refreshed throws an ExpirationException on send", () { |
| 39 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 33 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 40 var credentials = new oauth2.Credentials( | 34 var credentials = new oauth2.Credentials( |
| 41 'access token', null, null, [], expiration); | 35 'access token', null, null, [], expiration); |
| 42 var client = new oauth2.Client('identifier', 'secret', credentials, | 36 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 43 httpClient: httpClient); | 37 httpClient: httpClient); |
| 44 | 38 |
| 45 expectFutureThrows(client.get(requestUri), | 39 expect(client.get(requestUri), |
| 46 (e) => e is oauth2.ExpirationException); | 40 throwsA(new isInstanceOf<oauth2.ExpirationException>())); |
| 47 }); | 41 }); |
| 48 | 42 |
| 49 test("that can be refreshed refreshes the credentials and sends the " | 43 test("that can be refreshed refreshes the credentials and sends the " |
| 50 "request", () { | 44 "request", () { |
| 51 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 45 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 52 var credentials = new oauth2.Credentials( | 46 var credentials = new oauth2.Credentials( |
| 53 'access token', 'refresh token', tokenEndpoint, [], expiration); | 47 'access token', 'refresh token', tokenEndpoint, [], expiration); |
| 54 var client = new oauth2.Client('identifier', 'secret', credentials, | 48 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 55 httpClient: httpClient); | 49 httpClient: httpClient); |
| 56 | 50 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 expect(client.refreshCredentials().then((_) { | 110 expect(client.refreshCredentials().then((_) { |
| 117 expect(client.credentials.accessToken, equals('new access token')); | 111 expect(client.credentials.accessToken, equals('new access token')); |
| 118 }), completes); | 112 }), completes); |
| 119 }); | 113 }); |
| 120 | 114 |
| 121 test("without a refresh token can't manually refresh the credentials", () { | 115 test("without a refresh token can't manually refresh the credentials", () { |
| 122 var credentials = new oauth2.Credentials('access token'); | 116 var credentials = new oauth2.Credentials('access token'); |
| 123 var client = new oauth2.Client('identifier', 'secret', credentials, | 117 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 124 httpClient: httpClient); | 118 httpClient: httpClient); |
| 125 | 119 |
| 126 expectFutureThrows(client.refreshCredentials(), | 120 expect(client.refreshCredentials(), throwsA(isStateError)); |
|
Bob Nystrom
2013/04/30 21:56:54
You can use throwsStateError.
| |
| 127 (e) => e is StateError); | |
| 128 }); | 121 }); |
| 129 }); | 122 }); |
| 130 | 123 |
| 131 group('with invalid credentials', () { | 124 group('with invalid credentials', () { |
| 132 setUp(createHttpClient); | 125 setUp(createHttpClient); |
| 133 | 126 |
| 134 test('throws an AuthorizationException for a 401 response', () { | 127 test('throws an AuthorizationException for a 401 response', () { |
| 135 var credentials = new oauth2.Credentials('access token'); | 128 var credentials = new oauth2.Credentials('access token'); |
| 136 var client = new oauth2.Client('identifier', 'secret', credentials, | 129 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 137 httpClient: httpClient); | 130 httpClient: httpClient); |
| 138 | 131 |
| 139 httpClient.expectRequest((request) { | 132 httpClient.expectRequest((request) { |
| 140 expect(request.method, equals('GET')); | 133 expect(request.method, equals('GET')); |
| 141 expect(request.url.toString(), equals(requestUri.toString())); | 134 expect(request.url.toString(), equals(requestUri.toString())); |
| 142 expect(request.headers['authorization'], | 135 expect(request.headers['authorization'], |
| 143 equals('Bearer access token')); | 136 equals('Bearer access token')); |
| 144 | 137 |
| 145 var authenticate = 'Bearer error="invalid_token", error_description=' | 138 var authenticate = 'Bearer error="invalid_token", error_description=' |
| 146 '"Something is terribly wrong."'; | 139 '"Something is terribly wrong."'; |
| 147 return new Future.value(new http.Response('bad job', 401, | 140 return new Future.value(new http.Response('bad job', 401, |
| 148 headers: {'www-authenticate': authenticate})); | 141 headers: {'www-authenticate': authenticate})); |
| 149 }); | 142 }); |
| 150 | 143 |
| 151 expectFutureThrows(client.read(requestUri), | 144 expect(client.read(requestUri), |
| 152 (e) => e is oauth2.AuthorizationException); | 145 throwsA(new isInstanceOf<oauth2.AuthorizationException>())); |
| 153 }); | 146 }); |
| 154 | 147 |
| 155 test('passes through a 401 response without www-authenticate', () { | 148 test('passes through a 401 response without www-authenticate', () { |
| 156 var credentials = new oauth2.Credentials('access token'); | 149 var credentials = new oauth2.Credentials('access token'); |
| 157 var client = new oauth2.Client('identifier', 'secret', credentials, | 150 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 158 httpClient: httpClient); | 151 httpClient: httpClient); |
| 159 | 152 |
| 160 httpClient.expectRequest((request) { | 153 httpClient.expectRequest((request) { |
| 161 expect(request.method, equals('GET')); | 154 expect(request.method, equals('GET')); |
| 162 expect(request.url.toString(), equals(requestUri.toString())); | 155 expect(request.url.toString(), equals(requestUri.toString())); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 return new Future.value(new http.Response('bad job', 401, | 220 return new Future.value(new http.Response('bad job', 401, |
| 228 headers: {'www-authenticate': 'Bearer'})); | 221 headers: {'www-authenticate': 'Bearer'})); |
| 229 }); | 222 }); |
| 230 | 223 |
| 231 expect( | 224 expect( |
| 232 client.get(requestUri).then((response) => response.statusCode), | 225 client.get(requestUri).then((response) => response.statusCode), |
| 233 completion(equals(401))); | 226 completion(equals(401))); |
| 234 }); | 227 }); |
| 235 }); | 228 }); |
| 236 } | 229 } |
| OLD | NEW |