| 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 credentials_test; |    5 library credentials_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 '../../unittest/lib/unittest.dart'; |   12 import '../../unittest/lib/unittest.dart'; | 
|   13 import '../../http/lib/http.dart' as http; |   13 import '../../http/lib/http.dart' as http; | 
|   14 import '../lib/oauth2.dart' as oauth2; |   14 import '../lib/oauth2.dart' as oauth2; | 
|   15 import 'utils.dart'; |   15 import 'utils.dart'; | 
|   16  |   16  | 
|   17 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token'); |   17 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token'); | 
|   18  |   18  | 
|   19 ExpectClient httpClient; |   19 ExpectClient httpClient; | 
|   20  |   20  | 
|   21 void main() { |   21 void main() { | 
|   22   setUp(() => httpClient = new ExpectClient()); |   22   setUp(() => httpClient = new ExpectClient()); | 
|   23  |   23  | 
|   24   test('is not expired if no expiration exists', () { |   24   test('is not expired if no expiration exists', () { | 
|   25     var credentials = new oauth2.Credentials('access token'); |   25     var credentials = new oauth2.Credentials('access token'); | 
|   26     expect(credentials.isExpired, isFalse); |   26     expect(credentials.isExpired, isFalse); | 
|   27   }); |   27   }); | 
|   28  |   28  | 
|   29   test('is not expired if the expiration is in the future', () { |   29   test('is not expired if the expiration is in the future', () { | 
|   30     var expiration = new Date.now().add(new Duration(hours: 1)); |   30     var expiration = new DateTime.now().add(new Duration(hours: 1)); | 
|   31     var credentials = new oauth2.Credentials( |   31     var credentials = new oauth2.Credentials( | 
|   32         'access token', null, null, null, expiration); |   32         'access token', null, null, null, expiration); | 
|   33     expect(credentials.isExpired, isFalse); |   33     expect(credentials.isExpired, isFalse); | 
|   34   }); |   34   }); | 
|   35  |   35  | 
|   36   test('is expired if the expiration is in the past', () { |   36   test('is expired if the expiration is in the past', () { | 
|   37     var expiration = new Date.now().subtract(new Duration(hours: 1)); |   37     var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 
|   38     var credentials = new oauth2.Credentials( |   38     var credentials = new oauth2.Credentials( | 
|   39         'access token', null, null, null, expiration); |   39         'access token', null, null, null, expiration); | 
|   40     expect(credentials.isExpired, isTrue); |   40     expect(credentials.isExpired, isTrue); | 
|   41   }); |   41   }); | 
|   42  |   42  | 
|   43   test("can't refresh without a refresh token", () { |   43   test("can't refresh without a refresh token", () { | 
|   44     var credentials = new oauth2.Credentials( |   44     var credentials = new oauth2.Credentials( | 
|   45         'access token', null, tokenEndpoint); |   45         'access token', null, tokenEndpoint); | 
|   46     expect(credentials.canRefresh, false); |   46     expect(credentials.canRefresh, false); | 
|   47     credentials.refresh('identifier', 'secret', httpClient: httpClient) |   47     credentials.refresh('identifier', 'secret', httpClient: httpClient) | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  117       expect(credentials.accessToken, equals('new access token')); |  117       expect(credentials.accessToken, equals('new access token')); | 
|  118       expect(credentials.refreshToken, equals('refresh token')); |  118       expect(credentials.refreshToken, equals('refresh token')); | 
|  119     }), completes); |  119     }), completes); | 
|  120   }); |  120   }); | 
|  121  |  121  | 
|  122   group("fromJson", () { |  122   group("fromJson", () { | 
|  123     oauth2.Credentials fromMap(Map map) => |  123     oauth2.Credentials fromMap(Map map) => | 
|  124       new oauth2.Credentials.fromJson(JSON.stringify(map)); |  124       new oauth2.Credentials.fromJson(JSON.stringify(map)); | 
|  125  |  125  | 
|  126     test("should load the same credentials from toJson", () { |  126     test("should load the same credentials from toJson", () { | 
|  127       var expiration = new Date.now().subtract(new Duration(hours: 1)); |  127       var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 
|  128       var credentials = new oauth2.Credentials( |  128       var credentials = new oauth2.Credentials( | 
|  129           'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'], |  129           'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'], | 
|  130           expiration); |  130           expiration); | 
|  131       var reloaded = new oauth2.Credentials.fromJson(credentials.toJson()); |  131       var reloaded = new oauth2.Credentials.fromJson(credentials.toJson()); | 
|  132  |  132  | 
|  133       expect(reloaded.accessToken, equals(credentials.accessToken)); |  133       expect(reloaded.accessToken, equals(credentials.accessToken)); | 
|  134       expect(reloaded.refreshToken, equals(credentials.refreshToken)); |  134       expect(reloaded.refreshToken, equals(credentials.refreshToken)); | 
|  135       expect(reloaded.tokenEndpoint.toString(), |  135       expect(reloaded.tokenEndpoint.toString(), | 
|  136           equals(credentials.tokenEndpoint.toString())); |  136           equals(credentials.tokenEndpoint.toString())); | 
|  137       expect(reloaded.scopes, equals(credentials.scopes)); |  137       expect(reloaded.scopes, equals(credentials.scopes)); | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  170       expect(() => fromMap({"accessToken": "foo", "scopes": 12}), |  170       expect(() => fromMap({"accessToken": "foo", "scopes": 12}), | 
|  171           throwsFormatException); |  171           throwsFormatException); | 
|  172     }); |  172     }); | 
|  173  |  173  | 
|  174     test("should throw a FormatException if expiration is not an int", () { |  174     test("should throw a FormatException if expiration is not an int", () { | 
|  175       expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), |  175       expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), | 
|  176           throwsFormatException); |  176           throwsFormatException); | 
|  177     }); |  177     }); | 
|  178   }); |  178   }); | 
|  179 } |  179 } | 
| OLD | NEW |