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

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

Issue 11641005: Add cross-origin test with credentials. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 12 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
index 8d233a83a0000ed525aa73635bb8b3b26d4d3194..089b228f06a0aea04ed2e6055afdfcb35273d7d3 100644
--- a/tools/testing/dart/http_server.dart
+++ b/tools/testing/dart/http_server.dart
@@ -8,19 +8,17 @@ import 'dart:io';
import 'dart:isolate';
import 'test_suite.dart'; // For TestUtils.
-HttpServer _httpServer;
-
-void startHttpServer(String host, int port) {
+HttpServer startHttpServer(String host, [int allowedPort=-1]) {
Mads Ager (google) 2013/01/04 09:39:20 Could you add spaces: [int allowedPort = -1]?
Emily Fortuna 2013/01/04 23:26:54 Done.
var basePath = TestUtils.dartDir();
- _httpServer = new HttpServer();
- _httpServer.onError = (e) {
+ var 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) {
+ httpServer.defaultRequestHandler = (request, resp) {
var requestPath = new Path(request.path).canonicalize();
if (!requestPath.isAbsolute) {
resp.statusCode = HttpStatus.NOT_FOUND;
@@ -31,8 +29,16 @@ void startHttpServer(String host, int port) {
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", "*");
+ if (allowedPort != -1) {
+ // Allow loading from localhost:$allowedPort in browsers.
+ resp.headers.set("Access-Control-Allow-Origin",
+ "http://127.0.0.1:$allowedPort");
+ resp.headers.set('Access-Control-Allow-Credentials', 'true');
+ } else {
+ // No allowedPort specified. Allow from anywhere (but cross-origin
+ // requests *with credentials* will fail because you can't use "*").
+ resp.headers.set("Access-Control-Allow-Origin", "*");
+ }
file.openInputStream().pipe(resp.outputStream);
} else {
resp.statusCode = HttpStatus.NOT_FOUND;
@@ -43,15 +49,16 @@ void startHttpServer(String host, int port) {
};
// Echos back the contents of the request as the response data.
- _httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) {
+ httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) {
resp.headers.set("Access-Control-Allow-Origin", "*");
request.inputStream.pipe(resp.outputStream);
});
- _httpServer.listen(host, port);
+ httpServer.listen(host, 0);
+ return httpServer;
}
-terminateHttpServer() {
- if (_httpServer != null) _httpServer.close();
+terminateHttpServers([List<HttpServer> servers = const []]) {
Mads Ager (google) 2013/01/04 09:39:20 Can we make the servers argument mandatory?
Emily Fortuna 2013/01/04 23:26:54 Done.
+ for (var server in servers) server.close();
}

Powered by Google App Engine
This is Rietveld 408576698