| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library client_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:convert'; | |
| 9 | |
| 10 import 'package:http/http.dart' as http; | |
| 11 import 'package:oauth2/oauth2.dart' as oauth2; | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 import 'utils.dart'; | |
| 15 | |
| 16 final Uri requestUri = Uri.parse("http://example.com/resource"); | |
| 17 | |
| 18 final Uri tokenEndpoint = Uri.parse('http://example.com/token'); | |
| 19 | |
| 20 ExpectClient httpClient; | |
| 21 | |
| 22 void createHttpClient() { | |
| 23 httpClient = new ExpectClient(); | |
| 24 } | |
| 25 | |
| 26 void main() { | |
| 27 group('with expired credentials', () { | |
| 28 setUp(createHttpClient); | |
| 29 | |
| 30 test("that can't be refreshed throws an ExpirationException on send", () { | |
| 31 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | |
| 32 var credentials = new oauth2.Credentials( | |
| 33 'access token', null, null, [], expiration); | |
| 34 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 35 httpClient: httpClient); | |
| 36 | |
| 37 expect(client.get(requestUri), | |
| 38 throwsA(new isInstanceOf<oauth2.ExpirationException>())); | |
| 39 }); | |
| 40 | |
| 41 test("that can be refreshed refreshes the credentials and sends the " | |
| 42 "request", () { | |
| 43 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | |
| 44 var credentials = new oauth2.Credentials( | |
| 45 'access token', 'refresh token', tokenEndpoint, [], expiration); | |
| 46 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 47 httpClient: httpClient); | |
| 48 | |
| 49 httpClient.expectRequest((request) { | |
| 50 expect(request.method, equals('POST')); | |
| 51 expect(request.url.toString(), equals(tokenEndpoint.toString())); | |
| 52 return new Future.value(new http.Response(JSON.encode({ | |
| 53 'access_token': 'new access token', | |
| 54 'token_type': 'bearer' | |
| 55 }), 200, headers: {'content-type': 'application/json'})); | |
| 56 }); | |
| 57 | |
| 58 httpClient.expectRequest((request) { | |
| 59 expect(request.method, equals('GET')); | |
| 60 expect(request.url.toString(), equals(requestUri.toString())); | |
| 61 expect(request.headers['authorization'], | |
| 62 equals('Bearer new access token')); | |
| 63 | |
| 64 return new Future.value(new http.Response('good job', 200)); | |
| 65 }); | |
| 66 | |
| 67 expect(client.read(requestUri).then((_) { | |
| 68 expect(client.credentials.accessToken, equals('new access token')); | |
| 69 }), completes); | |
| 70 }); | |
| 71 }); | |
| 72 | |
| 73 group('with valid credentials', () { | |
| 74 setUp(createHttpClient); | |
| 75 | |
| 76 test("sends a request with bearer authorization", () { | |
| 77 var credentials = new oauth2.Credentials('access token'); | |
| 78 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 79 httpClient: httpClient); | |
| 80 | |
| 81 httpClient.expectRequest((request) { | |
| 82 expect(request.method, equals('GET')); | |
| 83 expect(request.url.toString(), equals(requestUri.toString())); | |
| 84 expect(request.headers['authorization'], equals('Bearer access token')); | |
| 85 | |
| 86 return new Future.value(new http.Response('good job', 200)); | |
| 87 }); | |
| 88 | |
| 89 expect(client.read(requestUri), completion(equals('good job'))); | |
| 90 }); | |
| 91 | |
| 92 test("can manually refresh the credentials", () { | |
| 93 var credentials = new oauth2.Credentials( | |
| 94 'access token', 'refresh token', tokenEndpoint); | |
| 95 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 96 httpClient: httpClient); | |
| 97 | |
| 98 httpClient.expectRequest((request) { | |
| 99 expect(request.method, equals('POST')); | |
| 100 expect(request.url.toString(), equals(tokenEndpoint.toString())); | |
| 101 return new Future.value(new http.Response(JSON.encode({ | |
| 102 'access_token': 'new access token', | |
| 103 'token_type': 'bearer' | |
| 104 }), 200, headers: {'content-type': 'application/json'})); | |
| 105 }); | |
| 106 | |
| 107 expect(client.refreshCredentials().then((_) { | |
| 108 expect(client.credentials.accessToken, equals('new access token')); | |
| 109 }), completes); | |
| 110 }); | |
| 111 | |
| 112 test("without a refresh token can't manually refresh the credentials", () { | |
| 113 var credentials = new oauth2.Credentials('access token'); | |
| 114 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 115 httpClient: httpClient); | |
| 116 | |
| 117 expect(client.refreshCredentials(), throwsA(isStateError)); | |
| 118 }); | |
| 119 }); | |
| 120 | |
| 121 group('with invalid credentials', () { | |
| 122 setUp(createHttpClient); | |
| 123 | |
| 124 test('throws an AuthorizationException for a 401 response', () { | |
| 125 var credentials = new oauth2.Credentials('access token'); | |
| 126 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 127 httpClient: httpClient); | |
| 128 | |
| 129 httpClient.expectRequest((request) { | |
| 130 expect(request.method, equals('GET')); | |
| 131 expect(request.url.toString(), equals(requestUri.toString())); | |
| 132 expect(request.headers['authorization'], equals('Bearer access token')); | |
| 133 | |
| 134 var authenticate = 'Bearer error="invalid_token", error_description=' | |
| 135 '"Something is terribly wrong."'; | |
| 136 return new Future.value(new http.Response('bad job', 401, | |
| 137 headers: {'www-authenticate': authenticate})); | |
| 138 }); | |
| 139 | |
| 140 expect(client.read(requestUri), | |
| 141 throwsA(new isInstanceOf<oauth2.AuthorizationException>())); | |
| 142 }); | |
| 143 | |
| 144 test('passes through a 401 response without www-authenticate', () { | |
| 145 var credentials = new oauth2.Credentials('access token'); | |
| 146 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 147 httpClient: httpClient); | |
| 148 | |
| 149 httpClient.expectRequest((request) { | |
| 150 expect(request.method, equals('GET')); | |
| 151 expect(request.url.toString(), equals(requestUri.toString())); | |
| 152 expect(request.headers['authorization'], | |
| 153 equals('Bearer access token')); | |
| 154 | |
| 155 return new Future.value(new http.Response('bad job', 401)); | |
| 156 }); | |
| 157 | |
| 158 expect( | |
| 159 client.get(requestUri).then((response) => response.statusCode), | |
| 160 completion(equals(401))); | |
| 161 }); | |
| 162 | |
| 163 test('passes through a 401 response with invalid www-authenticate', () { | |
| 164 var credentials = new oauth2.Credentials('access token'); | |
| 165 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 166 httpClient: httpClient); | |
| 167 | |
| 168 httpClient.expectRequest((request) { | |
| 169 expect(request.method, equals('GET')); | |
| 170 expect(request.url.toString(), equals(requestUri.toString())); | |
| 171 expect(request.headers['authorization'], | |
| 172 equals('Bearer access token')); | |
| 173 | |
| 174 var authenticate = 'Bearer error="invalid_token", error_description=' | |
| 175 '"Something is terribly wrong.", '; | |
| 176 return new Future.value(new http.Response('bad job', 401, | |
| 177 headers: {'www-authenticate': authenticate})); | |
| 178 }); | |
| 179 | |
| 180 expect( | |
| 181 client.get(requestUri).then((response) => response.statusCode), | |
| 182 completion(equals(401))); | |
| 183 }); | |
| 184 | |
| 185 test('passes through a 401 response with non-bearer www-authenticate', () { | |
| 186 var credentials = new oauth2.Credentials('access token'); | |
| 187 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 188 httpClient: httpClient); | |
| 189 | |
| 190 httpClient.expectRequest((request) { | |
| 191 expect(request.method, equals('GET')); | |
| 192 expect(request.url.toString(), equals(requestUri.toString())); | |
| 193 expect(request.headers['authorization'], | |
| 194 equals('Bearer access token')); | |
| 195 | |
| 196 return new Future.value(new http.Response('bad job', 401, | |
| 197 headers: {'www-authenticate': 'Digest'})); | |
| 198 }); | |
| 199 | |
| 200 expect( | |
| 201 client.get(requestUri).then((response) => response.statusCode), | |
| 202 completion(equals(401))); | |
| 203 }); | |
| 204 | |
| 205 test('passes through a 401 response with non-OAuth2 www-authenticate', () { | |
| 206 var credentials = new oauth2.Credentials('access token'); | |
| 207 var client = new oauth2.Client('identifier', 'secret', credentials, | |
| 208 httpClient: httpClient); | |
| 209 | |
| 210 httpClient.expectRequest((request) { | |
| 211 expect(request.method, equals('GET')); | |
| 212 expect(request.url.toString(), equals(requestUri.toString())); | |
| 213 expect(request.headers['authorization'], | |
| 214 equals('Bearer access token')); | |
| 215 | |
| 216 return new Future.value(new http.Response('bad job', 401, | |
| 217 headers: {'www-authenticate': 'Bearer'})); | |
| 218 }); | |
| 219 | |
| 220 expect( | |
| 221 client.get(requestUri).then((response) => response.statusCode), | |
| 222 completion(equals(401))); | |
| 223 }); | |
| 224 }); | |
| 225 } | |
| OLD | NEW |