| 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import "package:async_helper/async_helper.dart"; | 6 import "package:async_helper/async_helper.dart"; |
| 7 import "dart:async"; | 7 import "dart:async"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 | 9 |
| 10 const PATH = '/path?a=b#c'; | 10 const sendPath = '/path?a=b#c'; |
| 11 const expectedPath = '/path?a=b'; |
| 11 | 12 |
| 12 void test(String expected, Map headers) { | 13 void test(String expected, Map headers) { |
| 13 asyncStart(); | 14 asyncStart(); |
| 14 HttpServer.bind("localhost", 0).then((server) { | 15 HttpServer.bind("localhost", 0).then((server) { |
| 15 expected = expected.replaceAll('%PORT', server.port.toString()); | 16 expected = expected.replaceAll('%PORT', server.port.toString()); |
| 16 server.listen((request) { | 17 server.listen((request) { |
| 17 Expect.equals("$expected$PATH", | 18 Expect.equals("$expected$expectedPath", |
| 18 request.requestedUri.toString()); | 19 request.requestedUri.toString()); |
| 19 request.response.close(); | 20 request.response.close(); |
| 20 }); | 21 }); |
| 21 HttpClient client = new HttpClient(); | 22 HttpClient client = new HttpClient(); |
| 22 client.get("localhost", server.port, PATH) | 23 client.get("localhost", server.port, sendPath) |
| 23 .then((request) { | 24 .then((request) { |
| 24 for (var v in headers.keys) { | 25 for (var v in headers.keys) { |
| 25 if (headers[v] != null) { | 26 if (headers[v] != null) { |
| 26 request.headers.set(v, headers[v]); | 27 request.headers.set(v, headers[v]); |
| 27 } else { | 28 } else { |
| 28 request.headers.removeAll(v); | 29 request.headers.removeAll(v); |
| 29 } | 30 } |
| 30 } | 31 } |
| 31 return request.close(); | 32 return request.close(); |
| 32 }) | 33 }) |
| 33 .then((response) => response.drain()) | 34 .then((response) => response.drain()) |
| 34 .then((_) { | 35 .then((_) { |
| 35 server.close(); | 36 server.close(); |
| 36 asyncEnd(); | 37 asyncEnd(); |
| 37 }); | 38 }); |
| 38 }); | 39 }); |
| 39 } | 40 } |
| 40 | 41 |
| 41 | 42 |
| 42 void main() { | 43 void main() { |
| 43 test('http://localhost:%PORT', {}); | 44 test('http://localhost:%PORT', {}); |
| 44 test('https://localhost:%PORT', {'x-forwarded-proto': 'https'}); | 45 test('https://localhost:%PORT', {'x-forwarded-proto': 'https'}); |
| 45 test('ws://localhost:%PORT', {'x-forwarded-proto': 'ws'}); | 46 test('ws://localhost:%PORT', {'x-forwarded-proto': 'ws'}); |
| 46 test('http://my-host:321', {'x-forwarded-host': 'my-host:321'}); | 47 test('http://my-host:321', {'x-forwarded-host': 'my-host:321'}); |
| 47 test('http://localhost:%PORT', {'host': null}); | 48 test('http://localhost:%PORT', {'host': null}); |
| 48 } | 49 } |
| 49 | 50 |
| OLD | NEW |