| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:convert'; | 6 import 'dart:convert'; |
| 7 | 7 |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
| 10 import 'package:oauth2/oauth2.dart' as oauth2; | 10 import 'package:oauth2/oauth2.dart' as oauth2; |
| 11 | 11 |
| 12 import 'utils.dart'; | 12 import 'utils.dart'; |
| 13 | 13 |
| 14 final redirectUrl = Uri.parse('http://example.com/redirect'); | 14 final redirectUrl = Uri.parse('http://example.com/redirect'); |
| 15 | 15 |
| 16 void main() { | 16 void main() { |
| 17 var client; | 17 var client; |
| 18 var grant; | 18 var grant; |
| 19 setUp(() { | 19 setUp(() { |
| 20 client = new ExpectClient(); | 20 client = new ExpectClient(); |
| 21 grant = new oauth2.AuthorizationCodeGrant( | 21 grant = new oauth2.AuthorizationCodeGrant( |
| 22 'identifier', | 22 'identifier', |
| 23 'secret', | |
| 24 Uri.parse('https://example.com/authorization'), | 23 Uri.parse('https://example.com/authorization'), |
| 25 Uri.parse('https://example.com/token'), | 24 Uri.parse('https://example.com/token'), |
| 25 secret: 'secret', |
| 26 httpClient: client); | 26 httpClient: client); |
| 27 }); | 27 }); |
| 28 | 28 |
| 29 group('.getAuthorizationUrl', () { | 29 group('.getAuthorizationUrl', () { |
| 30 test('builds the correct URL', () { | 30 test('builds the correct URL', () { |
| 31 expect(grant.getAuthorizationUrl(redirectUrl).toString(), | 31 expect(grant.getAuthorizationUrl(redirectUrl).toString(), |
| 32 equals('https://example.com/authorization' | 32 equals('https://example.com/authorization' |
| 33 '?response_type=code' | 33 '?response_type=code' |
| 34 '&client_id=identifier' | 34 '&client_id=identifier' |
| 35 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | 35 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 53 equals('https://example.com/authorization' | 53 equals('https://example.com/authorization' |
| 54 '?response_type=code' | 54 '?response_type=code' |
| 55 '&client_id=identifier' | 55 '&client_id=identifier' |
| 56 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect' | 56 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect' |
| 57 '&state=state')); | 57 '&state=state')); |
| 58 }); | 58 }); |
| 59 | 59 |
| 60 test('merges with existing query parameters', () { | 60 test('merges with existing query parameters', () { |
| 61 grant = new oauth2.AuthorizationCodeGrant( | 61 grant = new oauth2.AuthorizationCodeGrant( |
| 62 'identifier', | 62 'identifier', |
| 63 'secret', | |
| 64 Uri.parse('https://example.com/authorization?query=value'), | 63 Uri.parse('https://example.com/authorization?query=value'), |
| 65 Uri.parse('https://example.com/token'), | 64 Uri.parse('https://example.com/token'), |
| 65 secret: 'secret', |
| 66 httpClient: client); | 66 httpClient: client); |
| 67 | 67 |
| 68 var authorizationUrl = grant.getAuthorizationUrl(redirectUrl); | 68 var authorizationUrl = grant.getAuthorizationUrl(redirectUrl); |
| 69 expect(authorizationUrl.toString(), | 69 expect(authorizationUrl.toString(), |
| 70 equals('https://example.com/authorization' | 70 equals('https://example.com/authorization' |
| 71 '?query=value' | 71 '?query=value' |
| 72 '&response_type=code' | 72 '&response_type=code' |
| 73 '&client_id=identifier' | 73 '&client_id=identifier' |
| 74 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); | 74 '&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect')); |
| 75 }); | 75 }); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 }); | 120 }); |
| 121 | 121 |
| 122 test('sends an authorization code request', () { | 122 test('sends an authorization code request', () { |
| 123 grant.getAuthorizationUrl(redirectUrl); | 123 grant.getAuthorizationUrl(redirectUrl); |
| 124 client.expectRequest((request) { | 124 client.expectRequest((request) { |
| 125 expect(request.method, equals('POST')); | 125 expect(request.method, equals('POST')); |
| 126 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 126 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 127 expect(request.bodyFields, equals({ | 127 expect(request.bodyFields, equals({ |
| 128 'grant_type': 'authorization_code', | 128 'grant_type': 'authorization_code', |
| 129 'code': 'auth code', | 129 'code': 'auth code', |
| 130 'redirect_uri': redirectUrl.toString(), | 130 'redirect_uri': redirectUrl.toString() |
| 131 'client_id': 'identifier', | |
| 132 'client_secret': 'secret' | |
| 133 })); | 131 })); |
| 132 expect(request.headers, containsPair( |
| 133 "Authorization", |
| 134 "Basic aWRlbnRpZmllcjpzZWNyZXQ=")); |
| 134 | 135 |
| 135 return new Future.value(new http.Response(JSON.encode({ | 136 return new Future.value(new http.Response(JSON.encode({ |
| 136 'access_token': 'access token', | 137 'access_token': 'access token', |
| 137 'token_type': 'bearer', | 138 'token_type': 'bearer', |
| 138 }), 200, headers: {'content-type': 'application/json'})); | 139 }), 200, headers: {'content-type': 'application/json'})); |
| 139 }); | 140 }); |
| 140 | 141 |
| 141 expect(grant.handleAuthorizationResponse({'code': 'auth code'}) | 142 expect(grant.handleAuthorizationResponse({'code': 'auth code'}) |
| 142 .then((client) => client.credentials.accessToken), | 143 .then((client) => client.credentials.accessToken), |
| 143 completion(equals('access token'))); | 144 completion(equals('access token'))); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 156 }); | 157 }); |
| 157 | 158 |
| 158 test('sends an authorization code request', () { | 159 test('sends an authorization code request', () { |
| 159 grant.getAuthorizationUrl(redirectUrl); | 160 grant.getAuthorizationUrl(redirectUrl); |
| 160 client.expectRequest((request) { | 161 client.expectRequest((request) { |
| 161 expect(request.method, equals('POST')); | 162 expect(request.method, equals('POST')); |
| 162 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); | 163 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 163 expect(request.bodyFields, equals({ | 164 expect(request.bodyFields, equals({ |
| 164 'grant_type': 'authorization_code', | 165 'grant_type': 'authorization_code', |
| 165 'code': 'auth code', | 166 'code': 'auth code', |
| 167 'redirect_uri': redirectUrl.toString() |
| 168 })); |
| 169 expect(request.headers, containsPair( |
| 170 "Authorization", |
| 171 "Basic aWRlbnRpZmllcjpzZWNyZXQ=")); |
| 172 |
| 173 return new Future.value(new http.Response(JSON.encode({ |
| 174 'access_token': 'access token', |
| 175 'token_type': 'bearer', |
| 176 }), 200, headers: {'content-type': 'application/json'})); |
| 177 }); |
| 178 |
| 179 expect(grant.handleAuthorizationCode('auth code'), |
| 180 completion(predicate((client) { |
| 181 expect(client.credentials.accessToken, equals('access token')); |
| 182 return true; |
| 183 }))); |
| 184 }); |
| 185 }); |
| 186 |
| 187 group("with basicAuth: false", () { |
| 188 setUp(() { |
| 189 client = new ExpectClient(); |
| 190 grant = new oauth2.AuthorizationCodeGrant( |
| 191 'identifier', |
| 192 Uri.parse('https://example.com/authorization'), |
| 193 Uri.parse('https://example.com/token'), |
| 194 secret: 'secret', |
| 195 basicAuth: false, |
| 196 httpClient: client); |
| 197 }); |
| 198 |
| 199 test('.handleAuthorizationResponse sends an authorization code request', |
| 200 () { |
| 201 grant.getAuthorizationUrl(redirectUrl); |
| 202 client.expectRequest((request) { |
| 203 expect(request.method, equals('POST')); |
| 204 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 205 expect(request.bodyFields, equals({ |
| 206 'grant_type': 'authorization_code', |
| 207 'code': 'auth code', |
| 166 'redirect_uri': redirectUrl.toString(), | 208 'redirect_uri': redirectUrl.toString(), |
| 167 'client_id': 'identifier', | 209 'client_id': 'identifier', |
| 168 'client_secret': 'secret' | 210 'client_secret': 'secret' |
| 211 })); |
| 212 |
| 213 return new Future.value(new http.Response(JSON.encode({ |
| 214 'access_token': 'access token', |
| 215 'token_type': 'bearer', |
| 216 }), 200, headers: {'content-type': 'application/json'})); |
| 217 }); |
| 218 |
| 219 expect(grant.handleAuthorizationResponse({'code': 'auth code'}) |
| 220 .then((client) => client.credentials.accessToken), |
| 221 completion(equals('access token'))); |
| 222 }); |
| 223 |
| 224 test('.handleAuthorizationCode sends an authorization code request', () { |
| 225 grant.getAuthorizationUrl(redirectUrl); |
| 226 client.expectRequest((request) { |
| 227 expect(request.method, equals('POST')); |
| 228 expect(request.url.toString(), equals(grant.tokenEndpoint.toString())); |
| 229 expect(request.bodyFields, equals({ |
| 230 'grant_type': 'authorization_code', |
| 231 'code': 'auth code', |
| 232 'redirect_uri': redirectUrl.toString(), |
| 233 'client_id': 'identifier', |
| 234 'client_secret': 'secret' |
| 169 })); | 235 })); |
| 170 | 236 |
| 171 return new Future.value(new http.Response(JSON.encode({ | 237 return new Future.value(new http.Response(JSON.encode({ |
| 172 'access_token': 'access token', | 238 'access_token': 'access token', |
| 173 'token_type': 'bearer', | 239 'token_type': 'bearer', |
| 174 }), 200, headers: {'content-type': 'application/json'})); | 240 }), 200, headers: {'content-type': 'application/json'})); |
| 175 }); | 241 }); |
| 176 | 242 |
| 177 expect(grant.handleAuthorizationCode('auth code'), | 243 expect(grant.handleAuthorizationCode('auth code'), |
| 178 completion(predicate((client) { | 244 completion(predicate((client) { |
| 179 expect(client.credentials.accessToken, equals('access token')); | 245 expect(client.credentials.accessToken, equals('access token')); |
| 180 return true; | 246 return true; |
| 181 }))); | 247 }))); |
| 182 }); | 248 }); |
| 183 }); | 249 }); |
| 184 } | 250 } |
| OLD | NEW |