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"; | 10 import "package:unittest/unittest.dart"; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); | 35 var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); |
| 36 | 36 |
| 37 return func(dir) | 37 return func(dir) |
| 38 .whenComplete(() { | 38 .whenComplete(() { |
| 39 return dir.delete(recursive: true); | 39 return dir.delete(recursive: true); |
| 40 }); | 40 }); |
| 41 }); | 41 }); |
| 42 } | 42 } |
| 43 | 43 |
| 44 Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir, | 44 Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir, |
| 45 String path, | 45 String path, |
| 46 {String host, | 46 {String host, |
| 47 bool secure: false, | 47 bool secure: false, |
| 48 DateTime ifModifiedSince, | 48 DateTime ifModifiedSince, |
| 49 bool rawPath: false, | 49 bool rawPath: false, |
| 50 bool followRedirects: true}) { | 50 bool followRedirects: true, |
| 51 int from, | |
| 52 int to}) { | |
| 51 | 53 |
| 52 // if this is a mock test, then run the mock code path | 54 // if this is a mock test, then run the mock code path |
| 53 if(_isMockTestExpando[currentTestCase]) { | 55 if(_isMockTestExpando[currentTestCase]) { |
| 54 var uri = _getUri(0, path, secure: secure, rawPath: rawPath); | 56 var uri = _getUri(0, path, secure: secure, rawPath: rawPath); |
| 55 | 57 |
| 56 var request = new MockHttpRequest(uri, followRedirects: followRedirects, | 58 var request = new MockHttpRequest(uri, followRedirects: followRedirects, |
| 57 ifModifiedSince: ifModifiedSince); | 59 ifModifiedSince: ifModifiedSince); |
| 60 _addRangeHeader(request, from, to); | |
| 58 | 61 |
| 59 return _withMockRequest(virtualDir, request) | 62 return _withMockRequest(virtualDir, request) |
| 60 .then((response) { | 63 .then((response) { |
| 61 return response.statusCode; | 64 return response.statusCode; |
| 62 }); | 65 }); |
| 63 }; | 66 }; |
| 64 | 67 |
| 65 assert(_isMockTestExpando[currentTestCase] == false); | 68 assert(_isMockTestExpando[currentTestCase] == false); |
| 66 | 69 |
| 67 return _withServer(virtualDir, (port) { | 70 return _withServer(virtualDir, (port) { |
| 68 return getStatusCode(port, path, host: host, secure: secure, | 71 return getStatusCode(port, path, host: host, secure: secure, |
| 69 ifModifiedSince: ifModifiedSince, rawPath: rawPath, | 72 ifModifiedSince: ifModifiedSince, rawPath: rawPath, |
| 70 followRedirects: followRedirects); | 73 followRedirects: followRedirects, from: from, to: to); |
| 71 }); | 74 }); |
| 72 } | 75 } |
| 73 | 76 |
| 74 Future<int> getStatusCode(int port, | 77 Future<int> getStatusCode(int port, |
| 75 String path, | 78 String path, |
| 76 {String host, | 79 {String host, |
| 77 bool secure: false, | 80 bool secure: false, |
| 78 DateTime ifModifiedSince, | 81 DateTime ifModifiedSince, |
| 79 bool rawPath: false, | 82 bool rawPath: false, |
| 80 bool followRedirects: true}) { | 83 bool followRedirects: true, |
| 84 int from, | |
| 85 int to}) { | |
| 81 var uri = _getUri(port, path, secure: secure, rawPath: rawPath); | 86 var uri = _getUri(port, path, secure: secure, rawPath: rawPath); |
| 82 | 87 |
| 83 return new HttpClient().getUrl(uri) | 88 return new HttpClient().getUrl(uri) |
| 84 .then((request) { | 89 .then((request) { |
| 85 if (!followRedirects) request.followRedirects = false; | 90 if (!followRedirects) request.followRedirects = false; |
| 86 if (host != null) request.headers.host = host; | 91 if (host != null) request.headers.host = host; |
| 87 if (ifModifiedSince != null) { | 92 if (ifModifiedSince != null) { |
| 88 request.headers.ifModifiedSince = ifModifiedSince; | 93 request.headers.ifModifiedSince = ifModifiedSince; |
| 89 } | 94 } |
| 95 _addRangeHeader(request, from, to); | |
| 90 return request.close(); | 96 return request.close(); |
| 91 }) | 97 }) |
| 92 .then((response) => response.drain().then( | 98 .then((response) => response.drain().then( |
| 93 (_) => response.statusCode)); | 99 (_) => response.statusCode)); |
| 94 } | 100 } |
| 95 | 101 |
| 96 Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) { | 102 Future<HttpHeaders> getHeaders( |
| 103 VirtualDirectory virDir, String path, {int from, int to}) { | |
| 97 | 104 |
| 98 // if this is a mock test, then run the mock code path | 105 // if this is a mock test, then run the mock code path |
| 99 if(_isMockTestExpando[currentTestCase]) { | 106 if(_isMockTestExpando[currentTestCase]) { |
| 100 var uri = _getUri(0, path); | 107 var uri = _getUri(0, path); |
| 101 | 108 |
| 102 var request = new MockHttpRequest(uri); | 109 var request = new MockHttpRequest(uri); |
| 110 _addRangeHeader(request, from, to); | |
| 103 | 111 |
| 104 return _withMockRequest(virDir, request) | 112 return _withMockRequest(virDir, request) |
| 105 .then((response) { | 113 .then((response) { |
| 106 return response.headers; | 114 return response.headers; |
| 107 }); | 115 }); |
| 108 } | 116 } |
| 109 | 117 |
| 110 assert(_isMockTestExpando[currentTestCase] == false); | 118 assert(_isMockTestExpando[currentTestCase] == false); |
| 111 | 119 |
| 112 return _withServer(virDir, (port) { | 120 return _withServer(virDir, (port) { |
| 113 return _getHeaders(port, path); | 121 return _getHeaders(port, path, from, to); |
| 114 }); | 122 }); |
| 115 } | 123 } |
| 116 | 124 |
| 117 Future<String> getAsString(VirtualDirectory virtualDir, String path) { | 125 Future<String> getAsString(VirtualDirectory virtualDir, String path) { |
| 118 | 126 |
| 119 // if this is a mock test, then run the mock code path | 127 // if this is a mock test, then run the mock code path |
| 120 if(_isMockTestExpando[currentTestCase]) { | 128 if(_isMockTestExpando[currentTestCase]) { |
| 121 var uri = _getUri(0, path); | 129 var uri = _getUri(0, path); |
| 122 | 130 |
| 123 var request = new MockHttpRequest(uri); | 131 var request = new MockHttpRequest(uri); |
| 124 | 132 |
| 125 return _withMockRequest(virtualDir, request) | 133 return _withMockRequest(virtualDir, request) |
| 126 .then((response) { | 134 .then((response) { |
| 127 return response.mockContent; | 135 return response.mockContent; |
| 128 }); | 136 }); |
| 129 }; | 137 }; |
| 130 | 138 |
| 131 assert(_isMockTestExpando[currentTestCase] == false); | 139 assert(_isMockTestExpando[currentTestCase] == false); |
| 132 | 140 |
| 133 return _withServer(virtualDir, (int port) { | 141 return _withServer(virtualDir, (int port) { |
| 134 return _getAsString(port, path); | 142 return _getAsString(port, path); |
| 135 }); | 143 }); |
| 136 } | 144 } |
| 137 | 145 |
| 146 Future<String> getAsBytes( | |
| 147 VirtualDirectory virtualDir, String path, {int from, int to}) { | |
| 148 | |
| 149 // if this is a mock test, then run the mock code path | |
| 150 if(_isMockTestExpando[currentTestCase]) { | |
|
kustermann
2014/11/14 13:25:27
space after 'if'
Søren Gjesse
2014/11/14 15:27:29
Done.
| |
| 151 var uri = _getUri(0, path); | |
| 152 | |
| 153 var request = new MockHttpRequest(uri); | |
| 154 _addRangeHeader(request, from, to); | |
| 155 | |
| 156 return _withMockRequest(virtualDir, request) | |
| 157 .then((response) { | |
| 158 return response.mockContentBinary; | |
| 159 }); | |
| 160 }; | |
| 161 | |
| 162 assert(_isMockTestExpando[currentTestCase] == false); | |
|
kustermann
2014/11/14 13:25:27
This assert is not doing anything, If you go into
Søren Gjesse
2014/11/14 15:27:30
Honestly don't know. It is in all these methods.
| |
| 163 | |
| 164 return _withServer(virtualDir, (int port) { | |
| 165 return _getAsBytes(port, path, from, to); | |
|
kustermann
2014/11/14 13:25:27
_getAsBytes is returning Future<List<int>> but the
Søren Gjesse
2014/11/14 15:27:29
Done.
| |
| 166 }); | |
| 167 } | |
| 168 | |
| 138 Future<MockHttpResponse> _withMockRequest(VirtualDirectory virDir, | 169 Future<MockHttpResponse> _withMockRequest(VirtualDirectory virDir, |
| 139 MockHttpRequest request) { | 170 MockHttpRequest request) { |
| 140 return virDir.serveRequest(request).then((value) { | 171 return virDir.serveRequest(request).then((value) { |
| 141 expect(value, isNull); | 172 expect(value, isNull); |
| 142 expect(request.response.mockDone, isTrue); | 173 expect(request.response.mockDone, isTrue); |
| 143 return request.response; | 174 return request.response; |
| 144 }) | 175 }) |
| 145 .then((HttpResponse response) { | 176 .then((HttpResponse response) { |
| 146 if(response.statusCode == HttpStatus.MOVED_PERMANENTLY || | 177 if(response.statusCode == HttpStatus.MOVED_PERMANENTLY || |
| 147 response.statusCode == HttpStatus.MOVED_TEMPORARILY) { | 178 response.statusCode == HttpStatus.MOVED_TEMPORARILY) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 160 HttpServer server; | 191 HttpServer server; |
| 161 return HttpServer.bind('localhost', 0) | 192 return HttpServer.bind('localhost', 0) |
| 162 .then((value) { | 193 .then((value) { |
| 163 server = value; | 194 server = value; |
| 164 virDir.serve(server); | 195 virDir.serve(server); |
| 165 return func(server.port); | 196 return func(server.port); |
| 166 }) | 197 }) |
| 167 .whenComplete(() => server.close()); | 198 .whenComplete(() => server.close()); |
| 168 } | 199 } |
| 169 | 200 |
| 170 Future<HttpHeaders> _getHeaders(int port, String path) => | 201 Future<HttpHeaders> _getHeaders(int port, String path, int from, int to) => |
| 171 new HttpClient() | 202 new HttpClient() |
|
kustermann
2014/11/14 13:25:27
Who is closing HttpClient here?
Søren Gjesse
2014/11/14 15:27:29
Nobody. Added close.
| |
| 172 .get('localhost', port, path) | 203 .get('localhost', port, path) |
| 173 .then((request) => request.close()) | 204 .then((request) { |
| 205 _addRangeHeader(request, from, to); | |
| 206 return request.close(); | |
| 207 }) | |
| 174 .then((response) => response.drain().then( | 208 .then((response) => response.drain().then( |
| 175 (_) => response.headers)); | 209 (_) => response.headers)); |
| 176 | 210 |
| 177 Future<String> _getAsString(int port, String path) => | 211 Future<String> _getAsString(int port, String path) => |
| 178 new HttpClient() | 212 new HttpClient() |
|
kustermann
2014/11/14 13:25:27
ditto.
Søren Gjesse
2014/11/14 15:27:29
Ditto.
| |
| 179 .get('localhost', port, path) | 213 .get('localhost', port, path) |
| 180 .then((request) => request.close()) | 214 .then((request) => request.close()) |
| 181 .then((response) => UTF8.decodeStream(response)); | 215 .then((response) => UTF8.decodeStream(response)); |
| 182 | 216 |
| 217 Future<List<int>> _getAsBytes(int port, String path, int from, int to) => | |
| 218 new HttpClient() | |
|
kustermann
2014/11/14 13:25:27
ditto
Søren Gjesse
2014/11/14 15:27:29
Ditto.
| |
| 219 .get('localhost', port, path) | |
| 220 .then((request) { | |
| 221 _addRangeHeader(request, from, to); | |
| 222 return request.close(); | |
| 223 }) | |
| 224 .then((response) => response.fold([], (p, e) => p..addAll(e))); | |
| 225 | |
| 183 Uri _getUri(int port, | 226 Uri _getUri(int port, |
| 184 String path, | 227 String path, |
| 185 {bool secure: false, | 228 {bool secure: false, |
| 186 bool rawPath: false}) { | 229 bool rawPath: false}) { |
| 187 if (rawPath) { | 230 if (rawPath) { |
| 188 return new Uri(scheme: secure ? 'https' : 'http', | 231 return new Uri(scheme: secure ? 'https' : 'http', |
| 189 host: 'localhost', | 232 host: 'localhost', |
| 190 port: port, | 233 port: port, |
| 191 path: path); | 234 path: path); |
| 192 } else { | 235 } else { |
| 193 return (secure ? | 236 return (secure ? |
| 194 new Uri.https('localhost:$port', path) : | 237 new Uri.https('localhost:$port', path) : |
| 195 new Uri.http('localhost:$port', path)); | 238 new Uri.http('localhost:$port', path)); |
| 196 } | 239 } |
| 197 } | 240 } |
| 198 | 241 |
| 242 void _addRangeHeader(HttpRequest request, int from, int to) { | |
| 243 var fromStr = from != null ? '$from' : ''; | |
| 244 var toStr = to != null ? '$to' : ''; | |
| 245 if (fromStr.isNotEmpty || toStr.isNotEmpty) { | |
| 246 request.headers.set(HttpHeaders.RANGE, 'bytes=$fromStr-$toStr'); | |
| 247 } | |
| 248 } | |
| 249 | |
| 199 const CERTIFICATE = "localhost_cert"; | 250 const CERTIFICATE = "localhost_cert"; |
| 200 | 251 |
| 201 | 252 |
| 202 void setupSecure() { | 253 void setupSecure() { |
| 203 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | 254 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 204 SecureSocket.initialize(database: certificateDatabase, | 255 SecureSocket.initialize(database: certificateDatabase, |
| 205 password: 'dartdart'); | 256 password: 'dartdart'); |
| 206 } | 257 } |
| OLD | NEW |