| OLD | NEW |
| 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 "dart:io"; | 6 import "dart:io"; |
| 7 import "dart:uri"; | 7 import "dart:uri"; |
| 8 | 8 |
| 9 HttpServer setupServer() { | 9 HttpServer setupServer() { |
| 10 HttpServer server = new HttpServer(); | 10 HttpServer server = new HttpServer(); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 } | 121 } |
| 122 ); | 122 ); |
| 123 server.addRequestHandler( | 123 server.addRequestHandler( |
| 124 (HttpRequest request) => request.path == "/303target", | 124 (HttpRequest request) => request.path == "/303target", |
| 125 (HttpRequest request, HttpResponse response) { | 125 (HttpRequest request, HttpResponse response) { |
| 126 Expect.equals("GET", request.method); | 126 Expect.equals("GET", request.method); |
| 127 response.outputStream.close(); | 127 response.outputStream.close(); |
| 128 } | 128 } |
| 129 ); | 129 ); |
| 130 | 130 |
| 131 // Setup redirect where we close the connection. |
| 132 server.addRequestHandler( |
| 133 (HttpRequest request) => request.path == "/closing", |
| 134 (HttpRequest request, HttpResponse response) { |
| 135 response.headers.set(HttpHeaders.LOCATION, |
| 136 "http://127.0.0.1:${server.port}/"); |
| 137 response.statusCode = HttpStatus.FOUND; |
| 138 response.persistentConnection = false; |
| 139 response.outputStream.close(); |
| 140 } |
| 141 ); |
| 142 |
| 131 return server; | 143 return server; |
| 132 } | 144 } |
| 133 | 145 |
| 134 void checkRedirects(int redirectCount, HttpClientConnection conn) { | 146 void checkRedirects(int redirectCount, HttpClientConnection conn) { |
| 135 if (redirectCount < 2) { | 147 if (redirectCount < 2) { |
| 136 Expect.isNull(conn.redirects); | 148 Expect.isNull(conn.redirects); |
| 137 } else { | 149 } else { |
| 138 Expect.equals(redirectCount - 1, conn.redirects.length); | 150 Expect.equals(redirectCount - 1, conn.redirects.length); |
| 139 for (int i = 0; i < redirectCount - 2; i++) { | 151 for (int i = 0; i < redirectCount - 2; i++) { |
| 140 Expect.equals(conn.redirects[i].location.path, "/${i + 2}"); | 152 Expect.equals(conn.redirects[i].location.path, "/${i + 2}"); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | 357 response.inputStream.onClosed = () => Expect.fail("Response not expected"); |
| 346 }; | 358 }; |
| 347 conn.onError = (e) { | 359 conn.onError = (e) { |
| 348 Expect.isTrue(e is RedirectLoopException); | 360 Expect.isTrue(e is RedirectLoopException); |
| 349 Expect.equals(2, e.redirects.length); | 361 Expect.equals(2, e.redirects.length); |
| 350 server.close(); | 362 server.close(); |
| 351 client.shutdown(); | 363 client.shutdown(); |
| 352 }; | 364 }; |
| 353 } | 365 } |
| 354 | 366 |
| 367 void testRedirectClosingConnection() { |
| 368 HttpServer server = setupServer(); |
| 369 HttpClient client = new HttpClient(); |
| 370 |
| 371 int redirectCount = 0; |
| 372 HttpClientConnection conn = |
| 373 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/closing")); |
| 374 |
| 375 conn.followRedirects = true; |
| 376 conn.onResponse = (HttpClientResponse response) { |
| 377 response.inputStream.onData = () => Expect.fail("Response not expected"); |
| 378 response.inputStream.onClosed = () => Expect.fail("Response not expected"); |
| 379 }; |
| 380 conn.onError = (e) { |
| 381 Expect.isTrue(e is RedirectException); |
| 382 Expect.isNull(e.redirects); |
| 383 server.close(); |
| 384 client.shutdown(); |
| 385 }; |
| 386 } |
| 387 |
| 355 main() { | 388 main() { |
| 356 testManualRedirect(); | 389 testManualRedirect(); |
| 357 testManualRedirectWithHeaders(); | 390 testManualRedirectWithHeaders(); |
| 358 testAutoRedirect(); | 391 testAutoRedirect(); |
| 359 testAutoRedirectWithHeaders(); | 392 testAutoRedirectWithHeaders(); |
| 360 testAutoRedirect301POST(); | 393 testAutoRedirect301POST(); |
| 361 testAutoRedirect303POST(); | 394 testAutoRedirect303POST(); |
| 362 testAutoRedirectLimit(); | 395 testAutoRedirectLimit(); |
| 363 testRedirectLoop(); | 396 testRedirectLoop(); |
| 397 testRedirectClosingConnection(); |
| 364 } | 398 } |
| OLD | NEW |