| 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'; | |
| 9 import 'dart:json' as JSON; | 8 import 'dart:json' as JSON; |
| 10 | 9 |
| 11 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| 12 import 'package:oauth2/oauth2.dart' as oauth2; | 11 import 'package:oauth2/oauth2.dart' as oauth2; |
| 13 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 14 | 13 |
| 15 import 'utils.dart'; | 14 import 'utils.dart'; |
| 16 | 15 |
| 17 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); | 16 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); |
| 18 | 17 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 "client_secret": "secret" | 74 "client_secret": "secret" |
| 76 })); | 75 })); |
| 77 | 76 |
| 78 return new Future.value(new http.Response(JSON.stringify({ | 77 return new Future.value(new http.Response(JSON.stringify({ |
| 79 'access_token': 'new access token', | 78 'access_token': 'new access token', |
| 80 'token_type': 'bearer', | 79 'token_type': 'bearer', |
| 81 'refresh_token': 'new refresh token' | 80 'refresh_token': 'new refresh token' |
| 82 }), 200, headers: {'content-type': 'application/json'})); | 81 }), 200, headers: {'content-type': 'application/json'})); |
| 83 }); | 82 }); |
| 84 | 83 |
| 85 | 84 |
| 86 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 85 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) |
| 87 .then((credentials) { | 86 .then((credentials) { |
| 88 expect(credentials.accessToken, equals('new access token')); | 87 expect(credentials.accessToken, equals('new access token')); |
| 89 expect(credentials.refreshToken, equals('new refresh token')); | 88 expect(credentials.refreshToken, equals('new refresh token')); |
| 90 }), completes); | 89 }), completes); |
| 91 }); | 90 }); |
| 92 | 91 |
| 93 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", () { |
| 94 var credentials = new oauth2.Credentials( | 93 var credentials = new oauth2.Credentials( |
| 95 'access token', 'refresh token', tokenEndpoint); | 94 'access token', 'refresh token', tokenEndpoint); |
| 96 expect(credentials.canRefresh, true); | 95 expect(credentials.canRefresh, true); |
| 97 | 96 |
| 98 httpClient.expectRequest((request) { | 97 httpClient.expectRequest((request) { |
| 99 expect(request.method, equals('POST')); | 98 expect(request.method, equals('POST')); |
| 100 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 99 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 101 expect(request.bodyFields, equals({ | 100 expect(request.bodyFields, equals({ |
| 102 "grant_type": "refresh_token", | 101 "grant_type": "refresh_token", |
| 103 "refresh_token": "refresh token", | 102 "refresh_token": "refresh token", |
| 104 "client_id": "identifier", | 103 "client_id": "identifier", |
| 105 "client_secret": "secret" | 104 "client_secret": "secret" |
| 106 })); | 105 })); |
| 107 | 106 |
| 108 return new Future.value(new http.Response(JSON.stringify({ | 107 return new Future.value(new http.Response(JSON.stringify({ |
| 109 'access_token': 'new access token', | 108 'access_token': 'new access token', |
| 110 'token_type': 'bearer' | 109 'token_type': 'bearer' |
| 111 }), 200, headers: {'content-type': 'application/json'})); | 110 }), 200, headers: {'content-type': 'application/json'})); |
| 112 }); | 111 }); |
| 113 | 112 |
| 114 | 113 |
| 115 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 114 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) |
| 116 .then((credentials) { | 115 .then((credentials) { |
| 117 expect(credentials.accessToken, equals('new access token')); | 116 expect(credentials.accessToken, equals('new access token')); |
| 118 expect(credentials.refreshToken, equals('refresh token')); | 117 expect(credentials.refreshToken, equals('refresh token')); |
| 119 }), completes); | 118 }), completes); |
| 120 }); | 119 }); |
| 121 | 120 |
| 122 group("fromJson", () { | 121 group("fromJson", () { |
| 123 oauth2.Credentials fromMap(Map map) => | 122 oauth2.Credentials fromMap(Map map) => |
| 124 new oauth2.Credentials.fromJson(JSON.stringify(map)); | 123 new oauth2.Credentials.fromJson(JSON.stringify(map)); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), | 169 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), |
| 171 throwsFormatException); | 170 throwsFormatException); |
| 172 }); | 171 }); |
| 173 | 172 |
| 174 test("should throw a FormatException if expiration is not an int", () { | 173 test("should throw a FormatException if expiration is not an int", () { |
| 175 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), | 174 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), |
| 176 throwsFormatException); | 175 throwsFormatException); |
| 177 }); | 176 }); |
| 178 }); | 177 }); |
| 179 } | 178 } |
| OLD | NEW |