OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:http/http.dart' as http; | 7 import 'package:http/http.dart' as http; |
8 import 'package:http/testing.dart'; | 8 import 'package:http/testing.dart'; |
9 import 'package:scheduled_test/scheduled_test.dart'; | 9 import 'package:scheduled_test/scheduled_test.dart'; |
10 import 'package:shelf/shelf.dart' as shelf; | 10 import 'package:shelf/shelf.dart' as shelf; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 containsPair('warning', '214 shelf_proxy "GZIP decoded"')); | 208 containsPair('warning', '214 shelf_proxy "GZIP decoded"')); |
209 }), completes); | 209 }), completes); |
210 }); | 210 }); |
211 } | 211 } |
212 | 212 |
213 /// Creates a proxy server proxying to a server running [handler]. | 213 /// Creates a proxy server proxying to a server running [handler]. |
214 /// | 214 /// |
215 /// [targetPath] is the root-relative path on the target server to proxy to. It | 215 /// [targetPath] is the root-relative path on the target server to proxy to. It |
216 /// defaults to `/`. | 216 /// defaults to `/`. |
217 void createProxy(shelf.Handler handler, {String targetPath}) { | 217 void createProxy(shelf.Handler handler, {String targetPath}) { |
218 handler = wrapAsync(handler, 'target server handler'); | 218 handler = expectAsync(handler, reason: 'target server handler'); |
219 schedule(() { | 219 schedule(() { |
220 return shelf_io.serve(handler, 'localhost', 0).then((targetServer) { | 220 return shelf_io.serve(handler, 'localhost', 0).then((targetServer) { |
221 targetUri = Uri.parse('http://localhost:${targetServer.port}'); | 221 targetUri = Uri.parse('http://localhost:${targetServer.port}'); |
222 if (targetPath != null) targetUri = targetUri.resolve(targetPath); | 222 if (targetPath != null) targetUri = targetUri.resolve(targetPath); |
223 var proxyServerHandler = wrapAsync( | 223 var proxyServerHandler = expectAsync( |
224 proxyHandler(targetUri), 'proxy server handler'); | 224 proxyHandler(targetUri), reason: 'proxy server handler'); |
225 | 225 |
226 return shelf_io.serve(proxyServerHandler, 'localhost', 0) | 226 return shelf_io.serve(proxyServerHandler, 'localhost', 0) |
227 .then((proxyServer) { | 227 .then((proxyServer) { |
228 proxyUri = Uri.parse('http://localhost:${proxyServer.port}'); | 228 proxyUri = Uri.parse('http://localhost:${proxyServer.port}'); |
229 | 229 |
230 currentSchedule.onComplete.schedule(() { | 230 currentSchedule.onComplete.schedule(() { |
231 proxyServer.close(force: true); | 231 proxyServer.close(force: true); |
232 targetServer.close(force: true); | 232 targetServer.close(force: true); |
233 }, 'tear down servers'); | 233 }, 'tear down servers'); |
234 }); | 234 }); |
(...skipping 13 matching lines...) Expand all Loading... |
248 /// Schedules a GET request with [headers] to the proxy server. | 248 /// Schedules a GET request with [headers] to the proxy server. |
249 Future<http.Response> get({Map<String, String> headers}) { | 249 Future<http.Response> get({Map<String, String> headers}) { |
250 return schedule(() { | 250 return schedule(() { |
251 var uri = proxyUri; | 251 var uri = proxyUri; |
252 var request = new http.Request('GET', uri); | 252 var request = new http.Request('GET', uri); |
253 if (headers != null) request.headers.addAll(headers); | 253 if (headers != null) request.headers.addAll(headers); |
254 request.followRedirects = false; | 254 request.followRedirects = false; |
255 return request.send().then(http.Response.fromStream); | 255 return request.send().then(http.Response.fromStream); |
256 }, 'GET proxy server'); | 256 }, 'GET proxy server'); |
257 } | 257 } |
OLD | NEW |