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 client_test; | 5 library client_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 21 matching lines...) Expand all Loading... |
32 var credentials = new oauth2.Credentials( | 32 var credentials = new oauth2.Credentials( |
33 'access token', null, null, [], expiration); | 33 'access token', null, null, [], expiration); |
34 var client = new oauth2.Client('identifier', 'secret', credentials, | 34 var client = new oauth2.Client('identifier', 'secret', credentials, |
35 httpClient: httpClient); | 35 httpClient: httpClient); |
36 | 36 |
37 expect(client.get(requestUri), | 37 expect(client.get(requestUri), |
38 throwsA(new isInstanceOf<oauth2.ExpirationException>())); | 38 throwsA(new isInstanceOf<oauth2.ExpirationException>())); |
39 }); | 39 }); |
40 | 40 |
41 test("that can be refreshed refreshes the credentials and sends the " | 41 test("that can be refreshed refreshes the credentials and sends the " |
42 "request", () { | 42 "request", () async { |
43 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); | 43 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); |
44 var credentials = new oauth2.Credentials( | 44 var credentials = new oauth2.Credentials( |
45 'access token', 'refresh token', tokenEndpoint, [], expiration); | 45 'access token', 'refresh token', tokenEndpoint, [], expiration); |
46 var client = new oauth2.Client('identifier', 'secret', credentials, | 46 var client = new oauth2.Client('identifier', 'secret', credentials, |
47 httpClient: httpClient); | 47 httpClient: httpClient); |
48 | 48 |
49 httpClient.expectRequest((request) { | 49 httpClient.expectRequest((request) { |
50 expect(request.method, equals('POST')); | 50 expect(request.method, equals('POST')); |
51 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 51 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
52 return new Future.value(new http.Response(JSON.encode({ | 52 return new Future.value(new http.Response(JSON.encode({ |
53 'access_token': 'new access token', | 53 'access_token': 'new access token', |
54 'token_type': 'bearer' | 54 'token_type': 'bearer' |
55 }), 200, headers: {'content-type': 'application/json'})); | 55 }), 200, headers: {'content-type': 'application/json'})); |
56 }); | 56 }); |
57 | 57 |
58 httpClient.expectRequest((request) { | 58 httpClient.expectRequest((request) { |
59 expect(request.method, equals('GET')); | 59 expect(request.method, equals('GET')); |
60 expect(request.url.toString(), equals(requestUri.toString())); | 60 expect(request.url.toString(), equals(requestUri.toString())); |
61 expect(request.headers['authorization'], | 61 expect(request.headers['authorization'], |
62 equals('Bearer new access token')); | 62 equals('Bearer new access token')); |
63 | 63 |
64 return new Future.value(new http.Response('good job', 200)); | 64 return new Future.value(new http.Response('good job', 200)); |
65 }); | 65 }); |
66 | 66 |
67 expect(client.read(requestUri).then((_) { | 67 await client.read(requestUri); |
68 expect(client.credentials.accessToken, equals('new access token')); | 68 expect(client.credentials.accessToken, equals('new access token')); |
69 }), completes); | |
70 }); | 69 }); |
71 }); | 70 }); |
72 | 71 |
73 group('with valid credentials', () { | 72 group('with valid credentials', () { |
74 setUp(createHttpClient); | 73 setUp(createHttpClient); |
75 | 74 |
76 test("sends a request with bearer authorization", () { | 75 test("sends a request with bearer authorization", () { |
77 var credentials = new oauth2.Credentials('access token'); | 76 var credentials = new oauth2.Credentials('access token'); |
78 var client = new oauth2.Client('identifier', 'secret', credentials, | 77 var client = new oauth2.Client('identifier', 'secret', credentials, |
79 httpClient: httpClient); | 78 httpClient: httpClient); |
80 | 79 |
81 httpClient.expectRequest((request) { | 80 httpClient.expectRequest((request) { |
82 expect(request.method, equals('GET')); | 81 expect(request.method, equals('GET')); |
83 expect(request.url.toString(), equals(requestUri.toString())); | 82 expect(request.url.toString(), equals(requestUri.toString())); |
84 expect(request.headers['authorization'], equals('Bearer access token')); | 83 expect(request.headers['authorization'], equals('Bearer access token')); |
85 | 84 |
86 return new Future.value(new http.Response('good job', 200)); | 85 return new Future.value(new http.Response('good job', 200)); |
87 }); | 86 }); |
88 | 87 |
89 expect(client.read(requestUri), completion(equals('good job'))); | 88 expect(client.read(requestUri), completion(equals('good job'))); |
90 }); | 89 }); |
91 | 90 |
92 test("can manually refresh the credentials", () { | 91 test("can manually refresh the credentials", () async { |
93 var credentials = new oauth2.Credentials( | 92 var credentials = new oauth2.Credentials( |
94 'access token', 'refresh token', tokenEndpoint); | 93 'access token', 'refresh token', tokenEndpoint); |
95 var client = new oauth2.Client('identifier', 'secret', credentials, | 94 var client = new oauth2.Client('identifier', 'secret', credentials, |
96 httpClient: httpClient); | 95 httpClient: httpClient); |
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 return new Future.value(new http.Response(JSON.encode({ | 100 return new Future.value(new http.Response(JSON.encode({ |
102 'access_token': 'new access token', | 101 'access_token': 'new access token', |
103 'token_type': 'bearer' | 102 'token_type': 'bearer' |
104 }), 200, headers: {'content-type': 'application/json'})); | 103 }), 200, headers: {'content-type': 'application/json'})); |
105 }); | 104 }); |
106 | 105 |
107 expect(client.refreshCredentials().then((_) { | 106 await client.refreshCredentials(); |
108 expect(client.credentials.accessToken, equals('new access token')); | 107 expect(client.credentials.accessToken, equals('new access token')); |
109 }), completes); | |
110 }); | 108 }); |
111 | 109 |
112 test("without a refresh token can't manually refresh the credentials", () { | 110 test("without a refresh token can't manually refresh the credentials", () { |
113 var credentials = new oauth2.Credentials('access token'); | 111 var credentials = new oauth2.Credentials('access token'); |
114 var client = new oauth2.Client('identifier', 'secret', credentials, | 112 var client = new oauth2.Client('identifier', 'secret', credentials, |
115 httpClient: httpClient); | 113 httpClient: httpClient); |
116 | 114 |
117 expect(client.refreshCredentials(), throwsA(isStateError)); | 115 expect(client.refreshCredentials(), throwsA(isStateError)); |
118 }); | 116 }); |
119 }); | 117 }); |
(...skipping 14 matching lines...) Expand all Loading... |
134 var authenticate = 'Bearer error="invalid_token", error_description=' | 132 var authenticate = 'Bearer error="invalid_token", error_description=' |
135 '"Something is terribly wrong."'; | 133 '"Something is terribly wrong."'; |
136 return new Future.value(new http.Response('bad job', 401, | 134 return new Future.value(new http.Response('bad job', 401, |
137 headers: {'www-authenticate': authenticate})); | 135 headers: {'www-authenticate': authenticate})); |
138 }); | 136 }); |
139 | 137 |
140 expect(client.read(requestUri), | 138 expect(client.read(requestUri), |
141 throwsA(new isInstanceOf<oauth2.AuthorizationException>())); | 139 throwsA(new isInstanceOf<oauth2.AuthorizationException>())); |
142 }); | 140 }); |
143 | 141 |
144 test('passes through a 401 response without www-authenticate', () { | 142 test('passes through a 401 response without www-authenticate', () async { |
145 var credentials = new oauth2.Credentials('access token'); | 143 var credentials = new oauth2.Credentials('access token'); |
146 var client = new oauth2.Client('identifier', 'secret', credentials, | 144 var client = new oauth2.Client('identifier', 'secret', credentials, |
147 httpClient: httpClient); | 145 httpClient: httpClient); |
148 | 146 |
149 httpClient.expectRequest((request) { | 147 httpClient.expectRequest((request) { |
150 expect(request.method, equals('GET')); | 148 expect(request.method, equals('GET')); |
151 expect(request.url.toString(), equals(requestUri.toString())); | 149 expect(request.url.toString(), equals(requestUri.toString())); |
152 expect(request.headers['authorization'], | 150 expect(request.headers['authorization'], |
153 equals('Bearer access token')); | 151 equals('Bearer access token')); |
154 | 152 |
155 return new Future.value(new http.Response('bad job', 401)); | 153 return new Future.value(new http.Response('bad job', 401)); |
156 }); | 154 }); |
157 | 155 |
158 expect( | 156 expect((await client.get(requestUri)).statusCode, equals(401)); |
159 client.get(requestUri).then((response) => response.statusCode), | |
160 completion(equals(401))); | |
161 }); | 157 }); |
162 | 158 |
163 test('passes through a 401 response with invalid www-authenticate', () { | 159 test('passes through a 401 response with invalid www-authenticate', |
| 160 () async { |
164 var credentials = new oauth2.Credentials('access token'); | 161 var credentials = new oauth2.Credentials('access token'); |
165 var client = new oauth2.Client('identifier', 'secret', credentials, | 162 var client = new oauth2.Client('identifier', 'secret', credentials, |
166 httpClient: httpClient); | 163 httpClient: httpClient); |
167 | 164 |
168 httpClient.expectRequest((request) { | 165 httpClient.expectRequest((request) { |
169 expect(request.method, equals('GET')); | 166 expect(request.method, equals('GET')); |
170 expect(request.url.toString(), equals(requestUri.toString())); | 167 expect(request.url.toString(), equals(requestUri.toString())); |
171 expect(request.headers['authorization'], | 168 expect(request.headers['authorization'], |
172 equals('Bearer access token')); | 169 equals('Bearer access token')); |
173 | 170 |
174 var authenticate = 'Bearer error="invalid_token", error_description=' | 171 var authenticate = 'Bearer error="invalid_token", error_description=' |
175 '"Something is terribly wrong.", '; | 172 '"Something is terribly wrong.", '; |
176 return new Future.value(new http.Response('bad job', 401, | 173 return new Future.value(new http.Response('bad job', 401, |
177 headers: {'www-authenticate': authenticate})); | 174 headers: {'www-authenticate': authenticate})); |
178 }); | 175 }); |
179 | 176 |
180 expect( | 177 expect((await client.get(requestUri)).statusCode, equals(401)); |
181 client.get(requestUri).then((response) => response.statusCode), | |
182 completion(equals(401))); | |
183 }); | 178 }); |
184 | 179 |
185 test('passes through a 401 response with non-bearer www-authenticate', () { | 180 test('passes through a 401 response with non-bearer www-authenticate', |
| 181 () async { |
186 var credentials = new oauth2.Credentials('access token'); | 182 var credentials = new oauth2.Credentials('access token'); |
187 var client = new oauth2.Client('identifier', 'secret', credentials, | 183 var client = new oauth2.Client('identifier', 'secret', credentials, |
188 httpClient: httpClient); | 184 httpClient: httpClient); |
189 | 185 |
190 httpClient.expectRequest((request) { | 186 httpClient.expectRequest((request) { |
191 expect(request.method, equals('GET')); | 187 expect(request.method, equals('GET')); |
192 expect(request.url.toString(), equals(requestUri.toString())); | 188 expect(request.url.toString(), equals(requestUri.toString())); |
193 expect(request.headers['authorization'], | 189 expect(request.headers['authorization'], |
194 equals('Bearer access token')); | 190 equals('Bearer access token')); |
195 | 191 |
196 return new Future.value(new http.Response('bad job', 401, | 192 return new Future.value(new http.Response('bad job', 401, |
197 headers: {'www-authenticate': 'Digest'})); | 193 headers: {'www-authenticate': 'Digest'})); |
198 }); | 194 }); |
199 | 195 |
200 expect( | 196 expect((await client.get(requestUri)).statusCode, equals(401)); |
201 client.get(requestUri).then((response) => response.statusCode), | |
202 completion(equals(401))); | |
203 }); | 197 }); |
204 | 198 |
205 test('passes through a 401 response with non-OAuth2 www-authenticate', () { | 199 test('passes through a 401 response with non-OAuth2 www-authenticate', |
| 200 () async { |
206 var credentials = new oauth2.Credentials('access token'); | 201 var credentials = new oauth2.Credentials('access token'); |
207 var client = new oauth2.Client('identifier', 'secret', credentials, | 202 var client = new oauth2.Client('identifier', 'secret', credentials, |
208 httpClient: httpClient); | 203 httpClient: httpClient); |
209 | 204 |
210 httpClient.expectRequest((request) { | 205 httpClient.expectRequest((request) { |
211 expect(request.method, equals('GET')); | 206 expect(request.method, equals('GET')); |
212 expect(request.url.toString(), equals(requestUri.toString())); | 207 expect(request.url.toString(), equals(requestUri.toString())); |
213 expect(request.headers['authorization'], | 208 expect(request.headers['authorization'], |
214 equals('Bearer access token')); | 209 equals('Bearer access token')); |
215 | 210 |
216 return new Future.value(new http.Response('bad job', 401, | 211 return new Future.value(new http.Response('bad job', 401, |
217 headers: {'www-authenticate': 'Bearer'})); | 212 headers: {'www-authenticate': 'Bearer'})); |
218 }); | 213 }); |
219 | 214 |
220 expect( | 215 expect((await client.get(requestUri)).statusCode, equals(401)); |
221 client.get(requestUri).then((response) => response.statusCode), | |
222 completion(equals(401))); | |
223 }); | 216 }); |
224 }); | 217 }); |
225 } | 218 } |
OLD | NEW |