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

Unified Diff: tests/standalone/io/http_redirect_test.dart

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r18818 Created 7 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: tests/standalone/io/http_redirect_test.dart
diff --git a/tests/standalone/io/http_redirect_test.dart b/tests/standalone/io/http_redirect_test.dart
index 4df9782b8ec0c3de39b553d2f9cb6935c74976d4..3209496ff0953425a8f4d097637c45bea986c0cb 100644
--- a/tests/standalone/io/http_redirect_test.dart
+++ b/tests/standalone/io/http_redirect_test.dart
@@ -3,386 +3,370 @@
// BSD-style license that can be found in the LICENSE file.
//
+import "dart:async";
import "dart:io";
import "dart:uri";
-HttpServer setupServer() {
- HttpServer server = new HttpServer();
- server.listen("127.0.0.1", 0, backlog: 5);
+Future<HttpServer> setupServer() {
+ Completer completer = new Completer();
+ HttpServer.bind().then((server) {
- void addRedirectHandler(int number, int statusCode) {
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/$number",
+ var handlers = new Map<String, Function>();
+ addRequestHandler(String path, void handler(HttpRequest request,
+ HttpResponse response)) {
+ handlers[path] = handler;
+ }
+
+ server.listen((HttpRequest request) {
+ if (handlers.containsKey(request.uri.path)) {
+ handlers[request.uri.path](request, request.response);
+ } else {
+ request.listen((_) {}, onDone: () {
+ request.response.statusCode = 404;
+ request.response.close();
+ });
+ }
+ });
+
+ void addRedirectHandler(int number, int statusCode) {
+ addRequestHandler(
+ "/$number",
+ (HttpRequest request, HttpResponse response) {
+ response.headers.set(HttpHeaders.LOCATION,
+ "http://127.0.0.1:${server.port}/${number + 1}");
+ response.statusCode = statusCode;
+ response.close();
+ });
+ }
+
+ // Setup simple redirect.
+ addRequestHandler(
+ "/redirect",
(HttpRequest request, HttpResponse response) {
- response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/${number + 1}");
- response.statusCode = statusCode;
- response.outputStream.close();
- });
- }
+ response.headers.set(HttpHeaders.LOCATION,
+ "http://127.0.0.1:${server.port}/location");
+ response.statusCode = HttpStatus.MOVED_PERMANENTLY;
+ response.close();
+ }
+ );
+ addRequestHandler(
+ "/location",
+ (HttpRequest request, HttpResponse response) {
+ response.close();
+ }
+ );
+
+ // Setup redirect chain.
+ int n = 1;
+ addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY);
+ addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY);
+ addRedirectHandler(n++, HttpStatus.SEE_OTHER);
+ addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT);
+ for (int i = n; i < 10; i++) {
+ addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY);
+ }
- // Setup simple redirect.
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/redirect",
- (HttpRequest request, HttpResponse response) {
- response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/location");
- response.statusCode = HttpStatus.MOVED_PERMANENTLY;
- response.outputStream.close();
- }
- );
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/location",
- (HttpRequest request, HttpResponse response) {
- response.outputStream.close();
- }
- );
-
- // Setup redirect chain.
- int n = 1;
- addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY);
- addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY);
- addRedirectHandler(n++, HttpStatus.SEE_OTHER);
- addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT);
- for (int i = n; i < 10; i++) {
- addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY);
- }
+ // Setup redirect loop.
+ addRequestHandler(
+ "/A",
+ (HttpRequest request, HttpResponse response) {
+ response.headers.set(HttpHeaders.LOCATION,
+ "http://127.0.0.1:${server.port}/B");
+ response.statusCode = HttpStatus.MOVED_PERMANENTLY;
+ response.close();
+ }
+ );
+ addRequestHandler(
+ "/B",
+ (HttpRequest request, HttpResponse response) {
+ response.headers.set(HttpHeaders.LOCATION,
+ "http://127.0.0.1:${server.port}/A");
+ response.statusCode = HttpStatus.MOVED_TEMPORARILY;
+ response.close();
+ }
+ );
+
+ // Setup redirect checking headers.
+ addRequestHandler(
+ "/src",
+ (HttpRequest request, HttpResponse response) {
+ Expect.equals("value", request.headers.value("X-Request-Header"));
+ response.headers.set(HttpHeaders.LOCATION,
+ "http://127.0.0.1:${server.port}/target");
+ response.statusCode = HttpStatus.MOVED_PERMANENTLY;
+ response.close();
+ }
+ );
+ addRequestHandler(
+ "/target",
+ (HttpRequest request, HttpResponse response) {
+ Expect.equals("value", request.headers.value("X-Request-Header"));
+ response.close();
+ }
+ );
+
+ // Setup redirect for 301 where POST should not redirect.
+ addRequestHandler(
+ "/301src",
+ (HttpRequest request, HttpResponse response) {
+ Expect.equals("POST", request.method);
+ request.listen(
+ (_) {},
+ onDone: () {
+ response.headers.set(
+ HttpHeaders.LOCATION,
+ "http://127.0.0.1:${server.port}/301target");
+ response.statusCode = HttpStatus.MOVED_PERMANENTLY;
+ response.close();
+ });
+ });
+ addRequestHandler(
+ "/301target",
+ (HttpRequest request, HttpResponse response) {
+ Expect.fail("Redirect of POST should not happen");
+ }
+ );
+
+ // Setup redirect for 303 where POST should turn into GET.
+ addRequestHandler(
+ "/303src",
+ (HttpRequest request, HttpResponse response) {
+ request.listen((_) {}, onDone: () {
+ Expect.equals("POST", request.method);
+ request.listen(
+ (_) {},
+ onDone: () {
+ response.headers.set(
+ HttpHeaders.LOCATION,
+ "http://127.0.0.1:${server.port}/303target");
+ response.statusCode = HttpStatus.SEE_OTHER;
+ response.close();
+ });
+ });
+ });
+ addRequestHandler(
+ "/303target",
+ (HttpRequest request, HttpResponse response) {
+ Expect.equals("GET", request.method);
+ response.close();
+ });
- // Setup redirect loop.
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/A",
- (HttpRequest request, HttpResponse response) {
- response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/B");
- response.statusCode = HttpStatus.MOVED_PERMANENTLY;
- response.outputStream.close();
- }
- );
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/B",
- (HttpRequest request, HttpResponse response) {
- response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/A");
- response.statusCode = HttpStatus.MOVED_TEMPORARILY;
- response.outputStream.close();
- }
- );
-
- // Setup redirect checking headers.
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/src",
- (HttpRequest request, HttpResponse response) {
- Expect.equals("value", request.headers.value("X-Request-Header"));
- response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/target");
- response.statusCode = HttpStatus.MOVED_PERMANENTLY;
- response.outputStream.close();
- }
- );
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/target",
- (HttpRequest request, HttpResponse response) {
- Expect.equals("value", request.headers.value("X-Request-Header"));
- response.outputStream.close();
- }
- );
-
- // Setup redirect for 301 where POST should not redirect.
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/301src",
- (HttpRequest request, HttpResponse response) {
- Expect.equals("POST", request.method);
- response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/301target");
- response.statusCode = HttpStatus.MOVED_PERMANENTLY;
- response.outputStream.close();
- }
- );
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/301target",
- (HttpRequest request, HttpResponse response) {
- Expect.fail("Redirect of POST should not happen");
- }
- );
-
- // Setup redirect for 303 where POST should turn into GET.
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/303src",
- (HttpRequest request, HttpResponse response) {
- Expect.equals("POST", request.method);
- Expect.equals(10, request.contentLength);
- request.inputStream.onData = request.inputStream.read;
- request.inputStream.onClosed = () {
+ // Setup redirect where we close the connection.
+ addRequestHandler(
+ "/closing",
+ (HttpRequest request, HttpResponse response) {
response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/303target");
- response.statusCode = HttpStatus.SEE_OTHER;
- response.outputStream.close();
- };
- }
- );
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/303target",
- (HttpRequest request, HttpResponse response) {
- Expect.equals("GET", request.method);
- response.outputStream.close();
- }
- );
-
- // Setup redirect where we close the connection.
- server.addRequestHandler(
- (HttpRequest request) => request.path == "/closing",
- (HttpRequest request, HttpResponse response) {
- response.headers.set(HttpHeaders.LOCATION,
- "http://127.0.0.1:${server.port}/");
- response.statusCode = HttpStatus.FOUND;
- response.persistentConnection = false;
- response.outputStream.close();
- }
- );
-
- return server;
+ "http://127.0.0.1:${server.port}/");
+ response.statusCode = HttpStatus.FOUND;
+ response.persistentConnection = false;
+ response.close();
+ });
+
+ completer.complete(server);
+ });
+ return completer.future;
}
-void checkRedirects(int redirectCount, HttpClientConnection conn) {
+void checkRedirects(int redirectCount, HttpClientResponse response) {
if (redirectCount < 2) {
- Expect.isNull(conn.redirects);
+ Expect.isTrue(response.redirects.isEmpty);
} else {
- Expect.equals(redirectCount - 1, conn.redirects.length);
+ Expect.equals(redirectCount - 1, response.redirects.length);
for (int i = 0; i < redirectCount - 2; i++) {
- Expect.equals(conn.redirects[i].location.path, "/${i + 2}");
+ Expect.equals(response.redirects[i].location.path, "/${i + 2}");
}
}
}
void testManualRedirect() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- int redirectCount = 0;
- HttpClientConnection conn =
- client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1"));
- conn.followRedirects = false;
- conn.onResponse = (HttpClientResponse response) {
- response.inputStream.onData = response.inputStream.read;
- response.inputStream.onClosed = () {
- redirectCount++;
- if (redirectCount < 10) {
- Expect.isTrue(response.isRedirect);
- checkRedirects(redirectCount, conn);
- conn.redirect();
- } else {
- Expect.equals(HttpStatus.NOT_FOUND, response.statusCode);
- server.close();
- client.shutdown();
- }
- };
- };
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ int redirectCount = 0;
+ handleResponse(HttpClientResponse response) {
+ response.listen(
+ (_) => Expect.fail("Response data not expected"),
+ onDone: () {
+ redirectCount++;
+ if (redirectCount < 10) {
+ Expect.isTrue(response.isRedirect);
+ checkRedirects(redirectCount, response);
+ response.redirect().then(handleResponse);
+ } else {
+ Expect.equals(HttpStatus.NOT_FOUND, response.statusCode);
+ server.close();
+ client.close();
+ }
+ });
+ }
+ client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1"))
+ .then((HttpClientRequest request) {
+ request.followRedirects = false;
+ return request.close();
+ })
+ .then(handleResponse);
+ });
}
void testManualRedirectWithHeaders() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- int redirectCount = 0;
- HttpClientConnection conn =
- client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src"));
- conn.followRedirects = false;
- conn.onRequest = (HttpClientRequest request) {
- request.headers.add("X-Request-Header", "value");
- request.outputStream.close();
- };
- conn.onResponse = (HttpClientResponse response) {
- response.inputStream.onData = response.inputStream.read;
- response.inputStream.onClosed = () {
- redirectCount++;
- if (redirectCount < 2) {
- Expect.isTrue(response.isRedirect);
- conn.redirect();
- } else {
- Expect.equals(HttpStatus.OK, response.statusCode);
- server.close();
- client.shutdown();
- }
- };
- };
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ int redirectCount = 0;
+
+ handleResponse(HttpClientResponse response) {
+ response.listen(
+ (_) => Expect.fail("Response data not expected"),
+ onDone: () {
+ redirectCount++;
+ if (redirectCount < 2) {
+ Expect.isTrue(response.isRedirect);
+ response.redirect().then(handleResponse);
+ } else {
+ Expect.equals(HttpStatus.OK, response.statusCode);
+ server.close();
+ client.close();
+ }
+ });
+ }
+
+ client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src"))
+ .then((HttpClientRequest request) {
+ request.followRedirects = false;
+ request.headers.add("X-Request-Header", "value");
+ return request.close();
+ }).then(handleResponse);
+ });
}
void testAutoRedirect() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- var requestCount = 0;
-
- void onRequest(HttpClientRequest request) {
- requestCount++;
- request.outputStream.close();
- }
-
- void onResponse(HttpClientResponse response) {
- response.inputStream.onData =
- () => Expect.fail("Response data not expected");
- response.inputStream.onClosed = () {
- Expect.equals(1, requestCount);
- server.close();
- client.shutdown();
- };
- };
-
- HttpClientConnection conn =
- client.getUrl(
- Uri.parse("http://127.0.0.1:${server.port}/redirect"));
- conn.onRequest = onRequest;
- conn.onResponse = onResponse;
- conn.onError = (e) => Expect.fail("Error not expected ($e)");
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/redirect"))
+ .then((HttpClientRequest request) {
+ return request.close();
+ })
+ .then((HttpClientResponse response) {
+ response.listen(
+ (_) => Expect.fail("Response data not expected"),
+ onDone: () {
+ Expect.equals(1, response.redirects.length);
+ server.close();
+ client.close();
+ });
+ });
+ });
}
void testAutoRedirectWithHeaders() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- var requestCount = 0;
-
- void onRequest(HttpClientRequest request) {
- requestCount++;
- request.headers.add("X-Request-Header", "value");
- request.outputStream.close();
- };
-
- void onResponse(HttpClientResponse response) {
- response.inputStream.onData =
- () => Expect.fail("Response data not expected");
- response.inputStream.onClosed = () {
- Expect.equals(1, requestCount);
- server.close();
- client.shutdown();
- };
- };
-
- HttpClientConnection conn =
- client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src"));
- conn.onRequest = onRequest;
- conn.onResponse = onResponse;
- conn.onError = (e) => Expect.fail("Error not expected ($e)");
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src"))
+ .then((HttpClientRequest request) {
+ request.headers.add("X-Request-Header", "value");
+ return request.close();
+ })
+ .then((HttpClientResponse response) {
+ response.listen(
+ (_) => Expect.fail("Response data not expected"),
+ onDone: () {
+ Expect.equals(1, response.redirects.length);
+ server.close();
+ client.close();
+ });
+ });
+ });
}
void testAutoRedirect301POST() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- var requestCount = 0;
-
- void onRequest(HttpClientRequest request) {
- requestCount++;
- request.outputStream.close();
- };
-
- void onResponse(HttpClientResponse response) {
- Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode);
- response.inputStream.onData =
- () => Expect.fail("Response data not expected");
- response.inputStream.onClosed = () {
- Expect.equals(1, requestCount);
- server.close();
- client.shutdown();
- };
- };
-
- HttpClientConnection conn =
- client.postUrl(
- Uri.parse("http://127.0.0.1:${server.port}/301src"));
- conn.onRequest = onRequest;
- conn.onResponse = onResponse;
- conn.onError = (e) => Expect.fail("Error not expected ($e)");
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ client.postUrl(Uri.parse("http://127.0.0.1:${server.port}/301src"))
+ .then((HttpClientRequest request) {
+ return request.close();
+ })
+ .then((HttpClientResponse response) {
+ Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode);
+ response.listen(
+ (_) => Expect.fail("Response data not expected"),
+ onDone: () {
+ Expect.equals(0, response.redirects.length);
+ server.close();
+ client.close();
+ });
+ });
+ });
}
void testAutoRedirect303POST() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- var requestCount = 0;
-
- void onRequest(HttpClientRequest request) {
- requestCount++;
- request.contentLength = 10;
- request.outputStream.write(new List<int>.fixedLength(10, fill: 0));
- request.outputStream.close();
- };
-
- void onResponse(HttpClientResponse response) {
- Expect.equals(HttpStatus.OK, response.statusCode);
- response.inputStream.onData =
- () => Expect.fail("Response data not expected");
- response.inputStream.onClosed = () {
- Expect.equals(1, requestCount);
- server.close();
- client.shutdown();
- };
- };
-
- HttpClientConnection conn =
- client.postUrl(
- Uri.parse("http://127.0.0.1:${server.port}/303src"));
- conn.onRequest = onRequest;
- conn.onResponse = onResponse;
- conn.onError = (e) => Expect.fail("Error not expected ($e)");
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ client.postUrl(Uri.parse("http://127.0.0.1:${server.port}/303src"))
+ .then((HttpClientRequest request) {
+ return request.close();
+ })
+ .then((HttpClientResponse response) {
+ Expect.equals(HttpStatus.OK, response.statusCode);
+ response.listen(
+ (_) => Expect.fail("Response data not expected"),
+ onDone: () {
+ Expect.equals(1, response.redirects.length);
+ server.close();
+ client.close();
+ });
+ });
+ });
}
void testAutoRedirectLimit() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- HttpClientConnection conn =
- client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1"));
- conn.onResponse = (HttpClientResponse response) {
- response.inputStream.onData = () => Expect.fail("Response not expected");
- response.inputStream.onClosed = () => Expect.fail("Response not expected");
- };
- conn.onError = (e) {
- Expect.isTrue(e is RedirectLimitExceededException);
- Expect.equals(5, e.redirects.length);
- server.close();
- client.shutdown();
- };
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1"))
+ .then((HttpClientRequest request) => request.close())
+ .catchError((e) {
+ Expect.equals(5, e.error.redirects.length);
+ server.close();
+ client.close();
+ }, test: (e) => e is RedirectLimitExceededException);
+ });
}
void testRedirectLoop() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- int redirectCount = 0;
- HttpClientConnection conn =
- client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/A"));
- conn.onResponse = (HttpClientResponse response) {
- response.inputStream.onData = () => Expect.fail("Response not expected");
- response.inputStream.onClosed = () => Expect.fail("Response not expected");
- };
- conn.onError = (e) {
- Expect.isTrue(e is RedirectLoopException);
- Expect.equals(2, e.redirects.length);
- server.close();
- client.shutdown();
- };
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ int redirectCount = 0;
+ client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/A"))
+ .then((HttpClientRequest request) => request.close())
+ .catchError((e) {
+ Expect.equals(2, e.error.redirects.length);
+ server.close();
+ client.close();
+ }, test: (e) => e is RedirectLoopException);
+ });
}
void testRedirectClosingConnection() {
- HttpServer server = setupServer();
- HttpClient client = new HttpClient();
-
- int redirectCount = 0;
- HttpClientConnection conn =
- client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/closing"));
-
- conn.followRedirects = true;
- conn.onResponse = (HttpClientResponse response) {
- response.inputStream.onData = () => Expect.fail("Response not expected");
- response.inputStream.onClosed = () => Expect.fail("Response not expected");
- };
- conn.onError = (e) {
- Expect.isTrue(e is RedirectException);
- Expect.isNull(e.redirects);
- server.close();
- client.shutdown();
- };
+ setupServer().then((server) {
+ HttpClient client = new HttpClient();
+
+ client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/closing"))
+ .then((request) => request.close())
+ .then((response) {
+ response.listen(
+ (_) {},
+ onDone: () {
+ Expect.equals(1, response.redirects.length);
+ server.close();
+ client.close();
+ });
+ });
+ });
}
main() {
« no previous file with comments | « tests/standalone/io/http_read_test.dart ('k') | tests/standalone/io/http_server_early_client_close_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698