| 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:json' as JSON; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| 11 import 'package:oauth2/oauth2.dart' as oauth2; | 11 import 'package:oauth2/oauth2.dart' as oauth2; |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 | 13 |
| 14 import 'utils.dart'; | 14 import 'utils.dart'; |
| 15 | 15 |
| 16 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); | 16 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); |
| 17 | 17 |
| 18 ExpectClient httpClient; | 18 ExpectClient httpClient; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 expect(request.method, equals('POST')); | 67 expect(request.method, equals('POST')); |
| 68 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 68 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 69 expect(request.bodyFields, equals({ | 69 expect(request.bodyFields, equals({ |
| 70 "grant_type": "refresh_token", | 70 "grant_type": "refresh_token", |
| 71 "refresh_token": "refresh token", | 71 "refresh_token": "refresh token", |
| 72 "scope": "scope1 scope2", | 72 "scope": "scope1 scope2", |
| 73 "client_id": "identifier", | 73 "client_id": "identifier", |
| 74 "client_secret": "secret" | 74 "client_secret": "secret" |
| 75 })); | 75 })); |
| 76 | 76 |
| 77 return new Future.value(new http.Response(JSON.stringify({ | 77 return new Future.value(new http.Response(JSON.encode({ |
| 78 'access_token': 'new access token', | 78 'access_token': 'new access token', |
| 79 'token_type': 'bearer', | 79 'token_type': 'bearer', |
| 80 'refresh_token': 'new refresh token' | 80 'refresh_token': 'new refresh token' |
| 81 }), 200, headers: {'content-type': 'application/json'})); | 81 }), 200, headers: {'content-type': 'application/json'})); |
| 82 }); | 82 }); |
| 83 | 83 |
| 84 | 84 |
| 85 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 85 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) |
| 86 .then((credentials) { | 86 .then((credentials) { |
| 87 expect(credentials.accessToken, equals('new access token')); | 87 expect(credentials.accessToken, equals('new access token')); |
| 88 expect(credentials.refreshToken, equals('new refresh token')); | 88 expect(credentials.refreshToken, equals('new refresh token')); |
| 89 }), completes); | 89 }), completes); |
| 90 }); | 90 }); |
| 91 | 91 |
| 92 test("uses the old refresh token if a new one isn't provided", () { | 92 test("uses the old refresh token if a new one isn't provided", () { |
| 93 var credentials = new oauth2.Credentials( | 93 var credentials = new oauth2.Credentials( |
| 94 'access token', 'refresh token', tokenEndpoint); | 94 'access token', 'refresh token', tokenEndpoint); |
| 95 expect(credentials.canRefresh, true); | 95 expect(credentials.canRefresh, true); |
| 96 | 96 |
| 97 httpClient.expectRequest((request) { | 97 httpClient.expectRequest((request) { |
| 98 expect(request.method, equals('POST')); | 98 expect(request.method, equals('POST')); |
| 99 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 99 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 100 expect(request.bodyFields, equals({ | 100 expect(request.bodyFields, equals({ |
| 101 "grant_type": "refresh_token", | 101 "grant_type": "refresh_token", |
| 102 "refresh_token": "refresh token", | 102 "refresh_token": "refresh token", |
| 103 "client_id": "identifier", | 103 "client_id": "identifier", |
| 104 "client_secret": "secret" | 104 "client_secret": "secret" |
| 105 })); | 105 })); |
| 106 | 106 |
| 107 return new Future.value(new http.Response(JSON.stringify({ | 107 return new Future.value(new http.Response(JSON.encode({ |
| 108 'access_token': 'new access token', | 108 'access_token': 'new access token', |
| 109 'token_type': 'bearer' | 109 'token_type': 'bearer' |
| 110 }), 200, headers: {'content-type': 'application/json'})); | 110 }), 200, headers: {'content-type': 'application/json'})); |
| 111 }); | 111 }); |
| 112 | 112 |
| 113 | 113 |
| 114 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 114 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) |
| 115 .then((credentials) { | 115 .then((credentials) { |
| 116 expect(credentials.accessToken, equals('new access token')); | 116 expect(credentials.accessToken, equals('new access token')); |
| 117 expect(credentials.refreshToken, equals('refresh token')); | 117 expect(credentials.refreshToken, equals('refresh token')); |
| 118 }), completes); | 118 }), completes); |
| 119 }); | 119 }); |
| 120 | 120 |
| 121 group("fromJson", () { | 121 group("fromJson", () { |
| 122 oauth2.Credentials fromMap(Map map) => | 122 oauth2.Credentials fromMap(Map map) => |
| 123 new oauth2.Credentials.fromJson(JSON.stringify(map)); | 123 new oauth2.Credentials.fromJson(JSON.encode(map)); |
| 124 | 124 |
| 125 test("should load the same credentials from toJson", () { | 125 test("should load the same credentials from toJson", () { |
| 126 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 126 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 127 var credentials = new oauth2.Credentials( | 127 var credentials = new oauth2.Credentials( |
| 128 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'], | 128 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'], |
| 129 expiration); | 129 expiration); |
| 130 var reloaded = new oauth2.Credentials.fromJson(credentials.toJson()); | 130 var reloaded = new oauth2.Credentials.fromJson(credentials.toJson()); |
| 131 | 131 |
| 132 expect(reloaded.accessToken, equals(credentials.accessToken)); | 132 expect(reloaded.accessToken, equals(credentials.accessToken)); |
| 133 expect(reloaded.refreshToken, equals(credentials.refreshToken)); | 133 expect(reloaded.refreshToken, equals(credentials.refreshToken)); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), | 169 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), |
| 170 throwsFormatException); | 170 throwsFormatException); |
| 171 }); | 171 }); |
| 172 | 172 |
| 173 test("should throw a FormatException if expiration is not an int", () { | 173 test("should throw a FormatException if expiration is not an int", () { |
| 174 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), | 174 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), |
| 175 throwsFormatException); | 175 throwsFormatException); |
| 176 }); | 176 }); |
| 177 }); | 177 }); |
| 178 } | 178 } |
| OLD | NEW |