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 "dart:async"; | 5 import "dart:async"; |
6 import "dart:typed_data"; | 6 import "dart:typed_data"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 | 8 |
9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 | 11 |
| 12 void testDefaultResponseHeaders() { |
| 13 checkDefaultHeaders(headers) { |
| 14 Expect.listEquals(headers[HttpHeaders.CONTENT_TYPE], |
| 15 ['text/plain; charset=utf-8']); |
| 16 Expect.listEquals(headers['X-Frame-Options'], |
| 17 ['SAMEORIGIN']); |
| 18 Expect.listEquals(headers['X-Content-Type-Options'], |
| 19 ['nosniff']); |
| 20 Expect.listEquals(headers['X-XSS-Protection'], |
| 21 ['1; mode=block']); |
| 22 } |
| 23 |
| 24 checkDefaultHeadersClear(headers) { |
| 25 Expect.isNull(headers[HttpHeaders.CONTENT_TYPE]); |
| 26 Expect.isNull(headers['X-Frame-Options']); |
| 27 Expect.isNull(headers['X-Content-Type-Options']); |
| 28 Expect.isNull(headers['X-XSS-Protection']); |
| 29 } |
| 30 |
| 31 checkDefaultHeadersClearAB(headers) { |
| 32 Expect.isNull(headers[HttpHeaders.CONTENT_TYPE]); |
| 33 Expect.isNull(headers['X-Frame-Options']); |
| 34 Expect.isNull(headers['X-Content-Type-Options']); |
| 35 Expect.isNull(headers['X-XSS-Protection']); |
| 36 Expect.listEquals(headers['a'], ['b']); |
| 37 } |
| 38 |
| 39 test(bool clearHeaders, Map defaultHeaders, Function checker) { |
| 40 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 41 if (clearHeaders) server.defaultResponseHeaders.clear(); |
| 42 if (defaultHeaders != null) { |
| 43 defaultHeaders.forEach( |
| 44 (name, value) => server.defaultResponseHeaders.add(name, value)); |
| 45 } |
| 46 checker(server.defaultResponseHeaders); |
| 47 server.listen((request) { |
| 48 request.response.close(); |
| 49 }); |
| 50 |
| 51 HttpClient client = new HttpClient(); |
| 52 client.get("127.0.0.1", server.port, "/") |
| 53 .then((request) => request.close()) |
| 54 .then((response) { |
| 55 checker(response.headers); |
| 56 server.close(); |
| 57 client.close(); |
| 58 }); |
| 59 }); |
| 60 } |
| 61 |
| 62 test(false, null, checkDefaultHeaders); |
| 63 test(true, null, checkDefaultHeadersClear); |
| 64 test(true, {'a': 'b'}, checkDefaultHeadersClearAB); |
| 65 } |
| 66 |
| 67 void testDefaultResponseHeadersContentType() { |
| 68 test(bool clearHeaders, String requestBody, List<int> responseBody) { |
| 69 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 70 if (clearHeaders) server.defaultResponseHeaders.clear(); |
| 71 server.listen((request) { |
| 72 request.response.write(requestBody); |
| 73 request.response.close(); |
| 74 }); |
| 75 |
| 76 HttpClient client = new HttpClient(); |
| 77 client.get("127.0.0.1", server.port, "/") |
| 78 .then((request) => request.close()) |
| 79 .then((response) { |
| 80 response |
| 81 .fold([], (a, b) => a..addAll(b)) |
| 82 .then((body) { |
| 83 Expect.listEquals(body, responseBody); |
| 84 }) |
| 85 .whenComplete(() { |
| 86 server.close(); |
| 87 client.close(); |
| 88 }); |
| 89 }); |
| 90 }); |
| 91 } |
| 92 |
| 93 test(false, 'æøå', [195, 166, 195, 184, 195, 165]); |
| 94 test(true, 'æøå', [230, 248, 229]); |
| 95 } |
| 96 |
12 void testListenOn() { | 97 void testListenOn() { |
13 ServerSocket socket; | 98 ServerSocket socket; |
14 HttpServer server; | 99 HttpServer server; |
15 | 100 |
16 void test(void onDone()) { | 101 void test(void onDone()) { |
17 Expect.equals(socket.port, server.port); | 102 Expect.equals(socket.port, server.port); |
18 | 103 |
19 HttpClient client = new HttpClient(); | 104 HttpClient client = new HttpClient(); |
20 client.get("127.0.0.1", socket.port, "/") | 105 client.get("127.0.0.1", socket.port, "/") |
21 .then((request) { | 106 .then((request) { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 client.get("127.0.0.1", server.port, "/") | 215 client.get("127.0.0.1", server.port, "/") |
131 .then((request) => request.close()) | 216 .then((request) => request.close()) |
132 .then((response) { | 217 .then((response) { |
133 response.listen((_) {}).cancel(); | 218 response.listen((_) {}).cancel(); |
134 }); | 219 }); |
135 }); | 220 }); |
136 } | 221 } |
137 | 222 |
138 | 223 |
139 void main() { | 224 void main() { |
| 225 testDefaultResponseHeaders(); |
| 226 testDefaultResponseHeadersContentType(); |
140 testListenOn(); | 227 testListenOn(); |
141 testHttpServerZone(); | 228 testHttpServerZone(); |
142 testHttpServerZoneError(); | 229 testHttpServerZoneError(); |
143 testHttpServerClientClose(); | 230 testHttpServerClientClose(); |
144 } | 231 } |
OLD | NEW |