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

Unified Diff: tools/testing/dart/http_server.dart

Issue 11035027: Add in-process http server to the dart test scripts. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address more comments. Created 8 years, 2 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: tools/testing/dart/http_server.dart
diff --git a/tools/testing/dart/http_server.dart b/tools/testing/dart/http_server.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d70da115d58b060005f0969286e37414f6246b44
--- /dev/null
+++ b/tools/testing/dart/http_server.dart
@@ -0,0 +1,48 @@
+// Copyright (c) 2012, 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.
+
+#library('http_server');
+
+#import('dart:io');
+#import('test_suite.dart'); // For TestUtils.
+
+HttpServer _httpServer;
+
+startHttpServer(String host, int port) {
+ var basePath = TestUtils.dartDir();
+ _httpServer = new HttpServer();
+ _httpServer.onError = (e) {
+ // Consider errors in the builtin http server fatal.
+ // Intead of just throwing the exception we print
+ // a message that makes it clearer what happened.
+ print('Test http server error: $e');
+ exit(1);
+ };
+ _httpServer.defaultRequestHandler = (request, resp) {
+ var requestPath = new Path(request.path).canonicalize();
+ if (!requestPath.isAbsolute) {
+ resp.statusCode = HttpStatus.NOT_FOUND;
+ resp.outputStream.close();
+ } else {
+ var path = basePath;
+ requestPath.segments().forEach((s) => path = path.append(s));
+ var file = new File(path.toNativePath());
+ file.exists().then((exists) {
+ if (exists) {
+ // Allow loading from localhost in browsers.
+ resp.headers.set("Access-Control-Allow-Origin", "*");
+ file.openInputStream().pipe(resp.outputStream);
+ } else {
+ resp.statusCode = HttpStatus.NOT_FOUND;
+ resp.outputStream.close();
+ }
+ });
+ }
+ };
+ _httpServer.listen(host, port);
+}
+
+terminateHttpServer() {
+ _httpServer.close();
+}

Powered by Google App Engine
This is Rietveld 408576698