| 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; | |
| 6 | |
| 7 import 'dart:async'; | 5 import 'dart:async'; |
| 8 import 'dart:convert'; | 6 import 'dart:convert'; |
| 9 | 7 |
| 10 import 'package:http/http.dart' as http; | 8 import 'package:http/http.dart' as http; |
| 11 import 'package:oauth2/oauth2.dart' as oauth2; | 9 import 'package:oauth2/oauth2.dart' as oauth2; |
| 12 import 'package:test/test.dart'; | 10 import 'package:test/test.dart'; |
| 13 | 11 |
| 14 import 'utils.dart'; | 12 import 'utils.dart'; |
| 15 | 13 |
| 16 final Uri requestUri = Uri.parse("http://example.com/resource"); | 14 final Uri requestUri = Uri.parse("http://example.com/resource"); |
| 17 | 15 |
| 18 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); | 16 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); |
| 19 | 17 |
| 20 ExpectClient httpClient; | 18 void main() { |
| 19 var httpClient; |
| 20 setUp(() => httpClient = new ExpectClient()); |
| 21 | 21 |
| 22 void createHttpClient() { | |
| 23 httpClient = new ExpectClient(); | |
| 24 } | |
| 25 | |
| 26 void main() { | |
| 27 group('with expired credentials', () { | 22 group('with expired credentials', () { |
| 28 setUp(createHttpClient); | |
| 29 | |
| 30 test("that can't be refreshed throws an ExpirationException on send", () { | 23 test("that can't be refreshed throws an ExpirationException on send", () { |
| 31 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 24 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 32 var credentials = new oauth2.Credentials( | 25 var credentials = new oauth2.Credentials( |
| 33 'access token', null, null, [], expiration); | 26 'access token', null, null, [], expiration); |
| 34 var client = new oauth2.Client('identifier', 'secret', credentials, | 27 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 35 httpClient: httpClient); | 28 httpClient: httpClient); |
| 36 | 29 |
| 37 expect(client.get(requestUri), | 30 expect(client.get(requestUri), |
| 38 throwsA(new isInstanceOf<oauth2.ExpirationException>())); | 31 throwsA(new isInstanceOf<oauth2.ExpirationException>())); |
| 39 }); | 32 }); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 63 | 56 |
| 64 return new Future.value(new http.Response('good job', 200)); | 57 return new Future.value(new http.Response('good job', 200)); |
| 65 }); | 58 }); |
| 66 | 59 |
| 67 await client.read(requestUri); | 60 await client.read(requestUri); |
| 68 expect(client.credentials.accessToken, equals('new access token')); | 61 expect(client.credentials.accessToken, equals('new access token')); |
| 69 }); | 62 }); |
| 70 }); | 63 }); |
| 71 | 64 |
| 72 group('with valid credentials', () { | 65 group('with valid credentials', () { |
| 73 setUp(createHttpClient); | |
| 74 | |
| 75 test("sends a request with bearer authorization", () { | 66 test("sends a request with bearer authorization", () { |
| 76 var credentials = new oauth2.Credentials('access token'); | 67 var credentials = new oauth2.Credentials('access token'); |
| 77 var client = new oauth2.Client('identifier', 'secret', credentials, | 68 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 78 httpClient: httpClient); | 69 httpClient: httpClient); |
| 79 | 70 |
| 80 httpClient.expectRequest((request) { | 71 httpClient.expectRequest((request) { |
| 81 expect(request.method, equals('GET')); | 72 expect(request.method, equals('GET')); |
| 82 expect(request.url.toString(), equals(requestUri.toString())); | 73 expect(request.url.toString(), equals(requestUri.toString())); |
| 83 expect(request.headers['authorization'], equals('Bearer access token')); | 74 expect(request.headers['authorization'], equals('Bearer access token')); |
| 84 | 75 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 110 test("without a refresh token can't manually refresh the credentials", () { | 101 test("without a refresh token can't manually refresh the credentials", () { |
| 111 var credentials = new oauth2.Credentials('access token'); | 102 var credentials = new oauth2.Credentials('access token'); |
| 112 var client = new oauth2.Client('identifier', 'secret', credentials, | 103 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 113 httpClient: httpClient); | 104 httpClient: httpClient); |
| 114 | 105 |
| 115 expect(client.refreshCredentials(), throwsA(isStateError)); | 106 expect(client.refreshCredentials(), throwsA(isStateError)); |
| 116 }); | 107 }); |
| 117 }); | 108 }); |
| 118 | 109 |
| 119 group('with invalid credentials', () { | 110 group('with invalid credentials', () { |
| 120 setUp(createHttpClient); | |
| 121 | |
| 122 test('throws an AuthorizationException for a 401 response', () { | 111 test('throws an AuthorizationException for a 401 response', () { |
| 123 var credentials = new oauth2.Credentials('access token'); | 112 var credentials = new oauth2.Credentials('access token'); |
| 124 var client = new oauth2.Client('identifier', 'secret', credentials, | 113 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 125 httpClient: httpClient); | 114 httpClient: httpClient); |
| 126 | 115 |
| 127 httpClient.expectRequest((request) { | 116 httpClient.expectRequest((request) { |
| 128 expect(request.method, equals('GET')); | 117 expect(request.method, equals('GET')); |
| 129 expect(request.url.toString(), equals(requestUri.toString())); | 118 expect(request.url.toString(), equals(requestUri.toString())); |
| 130 expect(request.headers['authorization'], equals('Bearer access token')); | 119 expect(request.headers['authorization'], equals('Bearer access token')); |
| 131 | 120 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 equals('Bearer access token')); | 198 equals('Bearer access token')); |
| 210 | 199 |
| 211 return new Future.value(new http.Response('bad job', 401, | 200 return new Future.value(new http.Response('bad job', 401, |
| 212 headers: {'www-authenticate': 'Bearer'})); | 201 headers: {'www-authenticate': 'Bearer'})); |
| 213 }); | 202 }); |
| 214 | 203 |
| 215 expect((await client.get(requestUri)).statusCode, equals(401)); | 204 expect((await client.get(requestUri)).statusCode, equals(401)); |
| 216 }); | 205 }); |
| 217 }); | 206 }); |
| 218 } | 207 } |
| OLD | NEW |