| 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:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 }); | 49 }); |
| 50 | 50 |
| 51 test("can't refresh without a token endpoint", () { | 51 test("can't refresh without a token endpoint", () { |
| 52 var credentials = new oauth2.Credentials('access token', 'refresh token'); | 52 var credentials = new oauth2.Credentials('access token', 'refresh token'); |
| 53 expect(credentials.canRefresh, false); | 53 expect(credentials.canRefresh, false); |
| 54 | 54 |
| 55 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), | 55 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient), |
| 56 throwsStateError); | 56 throwsStateError); |
| 57 }); | 57 }); |
| 58 | 58 |
| 59 test("can refresh with a refresh token and a token endpoint", () { | 59 test("can refresh with a refresh token and a token endpoint", () async { |
| 60 var credentials = new oauth2.Credentials( | 60 var credentials = new oauth2.Credentials( |
| 61 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']); | 61 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2']); |
| 62 expect(credentials.canRefresh, true); | 62 expect(credentials.canRefresh, true); |
| 63 | 63 |
| 64 httpClient.expectRequest((request) { | 64 httpClient.expectRequest((request) { |
| 65 expect(request.method, equals('POST')); | 65 expect(request.method, equals('POST')); |
| 66 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 66 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 67 expect(request.bodyFields, equals({ | 67 expect(request.bodyFields, equals({ |
| 68 "grant_type": "refresh_token", | 68 "grant_type": "refresh_token", |
| 69 "refresh_token": "refresh token", | 69 "refresh_token": "refresh token", |
| 70 "scope": "scope1 scope2", | 70 "scope": "scope1 scope2", |
| 71 "client_id": "identifier", | 71 "client_id": "identifier", |
| 72 "client_secret": "secret" | 72 "client_secret": "secret" |
| 73 })); | 73 })); |
| 74 | 74 |
| 75 return new Future.value(new http.Response(JSON.encode({ | 75 return new Future.value(new http.Response(JSON.encode({ |
| 76 'access_token': 'new access token', | 76 'access_token': 'new access token', |
| 77 'token_type': 'bearer', | 77 'token_type': 'bearer', |
| 78 'refresh_token': 'new refresh token' | 78 'refresh_token': 'new refresh token' |
| 79 }), 200, headers: {'content-type': 'application/json'})); | 79 }), 200, headers: {'content-type': 'application/json'})); |
| 80 }); | 80 }); |
| 81 | 81 |
| 82 | 82 |
| 83 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 83 credentials = await credentials.refresh('identifier', 'secret', |
| 84 .then((credentials) { | 84 httpClient: httpClient); |
| 85 expect(credentials.accessToken, equals('new access token')); | 85 expect(credentials.accessToken, equals('new access token')); |
| 86 expect(credentials.refreshToken, equals('new refresh token')); | 86 expect(credentials.refreshToken, equals('new refresh token')); |
| 87 }), completes); | |
| 88 }); | 87 }); |
| 89 | 88 |
| 90 test("uses the old refresh token if a new one isn't provided", () { | 89 test("uses the old refresh token if a new one isn't provided", () async { |
| 91 var credentials = new oauth2.Credentials( | 90 var credentials = new oauth2.Credentials( |
| 92 'access token', 'refresh token', tokenEndpoint); | 91 'access token', 'refresh token', tokenEndpoint); |
| 93 expect(credentials.canRefresh, true); | 92 expect(credentials.canRefresh, true); |
| 94 | 93 |
| 95 httpClient.expectRequest((request) { | 94 httpClient.expectRequest((request) { |
| 96 expect(request.method, equals('POST')); | 95 expect(request.method, equals('POST')); |
| 97 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 96 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 98 expect(request.bodyFields, equals({ | 97 expect(request.bodyFields, equals({ |
| 99 "grant_type": "refresh_token", | 98 "grant_type": "refresh_token", |
| 100 "refresh_token": "refresh token", | 99 "refresh_token": "refresh token", |
| 101 "client_id": "identifier", | 100 "client_id": "identifier", |
| 102 "client_secret": "secret" | 101 "client_secret": "secret" |
| 103 })); | 102 })); |
| 104 | 103 |
| 105 return new Future.value(new http.Response(JSON.encode({ | 104 return new Future.value(new http.Response(JSON.encode({ |
| 106 'access_token': 'new access token', | 105 'access_token': 'new access token', |
| 107 'token_type': 'bearer' | 106 'token_type': 'bearer' |
| 108 }), 200, headers: {'content-type': 'application/json'})); | 107 }), 200, headers: {'content-type': 'application/json'})); |
| 109 }); | 108 }); |
| 110 | 109 |
| 111 | 110 |
| 112 expect(credentials.refresh('identifier', 'secret', httpClient: httpClient) | 111 credentials = await credentials.refresh('identifier', 'secret', |
| 113 .then((credentials) { | 112 httpClient: httpClient); |
| 114 expect(credentials.accessToken, equals('new access token')); | 113 expect(credentials.accessToken, equals('new access token')); |
| 115 expect(credentials.refreshToken, equals('refresh token')); | 114 expect(credentials.refreshToken, equals('refresh token')); |
| 116 }), completes); | |
| 117 }); | 115 }); |
| 118 | 116 |
| 119 group("fromJson", () { | 117 group("fromJson", () { |
| 120 oauth2.Credentials fromMap(Map map) => | 118 oauth2.Credentials fromMap(Map map) => |
| 121 new oauth2.Credentials.fromJson(JSON.encode(map)); | 119 new oauth2.Credentials.fromJson(JSON.encode(map)); |
| 122 | 120 |
| 123 test("should load the same credentials from toJson", () { | 121 test("should load the same credentials from toJson", () { |
| 124 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 122 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
| 125 var credentials = new oauth2.Credentials( | 123 var credentials = new oauth2.Credentials( |
| 126 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'], | 124 'access token', 'refresh token', tokenEndpoint, ['scope1', 'scope2'], |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), | 165 expect(() => fromMap({"accessToken": "foo", "scopes": 12}), |
| 168 throwsFormatException); | 166 throwsFormatException); |
| 169 }); | 167 }); |
| 170 | 168 |
| 171 test("should throw a FormatException if expiration is not an int", () { | 169 test("should throw a FormatException if expiration is not an int", () { |
| 172 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), | 170 expect(() => fromMap({"accessToken": "foo", "expiration": "12"}), |
| 173 throwsFormatException); | 171 throwsFormatException); |
| 174 }); | 172 }); |
| 175 }); | 173 }); |
| 176 } | 174 } |
| OLD | NEW |