| 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 library utils; | 5 library utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import "package:unittest/unittest.dart"; |
| 11 |
| 12 import 'package:http_server/http_server.dart'; |
| 13 |
| 14 void testVirtualDir(String name, Future func(Directory dir)) { |
| 15 test(name, () { |
| 16 var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); |
| 17 |
| 18 return func(dir) |
| 19 .whenComplete(() { |
| 20 return dir.delete(recursive: true); |
| 21 }); |
| 22 }); |
| 23 } |
| 24 |
| 25 Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir, |
| 26 String path, |
| 27 {String host, |
| 28 bool secure: false, |
| 29 DateTime ifModifiedSince, |
| 30 bool rawPath: false}) { |
| 31 return _withServer((server) { |
| 32 |
| 33 virtualDir.serve(server); |
| 34 return getStatusCode(server.port, path, host: host, secure: secure, |
| 35 ifModifiedSince: ifModifiedSince, rawPath: rawPath); |
| 36 }); |
| 37 } |
| 10 | 38 |
| 11 Future<int> getStatusCode(int port, | 39 Future<int> getStatusCode(int port, |
| 12 String path, | 40 String path, |
| 13 {String host, | 41 {String host, |
| 14 bool secure: false, | 42 bool secure: false, |
| 15 DateTime ifModifiedSince, | 43 DateTime ifModifiedSince, |
| 16 bool rawPath: false}) { | 44 bool rawPath: false}) { |
| 17 Uri uri; | 45 Uri uri; |
| 18 if (rawPath) { | 46 if (rawPath) { |
| 19 uri = new Uri(scheme: secure ? 'https' : 'http', | 47 uri = new Uri(scheme: secure ? 'https' : 'http', |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 if (host != null) request.headers.host = host; | 59 if (host != null) request.headers.host = host; |
| 32 if (ifModifiedSince != null) { | 60 if (ifModifiedSince != null) { |
| 33 request.headers.ifModifiedSince = ifModifiedSince; | 61 request.headers.ifModifiedSince = ifModifiedSince; |
| 34 } | 62 } |
| 35 return request.close(); | 63 return request.close(); |
| 36 }) | 64 }) |
| 37 .then((response) => response.drain().then( | 65 .then((response) => response.drain().then( |
| 38 (_) => response.statusCode)); | 66 (_) => response.statusCode)); |
| 39 } | 67 } |
| 40 | 68 |
| 69 Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) { |
| 70 return _withServer((server) { |
| 71 virDir.serve(server); |
| 41 | 72 |
| 42 Future<HttpHeaders> getHeaders(int port, String path) => | 73 return new HttpClient() |
| 43 new HttpClient() | 74 .get('localhost', server.port, path) |
| 44 .get('localhost', port, path) | 75 .then((request) => request.close()) |
| 45 .then((request) => request.close()) | 76 .then((response) => response.drain().then((_) => response.headers)); |
| 46 .then((response) => response.drain().then( | 77 }); |
| 47 (_) => response.headers)); | 78 } |
| 48 | 79 |
| 80 Future<String> getAsString(VirtualDirectory virtualDir, String path) { |
| 81 return _withServer((server) { |
| 82 virtualDir.serve(server); |
| 49 | 83 |
| 84 return new HttpClient() |
| 85 .get('localhost', server.port, path) |
| 86 .then((request) => request.close()) |
| 87 .then((response) => UTF8.decodeStream(response)); |
| 88 }); |
| 89 } |
| 50 | 90 |
| 51 Future<String> getAsString(int port, String path) => | 91 Future _withServer(Future func(HttpServer server)) { |
| 52 new HttpClient() | 92 HttpServer server; |
| 53 .get('localhost', port, path) | 93 return HttpServer.bind('localhost', 0) |
| 54 .then((request) => request.close()) | 94 .then((value) { |
| 55 .then((response) => UTF8.decodeStream(response)); | 95 server = value; |
| 96 return func(server); |
| 97 }) |
| 98 .whenComplete(server.close); |
| 99 } |
| 56 | 100 |
| 57 | 101 |
| 58 const CERTIFICATE = "localhost_cert"; | 102 const CERTIFICATE = "localhost_cert"; |
| 59 | 103 |
| 60 | 104 |
| 61 void setupSecure() { | 105 void setupSecure() { |
| 62 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | 106 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 63 SecureSocket.initialize(database: certificateDatabase, | 107 SecureSocket.initialize(database: certificateDatabase, |
| 64 password: 'dartdart'); | 108 password: 'dartdart'); |
| 65 } | 109 } |
| OLD | NEW |