Chromium Code Reviews| 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, | |
|
Anders Johnsen
2014/01/07 07:42:10
Indentation
kevmoo
2014/01/07 19:36:52
Done.
| |
| 27 {String host, | |
| 28 bool secure: false, | |
| 29 DateTime ifModifiedSince, | |
| 30 bool rawPath: false}) => | |
| 31 _withServer((server) { | |
| 32 | |
| 33 virtualDir.serve(server); | |
| 34 return getStatusCode(server.port, path, host: host, secure: secure, | |
| 35 ifModifiedSince: ifModifiedSince, rawPath: rawPath); | |
| 36 }); | |
| 10 | 37 |
| 11 Future<int> getStatusCode(int port, | 38 Future<int> getStatusCode(int port, |
| 12 String path, | 39 String path, |
| 13 {String host, | 40 {String host, |
| 14 bool secure: false, | 41 bool secure: false, |
| 15 DateTime ifModifiedSince, | 42 DateTime ifModifiedSince, |
| 16 bool rawPath: false}) { | 43 bool rawPath: false}) { |
| 17 Uri uri; | 44 Uri uri; |
| 18 if (rawPath) { | 45 if (rawPath) { |
| 19 uri = new Uri(scheme: secure ? 'https' : 'http', | 46 uri = new Uri(scheme: secure ? 'https' : 'http', |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 31 if (host != null) request.headers.host = host; | 58 if (host != null) request.headers.host = host; |
| 32 if (ifModifiedSince != null) { | 59 if (ifModifiedSince != null) { |
| 33 request.headers.ifModifiedSince = ifModifiedSince; | 60 request.headers.ifModifiedSince = ifModifiedSince; |
| 34 } | 61 } |
| 35 return request.close(); | 62 return request.close(); |
| 36 }) | 63 }) |
| 37 .then((response) => response.drain().then( | 64 .then((response) => response.drain().then( |
| 38 (_) => response.statusCode)); | 65 (_) => response.statusCode)); |
| 39 } | 66 } |
| 40 | 67 |
| 68 Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) => | |
|
Anders Johnsen
2014/01/07 07:42:10
Please use { ... } for multiline body.
kevmoo
2014/01/07 19:36:52
Done.
| |
| 69 _withServer((server) { | |
| 70 virDir.serve(server); | |
| 41 | 71 |
| 42 Future<HttpHeaders> getHeaders(int port, String path) => | 72 return new HttpClient() |
| 43 new HttpClient() | 73 .get('localhost', server.port, path) |
| 44 .get('localhost', port, path) | 74 .then((request) => request.close()) |
| 45 .then((request) => request.close()) | 75 .then((response) => response.drain().then((_) => response.headers)); |
| 46 .then((response) => response.drain().then( | 76 }); |
| 47 (_) => response.headers)); | |
| 48 | 77 |
| 78 Future<String> getAsString(VirtualDirectory virtualDir, String path) => | |
| 79 _withServer((server) { | |
| 80 virtualDir.serve(server); | |
| 49 | 81 |
| 82 return new HttpClient() | |
| 83 .get('localhost', server.port, path) | |
| 84 .then((request) => request.close()) | |
| 85 .then((response) => UTF8.decodeStream(response)); | |
| 86 }); | |
| 50 | 87 |
| 51 Future<String> getAsString(int port, String path) => | 88 Future _withServer(Future func(HttpServer server)) { |
|
Anders Johnsen
2014/01/07 07:42:10
Consider:
{
return HttpServer.bind('localhost',
kevmoo
2014/01/07 19:36:52
I'll argue against this.
If func throws an error
| |
| 52 new HttpClient() | 89 HttpServer server; |
| 53 .get('localhost', port, path) | 90 return HttpServer.bind('localhost', 0) |
| 54 .then((request) => request.close()) | 91 .then((value) { |
| 55 .then((response) => UTF8.decodeStream(response)); | 92 server = value; |
| 93 return func(server); | |
| 94 }) | |
| 95 .whenComplete(() => server.close()); | |
| 96 } | |
| 56 | 97 |
| 57 | 98 |
| 58 const CERTIFICATE = "localhost_cert"; | 99 const CERTIFICATE = "localhost_cert"; |
| 59 | 100 |
| 60 | 101 |
| 61 void setupSecure() { | 102 void setupSecure() { |
| 62 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | 103 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 63 SecureSocket.initialize(database: certificateDatabase, | 104 SecureSocket.initialize(database: certificateDatabase, |
| 64 password: 'dartdart'); | 105 password: 'dartdart'); |
| 65 } | 106 } |
| OLD | NEW |