Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: pkg/oauth2/test/client_test.dart

Issue 14070010: Refactor Future constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added co19 issue number. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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:io'; 8 import 'dart:io';
9 import 'dart:json' as JSON; 9 import 'dart:json' as JSON;
10 import 'dart:uri'; 10 import 'dart:uri';
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 "request", () { 50 "request", () {
51 var expiration = new DateTime.now().subtract(new Duration(hours: 1)); 51 var expiration = new DateTime.now().subtract(new Duration(hours: 1));
52 var credentials = new oauth2.Credentials( 52 var credentials = new oauth2.Credentials(
53 'access token', 'refresh token', tokenEndpoint, [], expiration); 53 'access token', 'refresh token', tokenEndpoint, [], expiration);
54 var client = new oauth2.Client('identifier', 'secret', credentials, 54 var client = new oauth2.Client('identifier', 'secret', credentials,
55 httpClient: httpClient); 55 httpClient: httpClient);
56 56
57 httpClient.expectRequest((request) { 57 httpClient.expectRequest((request) {
58 expect(request.method, equals('POST')); 58 expect(request.method, equals('POST'));
59 expect(request.url.toString(), equals(tokenEndpoint.toString())); 59 expect(request.url.toString(), equals(tokenEndpoint.toString()));
60 return new Future.immediate(new http.Response(JSON.stringify({ 60 return new Future.value(new http.Response(JSON.stringify({
61 'access_token': 'new access token', 61 'access_token': 'new access token',
62 'token_type': 'bearer' 62 'token_type': 'bearer'
63 }), 200, headers: {'content-type': 'application/json'})); 63 }), 200, headers: {'content-type': 'application/json'}));
64 }); 64 });
65 65
66 httpClient.expectRequest((request) { 66 httpClient.expectRequest((request) {
67 expect(request.method, equals('GET')); 67 expect(request.method, equals('GET'));
68 expect(request.url.toString(), equals(requestUri.toString())); 68 expect(request.url.toString(), equals(requestUri.toString()));
69 expect(request.headers['authorization'], 69 expect(request.headers['authorization'],
70 equals('Bearer new access token')); 70 equals('Bearer new access token'));
71 71
72 return new Future.immediate(new http.Response('good job', 200)); 72 return new Future.value(new http.Response('good job', 200));
73 }); 73 });
74 74
75 expect(client.read(requestUri).then((_) { 75 expect(client.read(requestUri).then((_) {
76 expect(client.credentials.accessToken, equals('new access token')); 76 expect(client.credentials.accessToken, equals('new access token'));
77 }), completes); 77 }), completes);
78 }); 78 });
79 }); 79 });
80 80
81 group('with valid credentials', () { 81 group('with valid credentials', () {
82 setUp(createHttpClient); 82 setUp(createHttpClient);
83 83
84 test("sends a request with bearer authorization", () { 84 test("sends a request with bearer authorization", () {
85 var credentials = new oauth2.Credentials('access token'); 85 var credentials = new oauth2.Credentials('access token');
86 var client = new oauth2.Client('identifier', 'secret', credentials, 86 var client = new oauth2.Client('identifier', 'secret', credentials,
87 httpClient: httpClient); 87 httpClient: httpClient);
88 88
89 httpClient.expectRequest((request) { 89 httpClient.expectRequest((request) {
90 expect(request.method, equals('GET')); 90 expect(request.method, equals('GET'));
91 expect(request.url.toString(), equals(requestUri.toString())); 91 expect(request.url.toString(), equals(requestUri.toString()));
92 expect(request.headers['authorization'], 92 expect(request.headers['authorization'],
93 equals('Bearer access token')); 93 equals('Bearer access token'));
94 94
95 return new Future.immediate(new http.Response('good job', 200)); 95 return new Future.value(new http.Response('good job', 200));
96 }); 96 });
97 97
98 expect(client.read(requestUri), completion(equals('good job'))); 98 expect(client.read(requestUri), completion(equals('good job')));
99 }); 99 });
100 100
101 test("can manually refresh the credentials", () { 101 test("can manually refresh the credentials", () {
102 var credentials = new oauth2.Credentials( 102 var credentials = new oauth2.Credentials(
103 'access token', 'refresh token', tokenEndpoint); 103 'access token', 'refresh token', tokenEndpoint);
104 var client = new oauth2.Client('identifier', 'secret', credentials, 104 var client = new oauth2.Client('identifier', 'secret', credentials,
105 httpClient: httpClient); 105 httpClient: httpClient);
106 106
107 httpClient.expectRequest((request) { 107 httpClient.expectRequest((request) {
108 expect(request.method, equals('POST')); 108 expect(request.method, equals('POST'));
109 expect(request.url.toString(), equals(tokenEndpoint.toString())); 109 expect(request.url.toString(), equals(tokenEndpoint.toString()));
110 return new Future.immediate(new http.Response(JSON.stringify({ 110 return new Future.value(new http.Response(JSON.stringify({
111 'access_token': 'new access token', 111 'access_token': 'new access token',
112 'token_type': 'bearer' 112 'token_type': 'bearer'
113 }), 200, headers: {'content-type': 'application/json'})); 113 }), 200, headers: {'content-type': 'application/json'}));
114 }); 114 });
115 115
116 expect(client.refreshCredentials().then((_) { 116 expect(client.refreshCredentials().then((_) {
117 expect(client.credentials.accessToken, equals('new access token')); 117 expect(client.credentials.accessToken, equals('new access token'));
118 }), completes); 118 }), completes);
119 }); 119 });
120 120
(...skipping 16 matching lines...) Expand all
137 httpClient: httpClient); 137 httpClient: httpClient);
138 138
139 httpClient.expectRequest((request) { 139 httpClient.expectRequest((request) {
140 expect(request.method, equals('GET')); 140 expect(request.method, equals('GET'));
141 expect(request.url.toString(), equals(requestUri.toString())); 141 expect(request.url.toString(), equals(requestUri.toString()));
142 expect(request.headers['authorization'], 142 expect(request.headers['authorization'],
143 equals('Bearer access token')); 143 equals('Bearer access token'));
144 144
145 var authenticate = 'Bearer error="invalid_token", error_description=' 145 var authenticate = 'Bearer error="invalid_token", error_description='
146 '"Something is terribly wrong."'; 146 '"Something is terribly wrong."';
147 return new Future.immediate(new http.Response('bad job', 401, 147 return new Future.value(new http.Response('bad job', 401,
148 headers: {'www-authenticate': authenticate})); 148 headers: {'www-authenticate': authenticate}));
149 }); 149 });
150 150
151 expectFutureThrows(client.read(requestUri), 151 expectFutureThrows(client.read(requestUri),
152 (e) => e is oauth2.AuthorizationException); 152 (e) => e is oauth2.AuthorizationException);
153 }); 153 });
154 154
155 test('passes through a 401 response without www-authenticate', () { 155 test('passes through a 401 response without www-authenticate', () {
156 var credentials = new oauth2.Credentials('access token'); 156 var credentials = new oauth2.Credentials('access token');
157 var client = new oauth2.Client('identifier', 'secret', credentials, 157 var client = new oauth2.Client('identifier', 'secret', credentials,
158 httpClient: httpClient); 158 httpClient: httpClient);
159 159
160 httpClient.expectRequest((request) { 160 httpClient.expectRequest((request) {
161 expect(request.method, equals('GET')); 161 expect(request.method, equals('GET'));
162 expect(request.url.toString(), equals(requestUri.toString())); 162 expect(request.url.toString(), equals(requestUri.toString()));
163 expect(request.headers['authorization'], 163 expect(request.headers['authorization'],
164 equals('Bearer access token')); 164 equals('Bearer access token'));
165 165
166 return new Future.immediate(new http.Response('bad job', 401)); 166 return new Future.value(new http.Response('bad job', 401));
167 }); 167 });
168 168
169 expect( 169 expect(
170 client.get(requestUri).then((response) => response.statusCode), 170 client.get(requestUri).then((response) => response.statusCode),
171 completion(equals(401))); 171 completion(equals(401)));
172 }); 172 });
173 173
174 test('passes through a 401 response with invalid www-authenticate', () { 174 test('passes through a 401 response with invalid www-authenticate', () {
175 var credentials = new oauth2.Credentials('access token'); 175 var credentials = new oauth2.Credentials('access token');
176 var client = new oauth2.Client('identifier', 'secret', credentials, 176 var client = new oauth2.Client('identifier', 'secret', credentials,
177 httpClient: httpClient); 177 httpClient: httpClient);
178 178
179 httpClient.expectRequest((request) { 179 httpClient.expectRequest((request) {
180 expect(request.method, equals('GET')); 180 expect(request.method, equals('GET'));
181 expect(request.url.toString(), equals(requestUri.toString())); 181 expect(request.url.toString(), equals(requestUri.toString()));
182 expect(request.headers['authorization'], 182 expect(request.headers['authorization'],
183 equals('Bearer access token')); 183 equals('Bearer access token'));
184 184
185 var authenticate = 'Bearer error="invalid_token", error_description=' 185 var authenticate = 'Bearer error="invalid_token", error_description='
186 '"Something is terribly wrong.", '; 186 '"Something is terribly wrong.", ';
187 return new Future.immediate(new http.Response('bad job', 401, 187 return new Future.value(new http.Response('bad job', 401,
188 headers: {'www-authenticate': authenticate})); 188 headers: {'www-authenticate': authenticate}));
189 }); 189 });
190 190
191 expect( 191 expect(
192 client.get(requestUri).then((response) => response.statusCode), 192 client.get(requestUri).then((response) => response.statusCode),
193 completion(equals(401))); 193 completion(equals(401)));
194 }); 194 });
195 195
196 test('passes through a 401 response with non-bearer www-authenticate', () { 196 test('passes through a 401 response with non-bearer www-authenticate', () {
197 var credentials = new oauth2.Credentials('access token'); 197 var credentials = new oauth2.Credentials('access token');
198 var client = new oauth2.Client('identifier', 'secret', credentials, 198 var client = new oauth2.Client('identifier', 'secret', credentials,
199 httpClient: httpClient); 199 httpClient: httpClient);
200 200
201 httpClient.expectRequest((request) { 201 httpClient.expectRequest((request) {
202 expect(request.method, equals('GET')); 202 expect(request.method, equals('GET'));
203 expect(request.url.toString(), equals(requestUri.toString())); 203 expect(request.url.toString(), equals(requestUri.toString()));
204 expect(request.headers['authorization'], 204 expect(request.headers['authorization'],
205 equals('Bearer access token')); 205 equals('Bearer access token'));
206 206
207 return new Future.immediate(new http.Response('bad job', 401, 207 return new Future.value(new http.Response('bad job', 401,
208 headers: {'www-authenticate': 'Digest'})); 208 headers: {'www-authenticate': 'Digest'}));
209 }); 209 });
210 210
211 expect( 211 expect(
212 client.get(requestUri).then((response) => response.statusCode), 212 client.get(requestUri).then((response) => response.statusCode),
213 completion(equals(401))); 213 completion(equals(401)));
214 }); 214 });
215 215
216 test('passes through a 401 response with non-OAuth2 www-authenticate', () { 216 test('passes through a 401 response with non-OAuth2 www-authenticate', () {
217 var credentials = new oauth2.Credentials('access token'); 217 var credentials = new oauth2.Credentials('access token');
218 var client = new oauth2.Client('identifier', 'secret', credentials, 218 var client = new oauth2.Client('identifier', 'secret', credentials,
219 httpClient: httpClient); 219 httpClient: httpClient);
220 220
221 httpClient.expectRequest((request) { 221 httpClient.expectRequest((request) {
222 expect(request.method, equals('GET')); 222 expect(request.method, equals('GET'));
223 expect(request.url.toString(), equals(requestUri.toString())); 223 expect(request.url.toString(), equals(requestUri.toString()));
224 expect(request.headers['authorization'], 224 expect(request.headers['authorization'],
225 equals('Bearer access token')); 225 equals('Bearer access token'));
226 226
227 return new Future.immediate(new http.Response('bad job', 401, 227 return new Future.value(new http.Response('bad job', 401,
228 headers: {'www-authenticate': 'Bearer'})); 228 headers: {'www-authenticate': 'Bearer'}));
229 }); 229 });
230 230
231 expect( 231 expect(
232 client.get(requestUri).then((response) => response.statusCode), 232 client.get(requestUri).then((response) => response.statusCode),
233 completion(equals(401))); 233 completion(equals(401)));
234 }); 234 });
235 }); 235 });
236 } 236 }
OLDNEW
« no previous file with comments | « pkg/oauth2/test/authorization_code_grant_test.dart ('k') | pkg/oauth2/test/credentials_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698