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

Unified Diff: tests/standalone/io/http_advanced_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
« no previous file with comments | « tests/standalone/io/file_test.dart ('k') | tests/standalone/io/http_auth_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/standalone/io/http_advanced_test.dart
diff --git a/tests/standalone/io/http_advanced_test.dart b/tests/standalone/io/http_advanced_test.dart
index d6e1ad416fc68ebcf2d3e81d10685e5d8f45c310..e19365f66a892fb3c4a7e1f6b2152da9357ad310 100644
--- a/tests/standalone/io/http_advanced_test.dart
+++ b/tests/standalone/io/http_advanced_test.dart
@@ -1,4 +1,4 @@
-// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
@@ -11,11 +11,11 @@ import 'dart:async';
import 'dart:io';
import 'dart:isolate';
-class TestServerMain {
- TestServerMain()
+class IsolatedHttpServer {
+ IsolatedHttpServer()
: _statusPort = new ReceivePort(),
_serverPort = null {
- _serverPort = spawnFunction(startTestServer);
+ _serverPort = spawnFunction(startIsolatedHttpServer);
}
void setServerStartedHandler(void startedCallback(int port)) {
@@ -31,20 +31,22 @@ class TestServerMain {
});
// Send server start message to the server.
- var command = new TestServerCommand.start();
+ var command = new IsolatedHttpServerCommand.start();
_serverPort.send(command, _statusPort.toSendPort());
}
void shutdown() {
// Send server stop message to the server.
- _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort());
+ _serverPort.send(new IsolatedHttpServerCommand.stop(),
+ _statusPort.toSendPort());
_statusPort.close();
}
void chunkedEncoding() {
// Send chunked encoding message to the server.
_serverPort.send(
- new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort());
+ new IsolatedHttpServerCommand.chunkedEncoding(),
+ _statusPort.toSendPort());
}
ReceivePort _statusPort; // Port for receiving messages from the server.
@@ -53,14 +55,14 @@ class TestServerMain {
}
-class TestServerCommand {
+class IsolatedHttpServerCommand {
static const START = 0;
static const STOP = 1;
static const CHUNKED_ENCODING = 2;
- TestServerCommand.start() : _command = START;
- TestServerCommand.stop() : _command = STOP;
- TestServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING;
+ IsolatedHttpServerCommand.start() : _command = START;
+ IsolatedHttpServerCommand.stop() : _command = STOP;
+ IsolatedHttpServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING;
bool get isStart => _command == START;
bool get isStop => _command == STOP;
@@ -70,14 +72,14 @@ class TestServerCommand {
}
-class TestServerStatus {
+class IsolatedHttpServerStatus {
static const STARTED = 0;
static const STOPPED = 1;
static const ERROR = 2;
- TestServerStatus.started(this._port) : _state = STARTED;
- TestServerStatus.stopped() : _state = STOPPED;
- TestServerStatus.error() : _state = ERROR;
+ IsolatedHttpServerStatus.started(this._port) : _state = STARTED;
+ IsolatedHttpServerStatus.stopped() : _state = STOPPED;
+ IsolatedHttpServerStatus.error() : _state = ERROR;
bool get isStarted => _state == STARTED;
bool get isStopped => _state == STOPPED;
@@ -90,7 +92,7 @@ class TestServerStatus {
}
-void startTestServer() {
+void startIsolatedHttpServer() {
var server = new TestServer();
server.init();
port.receive(server.dispatch);
@@ -99,40 +101,45 @@ void startTestServer() {
class TestServer {
// Return a 404.
- void _notFoundHandler(HttpRequest request, HttpResponse response) {
+ void _notFoundHandler(HttpRequest request) {
+ var response = request.response;
response.statusCode = HttpStatus.NOT_FOUND;
response.headers.set("Content-Type", "text/html; charset=UTF-8");
response.outputStream.writeString("Page not found");
- response.outputStream.close();
+ response.close();
}
// Check the "Host" header.
- void _hostHandler(HttpRequest request, HttpResponse response) {
+ void _hostHandler(HttpRequest request) {
+ var response = request.response;
Expect.equals(1, request.headers["Host"].length);
Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]);
Expect.equals("www.dartlang.org", request.headers.host);
Expect.equals(1234, request.headers.port);
response.statusCode = HttpStatus.OK;
- response.outputStream.close();
+ response.close();
}
// Set the "Expires" header using the expires property.
- void _expires1Handler(HttpRequest request, HttpResponse response) {
+ void _expires1Handler(HttpRequest request) {
+ var response = request.response;
DateTime date = new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0);
response.headers.expires = date;
Expect.equals(date, response.headers.expires);
- response.outputStream.close();
+ response.close();
}
// Set the "Expires" header.
- void _expires2Handler(HttpRequest request, HttpResponse response) {
+ void _expires2Handler(HttpRequest request) {
+ var response = request.response;
response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT");
DateTime date = new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0);
Expect.equals(date, response.headers.expires);
- response.outputStream.close();
+ response.close();
}
- void _contentType1Handler(HttpRequest request, HttpResponse response) {
+ void _contentType1Handler(HttpRequest request) {
+ var response = request.response;
Expect.equals("text/html", request.headers.contentType.value);
Expect.equals("text", request.headers.contentType.primaryType);
Expect.equals("html", request.headers.contentType.subType);
@@ -141,10 +148,11 @@ class TestServer {
ContentType contentType = new ContentType("text", "html");
contentType.parameters["charset"] = "utf-8";
response.headers.contentType = contentType;
- response.outputStream.close();
+ response.close();
}
- void _contentType2Handler(HttpRequest request, HttpResponse response) {
+ void _contentType2Handler(HttpRequest request) {
+ var response = request.response;
Expect.equals("text/html", request.headers.contentType.value);
Expect.equals("text", request.headers.contentType.primaryType);
Expect.equals("html", request.headers.contentType.subType);
@@ -152,10 +160,12 @@ class TestServer {
response.headers.set(HttpHeaders.CONTENT_TYPE,
"text/html; charset = utf-8");
- response.outputStream.close();
+ response.close();
}
- void _cookie1Handler(HttpRequest request, HttpResponse response) {
+ void _cookie1Handler(HttpRequest request) {
+ var response = request.response;
+
// No cookies passed with this request.
Expect.equals(0, request.cookies.length);
@@ -170,84 +180,56 @@ class TestServer {
cookie2.domain = ".example.com";
cookie2.path = "/shop";
response.cookies.add(cookie2);
- response.outputStream.close();
+ response.close();
}
- void _cookie2Handler(HttpRequest request, HttpResponse response) {
+ void _cookie2Handler(HttpRequest request) {
+ var response = request.response;
+
// Two cookies passed with this request.
Expect.equals(2, request.cookies.length);
- response.outputStream.close();
- }
-
- void _flushHandler(HttpRequest request, HttpResponse response) {
- response.outputStream.flush();
- response.outputStream.close();
+ response.close();
}
void init() {
// Setup request handlers.
_requestHandlers = new Map();
- _requestHandlers["/host"] =
- (HttpRequest request, HttpResponse response) {
- _hostHandler(request, response);
- };
- _requestHandlers["/expires1"] =
- (HttpRequest request, HttpResponse response) {
- _expires1Handler(request, response);
- };
- _requestHandlers["/expires2"] =
- (HttpRequest request, HttpResponse response) {
- _expires2Handler(request, response);
- };
- _requestHandlers["/contenttype1"] =
- (HttpRequest request, HttpResponse response) {
- _contentType1Handler(request, response);
- };
- _requestHandlers["/contenttype2"] =
- (HttpRequest request, HttpResponse response) {
- _contentType2Handler(request, response);
- };
- _requestHandlers["/cookie1"] =
- (HttpRequest request, HttpResponse response) {
- _cookie1Handler(request, response);
- };
- _requestHandlers["/cookie2"] =
- (HttpRequest request, HttpResponse response) {
- _cookie2Handler(request, response);
- };
- _requestHandlers["/flush"] =
- (HttpRequest request, HttpResponse response) {
- _flushHandler(request, response);
- };
+ _requestHandlers["/host"] = _hostHandler;
+ _requestHandlers["/expires1"] = _expires1Handler;
+ _requestHandlers["/expires2"] = _expires2Handler;
+ _requestHandlers["/contenttype1"] = _contentType1Handler;
+ _requestHandlers["/contenttype2"] = _contentType2Handler;
+ _requestHandlers["/cookie1"] = _cookie1Handler;
+ _requestHandlers["/cookie2"] = _cookie2Handler;
}
void dispatch(message, replyTo) {
if (message.isStart) {
- _server = new HttpServer();
try {
- _server.listen("127.0.0.1", 0);
- _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) {
- _requestReceivedHandler(req, rsp);
- };
- replyTo.send(new TestServerStatus.started(_server.port), null);
+ HttpServer.bind().then((server) {
+ _server = server;
+ _server.listen(_requestReceivedHandler);
+ replyTo.send(new IsolatedHttpServerStatus.started(_server.port),
+ null);
+ });
} catch (e) {
- replyTo.send(new TestServerStatus.error(), null);
+ replyTo.send(new IsolatedHttpServerStatus.error(), null);
}
} else if (message.isStop) {
_server.close();
port.close();
- replyTo.send(new TestServerStatus.stopped(), null);
+ replyTo.send(new IsolatedHttpServerStatus.stopped(), null);
} else if (message.isChunkedEncoding) {
_chunkedEncoding = true;
}
}
- void _requestReceivedHandler(HttpRequest request, HttpResponse response) {
- var requestHandler =_requestHandlers[request.path];
+ void _requestReceivedHandler(HttpRequest request) {
+ var requestHandler =_requestHandlers[request.uri.path];
if (requestHandler != null) {
- requestHandler(request, response);
+ requestHandler(request);
} else {
- _notFoundHandler(request, response);
+ _notFoundHandler(request);
}
}
@@ -258,52 +240,52 @@ class TestServer {
Future testHost() {
Completer completer = new Completer();
- TestServerMain testServerMain = new TestServerMain();
- testServerMain.setServerStartedHandler((int port) {
+ IsolatedHttpServer server = new IsolatedHttpServer();
+ server.setServerStartedHandler((int port) {
HttpClient httpClient = new HttpClient();
- HttpClientConnection conn =
- httpClient.get("127.0.0.1", port, "/host");
- conn.onRequest = (HttpClientRequest request) {
- Expect.equals("127.0.0.1:$port", request.headers["host"][0]);
- request.headers.host = "www.dartlang.com";
- Expect.equals("www.dartlang.com:$port", request.headers["host"][0]);
- Expect.equals("www.dartlang.com", request.headers.host);
- Expect.equals(port, request.headers.port);
- request.headers.port = 1234;
- Expect.equals("www.dartlang.com:1234", request.headers["host"][0]);
- Expect.equals(1234, request.headers.port);
- request.headers.port = HttpClient.DEFAULT_HTTP_PORT;
- Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port);
- Expect.equals("www.dartlang.com", request.headers["host"][0]);
- request.headers.set("Host", "www.dartlang.org");
- Expect.equals("www.dartlang.org", request.headers.host);
- Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port);
- request.headers.set("Host", "www.dartlang.org:");
- Expect.equals("www.dartlang.org", request.headers.host);
- Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port);
- request.headers.set("Host", "www.dartlang.org:1234");
- Expect.equals("www.dartlang.org", request.headers.host);
- Expect.equals(1234, request.headers.port);
- request.outputStream.close();
- };
- conn.onResponse = (HttpClientResponse response) {
- Expect.equals(HttpStatus.OK, response.statusCode);
- response.inputStream.onData = response.inputStream.read;
- response.inputStream.onClosed = () {
- httpClient.shutdown();
- testServerMain.shutdown();
- completer.complete(true);
- };
- };
+ httpClient.get("127.0.0.1", port, "/host")
+ .then((request) {
+ Expect.equals("127.0.0.1:$port", request.headers["host"][0]);
+ request.headers.host = "www.dartlang.com";
+ Expect.equals("www.dartlang.com:$port", request.headers["host"][0]);
+ Expect.equals("www.dartlang.com", request.headers.host);
+ Expect.equals(port, request.headers.port);
+ request.headers.port = 1234;
+ Expect.equals("www.dartlang.com:1234", request.headers["host"][0]);
+ Expect.equals(1234, request.headers.port);
+ request.headers.port = HttpClient.DEFAULT_HTTP_PORT;
+ Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port);
+ Expect.equals("www.dartlang.com", request.headers["host"][0]);
+ request.headers.set("Host", "www.dartlang.org");
+ Expect.equals("www.dartlang.org", request.headers.host);
+ Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port);
+ request.headers.set("Host", "www.dartlang.org:");
+ Expect.equals("www.dartlang.org", request.headers.host);
+ Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port);
+ request.headers.set("Host", "www.dartlang.org:1234");
+ Expect.equals("www.dartlang.org", request.headers.host);
+ Expect.equals(1234, request.headers.port);
+ return request.close();
+ })
+ .then((response) {
+ Expect.equals(HttpStatus.OK, response.statusCode);
+ response.listen(
+ (_) { },
+ onDone: () {
+ httpClient.close();
+ server.shutdown();
+ completer.complete(true);
+ });
+ });
});
- testServerMain.start();
+ server.start();
return completer.future;
}
Future testExpires() {
Completer completer = new Completer();
- TestServerMain testServerMain = new TestServerMain();
- testServerMain.setServerStartedHandler((int port) {
+ IsolatedHttpServer server = new IsolatedHttpServer();
+ server.setServerStartedHandler((int port) {
int responses = 0;
HttpClient httpClient = new HttpClient();
@@ -313,34 +295,32 @@ Future testExpires() {
response.headers["expires"][0]);
Expect.equals(new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0),
response.headers.expires);
- response.inputStream.onData = response.inputStream.read;
- response.inputStream.onClosed = () {
- responses++;
- if (responses == 2) {
- httpClient.shutdown();
- testServerMain.shutdown();
- completer.complete(true);
- }
- };
+ response.listen((_) { },
+ onDone: () {
+ responses++;
+ if (responses == 2) {
+ httpClient.close();
+ server.shutdown();
+ completer.complete(true);
+ }
+ });
}
- HttpClientConnection conn1 = httpClient.get("127.0.0.1", port, "/expires1");
- conn1.onResponse = (HttpClientResponse response) {
- processResponse(response);
- };
- HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2");
- conn2.onResponse = (HttpClientResponse response) {
- processResponse(response);
- };
+ httpClient.get("127.0.0.1", port, "/expires1")
+ .then((request) => request.close())
+ .then(processResponse);
+ httpClient.get("127.0.0.1", port, "/expires2")
+ .then((request) => request.close())
+ .then(processResponse);
});
- testServerMain.start();
+ server.start();
return completer.future;
}
Future testContentType() {
Completer completer = new Completer();
- TestServerMain testServerMain = new TestServerMain();
- testServerMain.setServerStartedHandler((int port) {
+ IsolatedHttpServer server = new IsolatedHttpServer();
+ server.setServerStartedHandler((int port) {
int responses = 0;
HttpClient httpClient = new HttpClient();
@@ -353,132 +333,100 @@ Future testContentType() {
Expect.equals("html", response.headers.contentType.subType);
Expect.equals("utf-8",
response.headers.contentType.parameters["charset"]);
- response.inputStream.onData = response.inputStream.read;
- response.inputStream.onClosed = () {
- responses++;
- if (responses == 2) {
- httpClient.shutdown();
- testServerMain.shutdown();
- completer.complete(true);
- }
- };
+ response.listen(
+ (_) { },
+ onDone: () {
+ responses++;
+ if (responses == 2) {
+ httpClient.close();
+ server.shutdown();
+ completer.complete(true);
+ }
+ });
}
- HttpClientConnection conn1 =
- httpClient.get("127.0.0.1", port, "/contenttype1");
- conn1.onRequest = (HttpClientRequest request) {
- ContentType contentType = new ContentType();
- contentType.value = "text/html";
- contentType.parameters["charset"] = "utf-8";
- request.headers.contentType = contentType;
- request.outputStream.close();
- };
- conn1.onResponse = (HttpClientResponse response) {
- processResponse(response);
- };
- HttpClientConnection conn2 =
- httpClient.get("127.0.0.1", port, "/contenttype2");
- conn2.onRequest = (HttpClientRequest request) {
- request.headers.set(HttpHeaders.CONTENT_TYPE,
- "text/html; charset = utf-8");
- request.outputStream.close();
- };
- conn2.onResponse = (HttpClientResponse response) {
- processResponse(response);
- };
+ httpClient.get("127.0.0.1", port, "/contenttype1")
+ .then((request) {
+ ContentType contentType = new ContentType();
+ contentType.value = "text/html";
+ contentType.parameters["charset"] = "utf-8";
+ request.headers.contentType = contentType;
+ return request.close();
+ })
+ .then(processResponse);
+
+ httpClient.get("127.0.0.1", port, "/contenttype2")
+ .then((request) {
+ request.headers.set(HttpHeaders.CONTENT_TYPE,
+ "text/html; charset = utf-8");
+ return request.close();
+ })
+ .then(processResponse);
});
- testServerMain.start();
+ server.start();
return completer.future;
}
Future testCookies() {
Completer completer = new Completer();
- TestServerMain testServerMain = new TestServerMain();
- testServerMain.setServerStartedHandler((int port) {
+ IsolatedHttpServer server = new IsolatedHttpServer();
+ server.setServerStartedHandler((int port) {
int responses = 0;
HttpClient httpClient = new HttpClient();
- HttpClientConnection conn1 =
- httpClient.get("127.0.0.1", port, "/cookie1");
- conn1.onResponse = (HttpClientResponse response) {
- Expect.equals(2, response.cookies.length);
- response.cookies.forEach((cookie) {
- if (cookie.name == "name1") {
- Expect.equals("value1", cookie.value);
- DateTime date = new DateTime.utc(2014, DateTime.JAN, 5, 23, 59, 59, 0);
- Expect.equals(date, cookie.expires);
- Expect.equals("www.example.com", cookie.domain);
- Expect.isTrue(cookie.httpOnly);
- } else if (cookie.name == "name2") {
- Expect.equals("value2", cookie.value);
- Expect.equals(100, cookie.maxAge);
- Expect.equals(".example.com", cookie.domain);
- Expect.equals("/shop", cookie.path);
- } else {
- Expect.fail("Unexpected cookie");
- }
- });
- HttpClientConnection conn2 =
- httpClient.get("127.0.0.1", port, "/cookie2");
- conn2.onRequest = (HttpClientRequest request) {
- request.cookies.add(response.cookies[0]);
- request.cookies.add(response.cookies[1]);
- request.outputStream.close();
- };
- conn2.onResponse = (HttpClientResponse response) {
- response.inputStream.onData = response.inputStream.read;
- response.inputStream.onClosed = () {
- httpClient.shutdown();
- testServerMain.shutdown();
- completer.complete(true);
- };
- };
- };
- });
- testServerMain.start();
- return completer.future;
-}
-
-Future testFlush() {
- Completer completer = new Completer();
- TestServerMain testServerMain = new TestServerMain();
- testServerMain.setServerStartedHandler((int port) {
- HttpClient httpClient = new HttpClient();
-
- HttpClientConnection conn = httpClient.get("127.0.0.1", port, "/flush");
- conn.onRequest = (HttpClientRequest request) {
- request.outputStream.flush();
- request.outputStream.close();
- };
- conn.onResponse = (HttpClientResponse response) {
- Expect.equals(HttpStatus.OK, response.statusCode);
- response.inputStream.onData = response.inputStream.read;
- response.inputStream.onClosed = () {
- httpClient.shutdown();
- testServerMain.shutdown();
- completer.complete(true);
- };
- };
+ httpClient.get("127.0.0.1", port, "/cookie1")
+ .then((request) => request.close())
+ .then((response) {
+ Expect.equals(2, response.cookies.length);
+ response.cookies.forEach((cookie) {
+ if (cookie.name == "name1") {
+ Expect.equals("value1", cookie.value);
+ DateTime date =
+ new DateTime.utc(2014, DateTime.JAN, 5, 23, 59, 59, 0);
+ Expect.equals(date, cookie.expires);
+ Expect.equals("www.example.com", cookie.domain);
+ Expect.isTrue(cookie.httpOnly);
+ } else if (cookie.name == "name2") {
+ Expect.equals("value2", cookie.value);
+ Expect.equals(100, cookie.maxAge);
+ Expect.equals(".example.com", cookie.domain);
+ Expect.equals("/shop", cookie.path);
+ } else {
+ Expect.fail("Unexpected cookie");
+ }
+ });
+
+ response.listen(
+ (_) { },
+ onDone: () {
+ httpClient.get("127.0.0.1", port, "/cookie2")
+ .then((request) {
+ request.cookies.add(response.cookies[0]);
+ request.cookies.add(response.cookies[1]);
+ return request.close();
+ })
+ .then((response) {
+ response.listen(
+ (_) { },
+ onDone: () {
+ httpClient.close();
+ server.shutdown();
+ completer.complete(true);
+ });
+ });
+ });
+ });
});
- testServerMain.start();
+ server.start();
return completer.future;
}
void main() {
- print('testHost()');
testHost().then((_) {
- print('testExpires()');
return testExpires().then((_) {
- print('testContentType()');
return testContentType().then((_) {
- print('testCookies()');
- return testCookies().then((_) {
- print('testFlush()');
- return testFlush();
- });
+ return testCookies();
});
});
- }).then((_) {
- print('done');
});
}
« no previous file with comments | « tests/standalone/io/file_test.dart ('k') | tests/standalone/io/http_auth_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698