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

Side by Side Diff: tests/standalone/io/http_redirect_test.dart

Issue 2771453003: Format all tests. (Closed)
Patch Set: Format files Created 3 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 5
6 import "package:expect/expect.dart"; 6 import "package:expect/expect.dart";
7 import "dart:async"; 7 import "dart:async";
8 import "dart:io"; 8 import "dart:io";
9 9
10 Future<HttpServer> setupServer() { 10 Future<HttpServer> setupServer() {
11 Completer completer = new Completer(); 11 Completer completer = new Completer();
12 HttpServer.bind("127.0.0.1", 0).then((server) { 12 HttpServer.bind("127.0.0.1", 0).then((server) {
13
14 var handlers = new Map<String, Function>(); 13 var handlers = new Map<String, Function>();
15 addRequestHandler(String path, void handler(HttpRequest request, 14 addRequestHandler(
16 HttpResponse response)) { 15 String path, void handler(HttpRequest request, HttpResponse response)) {
17 handlers[path] = handler; 16 handlers[path] = handler;
18 } 17 }
19 18
20 server.listen((HttpRequest request) { 19 server.listen((HttpRequest request) {
21 if (handlers.containsKey(request.uri.path)) { 20 if (handlers.containsKey(request.uri.path)) {
22 handlers[request.uri.path](request, request.response); 21 handlers[request.uri.path](request, request.response);
23 } else { 22 } else {
24 request.listen((_) {}, onDone: () { 23 request.listen((_) {}, onDone: () {
25 request.response.statusCode = 404; 24 request.response.statusCode = 404;
26 request.response.close(); 25 request.response.close();
27 }); 26 });
28 } 27 }
29 }); 28 });
30 29
31 void addRedirectHandler(int number, int statusCode) { 30 void addRedirectHandler(int number, int statusCode) {
32 addRequestHandler( 31 addRequestHandler("/$number",
33 "/$number", 32 (HttpRequest request, HttpResponse response) {
34 (HttpRequest request, HttpResponse response) { 33 response.redirect(
35 response.redirect( 34 Uri.parse("http://127.0.0.1:${server.port}/${number + 1}"));
36 Uri.parse("http://127.0.0.1:${server.port}/${number + 1}")); 35 });
37 });
38 } 36 }
39 37
40 // Setup simple redirect. 38 // Setup simple redirect.
41 addRequestHandler( 39 addRequestHandler("/redirect",
42 "/redirect", 40 (HttpRequest request, HttpResponse response) {
43 (HttpRequest request, HttpResponse response) { 41 response.redirect(Uri.parse("http://127.0.0.1:${server.port}/location"),
44 response.redirect( 42 status: HttpStatus.MOVED_PERMANENTLY);
45 Uri.parse("http://127.0.0.1:${server.port}/location"), 43 });
46 status: HttpStatus.MOVED_PERMANENTLY); 44 addRequestHandler("/location",
47 } 45 (HttpRequest request, HttpResponse response) {
48 ); 46 response.close();
49 addRequestHandler( 47 });
50 "/location",
51 (HttpRequest request, HttpResponse response) {
52 response.close();
53 }
54 );
55 48
56 // Setup redirects with relative url. 49 // Setup redirects with relative url.
57 addRequestHandler( 50 addRequestHandler("/redirectUrl",
58 "/redirectUrl", 51 (HttpRequest request, HttpResponse response) {
59 (HttpRequest request, HttpResponse response) { 52 response.headers.set(HttpHeaders.LOCATION, "/some/relativeUrl");
60 response.headers.set(HttpHeaders.LOCATION, "/some/relativeUrl"); 53 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
61 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 54 response.close();
62 response.close(); 55 });
63 }
64 );
65 56
66 addRequestHandler( 57 addRequestHandler("/some/redirectUrl",
67 "/some/redirectUrl", 58 (HttpRequest request, HttpResponse response) {
68 (HttpRequest request, HttpResponse response) { 59 response.headers.set(HttpHeaders.LOCATION, "relativeUrl");
69 response.headers.set(HttpHeaders.LOCATION, "relativeUrl"); 60 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
70 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 61 response.close();
71 response.close(); 62 });
72 }
73 );
74 63
75 addRequestHandler( 64 addRequestHandler("/some/relativeUrl",
76 "/some/relativeUrl", 65 (HttpRequest request, HttpResponse response) {
77 (HttpRequest request, HttpResponse response) { 66 response.close();
78 response.close(); 67 });
79 }
80 );
81 68
82 addRequestHandler( 69 addRequestHandler("/some/relativeToAbsolute",
83 "/some/relativeToAbsolute", 70 (HttpRequest request, HttpResponse response) {
84 (HttpRequest request, HttpResponse response) { 71 response.redirect(Uri.parse("xxx"), status: HttpStatus.SEE_OTHER);
85 response.redirect(Uri.parse("xxx"), status: HttpStatus.SEE_OTHER); 72 });
86 }
87 );
88 73
89 addRequestHandler( 74 addRequestHandler("/redirectUrl2",
90 "/redirectUrl2", 75 (HttpRequest request, HttpResponse response) {
91 (HttpRequest request, HttpResponse response) { 76 response.headers.set(HttpHeaders.LOCATION, "location");
92 response.headers.set(HttpHeaders.LOCATION, "location"); 77 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
93 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 78 response.close();
94 response.close(); 79 });
95 }
96 );
97 80
98 addRequestHandler( 81 addRequestHandler("/redirectUrl3",
99 "/redirectUrl3", 82 (HttpRequest request, HttpResponse response) {
100 (HttpRequest request, HttpResponse response) { 83 response.headers.set(HttpHeaders.LOCATION, "./location");
101 response.headers.set(HttpHeaders.LOCATION, "./location"); 84 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
102 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 85 response.close();
103 response.close(); 86 });
104 }
105 );
106 87
107 addRequestHandler( 88 addRequestHandler("/redirectUrl4",
108 "/redirectUrl4", 89 (HttpRequest request, HttpResponse response) {
109 (HttpRequest request, HttpResponse response) { 90 response.headers.set(HttpHeaders.LOCATION, "./a/b/../../location");
110 response.headers.set(HttpHeaders.LOCATION, "./a/b/../../location"); 91 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
111 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 92 response.close();
112 response.close(); 93 });
113 }
114 );
115 94
116 addRequestHandler( 95 addRequestHandler("/redirectUrl5",
117 "/redirectUrl5", 96 (HttpRequest request, HttpResponse response) {
118 (HttpRequest request, HttpResponse response) { 97 response.headers
119 response.headers.set(HttpHeaders.LOCATION, 98 .set(HttpHeaders.LOCATION, "//127.0.0.1:${server.port}/location");
120 "//127.0.0.1:${server.port}/location"); 99 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
121 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 100 response.close();
122 response.close(); 101 });
123 }
124 );
125 102
126 // Setup redirect chain. 103 // Setup redirect chain.
127 int n = 1; 104 int n = 1;
128 addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY); 105 addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY);
129 addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY); 106 addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY);
130 addRedirectHandler(n++, HttpStatus.SEE_OTHER); 107 addRedirectHandler(n++, HttpStatus.SEE_OTHER);
131 addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT); 108 addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT);
132 for (int i = n; i < 10; i++) { 109 for (int i = n; i < 10; i++) {
133 addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY); 110 addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY);
134 } 111 }
135 112
136 // Setup redirect loop. 113 // Setup redirect loop.
137 addRequestHandler( 114 addRequestHandler("/A", (HttpRequest request, HttpResponse response) {
138 "/A", 115 response.headers
139 (HttpRequest request, HttpResponse response) { 116 .set(HttpHeaders.LOCATION, "http://127.0.0.1:${server.port}/B");
140 response.headers.set(HttpHeaders.LOCATION, 117 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
141 "http://127.0.0.1:${server.port}/B"); 118 response.close();
142 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 119 });
143 response.close(); 120 addRequestHandler("/B", (HttpRequest request, HttpResponse response) {
144 } 121 response.headers
145 ); 122 .set(HttpHeaders.LOCATION, "http://127.0.0.1:${server.port}/A");
146 addRequestHandler( 123 response.statusCode = HttpStatus.MOVED_TEMPORARILY;
147 "/B", 124 response.close();
148 (HttpRequest request, HttpResponse response) { 125 });
149 response.headers.set(HttpHeaders.LOCATION,
150 "http://127.0.0.1:${server.port}/A");
151 response.statusCode = HttpStatus.MOVED_TEMPORARILY;
152 response.close();
153 }
154 );
155 126
156 // Setup redirect checking headers. 127 // Setup redirect checking headers.
157 addRequestHandler( 128 addRequestHandler("/src", (HttpRequest request, HttpResponse response) {
158 "/src", 129 Expect.equals("value", request.headers.value("X-Request-Header"));
159 (HttpRequest request, HttpResponse response) { 130 response.headers
160 Expect.equals("value", request.headers.value("X-Request-Header")); 131 .set(HttpHeaders.LOCATION, "http://127.0.0.1:${server.port}/target");
161 response.headers.set(HttpHeaders.LOCATION, 132 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
162 "http://127.0.0.1:${server.port}/target"); 133 response.close();
163 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 134 });
164 response.close(); 135 addRequestHandler("/target", (HttpRequest request, HttpResponse response) {
165 } 136 Expect.equals("value", request.headers.value("X-Request-Header"));
166 ); 137 response.close();
167 addRequestHandler( 138 });
168 "/target",
169 (HttpRequest request, HttpResponse response) {
170 Expect.equals("value", request.headers.value("X-Request-Header"));
171 response.close();
172 }
173 );
174 139
175 // Setup redirect for 301 where POST should not redirect. 140 // Setup redirect for 301 where POST should not redirect.
176 addRequestHandler( 141 addRequestHandler("/301src", (HttpRequest request, HttpResponse response) {
177 "/301src", 142 Expect.equals("POST", request.method);
143 request.listen((_) {}, onDone: () {
144 response.headers.set(
145 HttpHeaders.LOCATION, "http://127.0.0.1:${server.port}/301target");
146 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
147 response.close();
148 });
149 });
150 addRequestHandler("/301target",
178 (HttpRequest request, HttpResponse response) { 151 (HttpRequest request, HttpResponse response) {
179 Expect.equals("POST", request.method); 152 Expect.fail("Redirect of POST should not happen");
180 request.listen( 153 });
181 (_) {},
182 onDone: () {
183 response.headers.set(
184 HttpHeaders.LOCATION,
185 "http://127.0.0.1:${server.port}/301target");
186 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
187 response.close();
188 });
189 });
190 addRequestHandler(
191 "/301target",
192 (HttpRequest request, HttpResponse response) {
193 Expect.fail("Redirect of POST should not happen");
194 }
195 );
196 154
197 // Setup redirect for 303 where POST should turn into GET. 155 // Setup redirect for 303 where POST should turn into GET.
198 addRequestHandler( 156 addRequestHandler("/303src", (HttpRequest request, HttpResponse response) {
199 "/303src", 157 request.listen((_) {}, onDone: () {
158 Expect.equals("POST", request.method);
159 response.headers.set(
160 HttpHeaders.LOCATION, "http://127.0.0.1:${server.port}/303target");
161 response.statusCode = HttpStatus.SEE_OTHER;
162 response.close();
163 });
164 });
165 addRequestHandler("/303target",
200 (HttpRequest request, HttpResponse response) { 166 (HttpRequest request, HttpResponse response) {
201 request.listen((_) {}, onDone: () { 167 Expect.equals("GET", request.method);
202 Expect.equals("POST", request.method); 168 response.close();
203 response.headers.set( 169 });
204 HttpHeaders.LOCATION,
205 "http://127.0.0.1:${server.port}/303target");
206 response.statusCode = HttpStatus.SEE_OTHER;
207 response.close();
208 });
209 });
210 addRequestHandler(
211 "/303target",
212 (HttpRequest request, HttpResponse response) {
213 Expect.equals("GET", request.method);
214 response.close();
215 });
216 170
217 // Setup redirect where we close the connection. 171 // Setup redirect where we close the connection.
218 addRequestHandler( 172 addRequestHandler("/closing", (HttpRequest request, HttpResponse response) {
219 "/closing", 173 response.headers
220 (HttpRequest request, HttpResponse response) { 174 .set(HttpHeaders.LOCATION, "http://127.0.0.1:${server.port}/");
221 response.headers.set(HttpHeaders.LOCATION, 175 response.statusCode = HttpStatus.FOUND;
222 "http://127.0.0.1:${server.port}/"); 176 response.persistentConnection = false;
223 response.statusCode = HttpStatus.FOUND; 177 response.close();
224 response.persistentConnection = false; 178 });
225 response.close();
226 });
227 179
228 completer.complete(server); 180 completer.complete(server);
229 }); 181 });
230 return completer.future; 182 return completer.future;
231 } 183 }
232 184
233 void checkRedirects(int redirectCount, HttpClientResponse response) { 185 void checkRedirects(int redirectCount, HttpClientResponse response) {
234 if (redirectCount < 2) { 186 if (redirectCount < 2) {
235 Expect.isTrue(response.redirects.isEmpty); 187 Expect.isTrue(response.redirects.isEmpty);
236 } else { 188 } else {
237 Expect.equals(redirectCount - 1, response.redirects.length); 189 Expect.equals(redirectCount - 1, response.redirects.length);
238 for (int i = 0; i < redirectCount - 2; i++) { 190 for (int i = 0; i < redirectCount - 2; i++) {
239 Expect.equals(response.redirects[i].location.path, "/${i + 2}"); 191 Expect.equals(response.redirects[i].location.path, "/${i + 2}");
240 } 192 }
241 } 193 }
242 } 194 }
243 195
244 void testManualRedirect() { 196 void testManualRedirect() {
245 setupServer().then((server) { 197 setupServer().then((server) {
246 HttpClient client = new HttpClient(); 198 HttpClient client = new HttpClient();
247 199
248 int redirectCount = 0; 200 int redirectCount = 0;
249 handleResponse(HttpClientResponse response) { 201 handleResponse(HttpClientResponse response) {
250 response.listen( 202 response.listen((_) => Expect.fail("Response data not expected"),
251 (_) => Expect.fail("Response data not expected"),
252 onDone: () { 203 onDone: () {
253 redirectCount++; 204 redirectCount++;
254 if (redirectCount < 10) { 205 if (redirectCount < 10) {
255 Expect.isTrue(response.isRedirect); 206 Expect.isTrue(response.isRedirect);
256 checkRedirects(redirectCount, response); 207 checkRedirects(redirectCount, response);
257 response.redirect().then(handleResponse); 208 response.redirect().then(handleResponse);
258 } else { 209 } else {
259 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); 210 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode);
260 server.close(); 211 server.close();
261 client.close(); 212 client.close();
262 } 213 }
263 }); 214 });
264 } 215 }
265 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")) 216
266 .then((HttpClientRequest request) { 217 client
267 request.followRedirects = false; 218 .getUrl(Uri.parse("http://127.0.0.1:${server.port}/1"))
268 return request.close(); 219 .then((HttpClientRequest request) {
269 }) 220 request.followRedirects = false;
270 .then(handleResponse); 221 return request.close();
222 }).then(handleResponse);
271 }); 223 });
272 } 224 }
273 225
274 void testManualRedirectWithHeaders() { 226 void testManualRedirectWithHeaders() {
275 setupServer().then((server) { 227 setupServer().then((server) {
276 HttpClient client = new HttpClient(); 228 HttpClient client = new HttpClient();
277 229
278 int redirectCount = 0; 230 int redirectCount = 0;
279 231
280 handleResponse(HttpClientResponse response) { 232 handleResponse(HttpClientResponse response) {
281 response.listen( 233 response.listen((_) => Expect.fail("Response data not expected"),
282 (_) => Expect.fail("Response data not expected"),
283 onDone: () { 234 onDone: () {
284 redirectCount++; 235 redirectCount++;
285 if (redirectCount < 2) { 236 if (redirectCount < 2) {
286 Expect.isTrue(response.isRedirect); 237 Expect.isTrue(response.isRedirect);
287 response.redirect().then(handleResponse); 238 response.redirect().then(handleResponse);
288 } else { 239 } else {
289 Expect.equals(HttpStatus.OK, response.statusCode); 240 Expect.equals(HttpStatus.OK, response.statusCode);
290 server.close(); 241 server.close();
291 client.close(); 242 client.close();
292 } 243 }
293 }); 244 });
294 } 245 }
295 246
296 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")) 247 client
297 .then((HttpClientRequest request) { 248 .getUrl(Uri.parse("http://127.0.0.1:${server.port}/src"))
298 request.followRedirects = false; 249 .then((HttpClientRequest request) {
299 request.headers.add("X-Request-Header", "value"); 250 request.followRedirects = false;
300 return request.close(); 251 request.headers.add("X-Request-Header", "value");
301 }).then(handleResponse); 252 return request.close();
253 }).then(handleResponse);
302 }); 254 });
303 } 255 }
304 256
305 void testAutoRedirect() { 257 void testAutoRedirect() {
306 setupServer().then((server) { 258 setupServer().then((server) {
307 HttpClient client = new HttpClient(); 259 HttpClient client = new HttpClient();
308 260
309 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/redirect")) 261 client
310 .then((HttpClientRequest request) { 262 .getUrl(Uri.parse("http://127.0.0.1:${server.port}/redirect"))
311 return request.close(); 263 .then((HttpClientRequest request) {
312 }) 264 return request.close();
313 .then((HttpClientResponse response) { 265 }).then((HttpClientResponse response) {
314 response.listen( 266 response.listen((_) => Expect.fail("Response data not expected"),
315 (_) => Expect.fail("Response data not expected"), 267 onDone: () {
316 onDone: () { 268 Expect.equals(1, response.redirects.length);
317 Expect.equals(1, response.redirects.length); 269 server.close();
318 server.close(); 270 client.close();
319 client.close();
320 });
321 }); 271 });
272 });
322 }); 273 });
323 } 274 }
324 275
325 void testAutoRedirectWithHeaders() { 276 void testAutoRedirectWithHeaders() {
326 setupServer().then((server) { 277 setupServer().then((server) {
327 HttpClient client = new HttpClient(); 278 HttpClient client = new HttpClient();
328 279
329 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")) 280 client
330 .then((HttpClientRequest request) { 281 .getUrl(Uri.parse("http://127.0.0.1:${server.port}/src"))
331 request.headers.add("X-Request-Header", "value"); 282 .then((HttpClientRequest request) {
332 return request.close(); 283 request.headers.add("X-Request-Header", "value");
333 }) 284 return request.close();
334 .then((HttpClientResponse response) { 285 }).then((HttpClientResponse response) {
335 response.listen( 286 response.listen((_) => Expect.fail("Response data not expected"),
336 (_) => Expect.fail("Response data not expected"), 287 onDone: () {
337 onDone: () { 288 Expect.equals(1, response.redirects.length);
338 Expect.equals(1, response.redirects.length); 289 server.close();
339 server.close(); 290 client.close();
340 client.close();
341 });
342 }); 291 });
292 });
343 }); 293 });
344 } 294 }
345 295
346 void testAutoRedirect301POST() { 296 void testAutoRedirect301POST() {
347 setupServer().then((server) { 297 setupServer().then((server) {
348 HttpClient client = new HttpClient(); 298 HttpClient client = new HttpClient();
349 299
350 client.postUrl(Uri.parse("http://127.0.0.1:${server.port}/301src")) 300 client
351 .then((HttpClientRequest request) { 301 .postUrl(Uri.parse("http://127.0.0.1:${server.port}/301src"))
352 return request.close(); 302 .then((HttpClientRequest request) {
353 }) 303 return request.close();
354 .then((HttpClientResponse response) { 304 }).then((HttpClientResponse response) {
355 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); 305 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode);
356 response.listen( 306 response.listen((_) => Expect.fail("Response data not expected"),
357 (_) => Expect.fail("Response data not expected"), 307 onDone: () {
358 onDone: () { 308 Expect.equals(0, response.redirects.length);
359 Expect.equals(0, response.redirects.length); 309 server.close();
360 server.close(); 310 client.close();
361 client.close();
362 });
363 }); 311 });
312 });
364 }); 313 });
365 } 314 }
366 315
367 void testAutoRedirect303POST() { 316 void testAutoRedirect303POST() {
368 setupServer().then((server) { 317 setupServer().then((server) {
369 HttpClient client = new HttpClient(); 318 HttpClient client = new HttpClient();
370 319
371 client.postUrl(Uri.parse("http://127.0.0.1:${server.port}/303src")) 320 client
372 .then((HttpClientRequest request) { 321 .postUrl(Uri.parse("http://127.0.0.1:${server.port}/303src"))
373 return request.close(); 322 .then((HttpClientRequest request) {
374 }) 323 return request.close();
375 .then((HttpClientResponse response) { 324 }).then((HttpClientResponse response) {
376 Expect.equals(HttpStatus.OK, response.statusCode); 325 Expect.equals(HttpStatus.OK, response.statusCode);
377 response.listen( 326 response.listen((_) => Expect.fail("Response data not expected"),
378 (_) => Expect.fail("Response data not expected"), 327 onDone: () {
379 onDone: () { 328 Expect.equals(1, response.redirects.length);
380 Expect.equals(1, response.redirects.length); 329 server.close();
381 server.close(); 330 client.close();
382 client.close();
383 });
384 }); 331 });
332 });
385 }); 333 });
386 } 334 }
387 335
388 void testAutoRedirectLimit() { 336 void testAutoRedirectLimit() {
389 setupServer().then((server) { 337 setupServer().then((server) {
390 HttpClient client = new HttpClient(); 338 HttpClient client = new HttpClient();
391 339
392 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")) 340 client
393 .then((HttpClientRequest request) => request.close()) 341 .getUrl(Uri.parse("http://127.0.0.1:${server.port}/1"))
394 .catchError((error) { 342 .then((HttpClientRequest request) => request.close())
395 Expect.equals(5, error.redirects.length); 343 .catchError((error) {
396 server.close(); 344 Expect.equals(5, error.redirects.length);
397 client.close(); 345 server.close();
398 }, test: (e) => e is RedirectException); 346 client.close();
347 }, test: (e) => e is RedirectException);
399 }); 348 });
400 } 349 }
401 350
402 void testRedirectLoop() { 351 void testRedirectLoop() {
403 setupServer().then((server) { 352 setupServer().then((server) {
404 HttpClient client = new HttpClient(); 353 HttpClient client = new HttpClient();
405 354
406 int redirectCount = 0; 355 int redirectCount = 0;
407 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/A")) 356 client
408 .then((HttpClientRequest request) => request.close()) 357 .getUrl(Uri.parse("http://127.0.0.1:${server.port}/A"))
409 .catchError((error) { 358 .then((HttpClientRequest request) => request.close())
410 Expect.equals(2, error.redirects.length); 359 .catchError((error) {
411 server.close(); 360 Expect.equals(2, error.redirects.length);
412 client.close(); 361 server.close();
413 }, test: (e) => e is RedirectException); 362 client.close();
363 }, test: (e) => e is RedirectException);
414 }); 364 });
415 } 365 }
416 366
417 void testRedirectClosingConnection() { 367 void testRedirectClosingConnection() {
418 setupServer().then((server) { 368 setupServer().then((server) {
419 HttpClient client = new HttpClient(); 369 HttpClient client = new HttpClient();
420 370
421 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/closing")) 371 client
372 .getUrl(Uri.parse("http://127.0.0.1:${server.port}/closing"))
422 .then((request) => request.close()) 373 .then((request) => request.close())
423 .then((response) { 374 .then((response) {
424 response.listen( 375 response.listen((_) {}, onDone: () {
425 (_) {}, 376 Expect.equals(1, response.redirects.length);
426 onDone: () { 377 server.close();
427 Expect.equals(1, response.redirects.length); 378 client.close();
428 server.close(); 379 });
429 client.close(); 380 });
430 });
431 });
432 }); 381 });
433 } 382 }
434 383
435 void testRedirectRelativeUrl() { 384 void testRedirectRelativeUrl() {
436 testPath(String path) { 385 testPath(String path) {
437 setupServer().then((server) { 386 setupServer().then((server) {
438 HttpClient client = new HttpClient(); 387 HttpClient client = new HttpClient();
439 388
440 print(path); 389 print(path);
441 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}$path")) 390 client
391 .getUrl(Uri.parse("http://127.0.0.1:${server.port}$path"))
442 .then((request) => request.close()) 392 .then((request) => request.close())
443 .then((response) { 393 .then((response) {
444 response.listen( 394 response.listen((_) {}, onDone: () {
445 (_) {}, 395 Expect.equals(HttpStatus.OK, response.statusCode);
446 onDone: () { 396 Expect.equals(1, response.redirects.length);
447 Expect.equals(HttpStatus.OK, response.statusCode); 397 server.close();
448 Expect.equals(1, response.redirects.length); 398 client.close();
449 server.close(); 399 });
450 client.close(); 400 });
451 });
452 });
453 }); 401 });
454 } 402 }
403
455 testPath("/redirectUrl"); 404 testPath("/redirectUrl");
456 testPath("/some/redirectUrl"); 405 testPath("/some/redirectUrl");
457 testPath("/redirectUrl2"); 406 testPath("/redirectUrl2");
458 testPath("/redirectUrl3"); 407 testPath("/redirectUrl3");
459 testPath("/redirectUrl4"); 408 testPath("/redirectUrl4");
460 testPath("/redirectUrl5"); 409 testPath("/redirectUrl5");
461 } 410 }
462 411
463 void testRedirectRelativeToAbsolute() { 412 void testRedirectRelativeToAbsolute() {
464 setupServer().then((server) { 413 setupServer().then((server) {
465 HttpClient client = new HttpClient(); 414 HttpClient client = new HttpClient();
466 415
467 int redirectCount = 0; 416 int redirectCount = 0;
468 handleResponse(HttpClientResponse response) { 417 handleResponse(HttpClientResponse response) {
469 response.listen( 418 response.listen((_) => Expect.fail("Response data not expected"),
470 (_) => Expect.fail("Response data not expected"),
471 onDone: () { 419 onDone: () {
472 Expect.equals(HttpStatus.SEE_OTHER, response.statusCode); 420 Expect.equals(HttpStatus.SEE_OTHER, response.statusCode);
473 Expect.equals("xxx", 421 Expect.equals("xxx", response.headers["Location"][0]);
474 response.headers["Location"][0]); 422 Expect.isTrue(response.isRedirect);
475 Expect.isTrue(response.isRedirect); 423 server.close();
476 server.close(); 424 client.close();
477 client.close(); 425 });
478 });
479 } 426 }
480 client.getUrl( 427
481 Uri.parse("http://127.0.0.1:${server.port}/some/relativeToAbsolute")) 428 client
482 .then((HttpClientRequest request) { 429 .getUrl(Uri
483 request.followRedirects = false; 430 .parse("http://127.0.0.1:${server.port}/some/relativeToAbsolute"))
484 return request.close(); 431 .then((HttpClientRequest request) {
485 }) 432 request.followRedirects = false;
486 .then(handleResponse); 433 return request.close();
434 }).then(handleResponse);
487 }); 435 });
488 } 436 }
489 437
490
491 main() { 438 main() {
492 testManualRedirect(); 439 testManualRedirect();
493 testManualRedirectWithHeaders(); 440 testManualRedirectWithHeaders();
494 testAutoRedirect(); 441 testAutoRedirect();
495 testAutoRedirectWithHeaders(); 442 testAutoRedirectWithHeaders();
496 testAutoRedirect301POST(); 443 testAutoRedirect301POST();
497 testAutoRedirect303POST(); 444 testAutoRedirect303POST();
498 testAutoRedirectLimit(); 445 testAutoRedirectLimit();
499 testRedirectLoop(); 446 testRedirectLoop();
500 testRedirectClosingConnection(); 447 testRedirectClosingConnection();
501 testRedirectRelativeUrl(); 448 testRedirectRelativeUrl();
502 testRedirectRelativeToAbsolute(); 449 testRedirectRelativeToAbsolute();
503 } 450 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698