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 "dart:isolate"; | 6 import "dart:isolate"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 | 8 |
9 void testNoBody(int totalConnections, bool explicitContentLength) { | 9 void testNoBody(int totalConnections, bool explicitContentLength) { |
10 var errors = 0; | 10 var errors = 0; |
11 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { | 11 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { |
12 server.listen( | 12 server.listen( |
13 (HttpRequest request) { | 13 (HttpRequest request) { |
14 Expect.equals("0", request.headers.value('content-length')); | 14 Expect.equals("0", request.headers.value('content-length')); |
15 Expect.equals(0, request.contentLength); | 15 Expect.equals(0, request.contentLength); |
16 var response = request.response; | 16 var response = request.response; |
17 response.contentLength = 0; | 17 response.contentLength = 0; |
18 response.done | 18 response.done |
19 .then((_) { | 19 .then((_) { |
20 Expect.fail("Unexpected successful response completion"); | 20 Expect.fail("Unexpected successful response completion"); |
21 }) | 21 }) |
22 .catchError((e) { | 22 .catchError((error) { |
23 Expect.isTrue(e.error is HttpException); | 23 Expect.isTrue(error is HttpException); |
24 }); | 24 }); |
25 // write with content length 0 closes the connection and | 25 // write with content length 0 closes the connection and |
26 // reports an error. | 26 // reports an error. |
27 response.write("x"); | 27 response.write("x"); |
28 // Subsequent write are ignored as there is already an | 28 // Subsequent write are ignored as there is already an |
29 // error. | 29 // error. |
30 response.write("x"); | 30 response.write("x"); |
31 // After an explicit close, write becomes a state error | 31 // After an explicit close, write becomes a state error |
32 // because we have said we will not add more. | 32 // because we have said we will not add more. |
33 response.close(); | 33 response.close(); |
34 Expect.throws(() => response.write("x"), | 34 Expect.throws(() => response.write("x"), |
35 (e) => e is StateError); | 35 (e) => e is StateError); |
36 }, | 36 }, |
37 onError: (e) { | 37 onError: (e) { |
38 Expect.fail("Unexpected server error $e"); | 38 String msg = "Unexpected server error $e"; |
| 39 var trace = getAttachedStackTrace(e); |
| 40 if (trace != null) msg += "\nStackTrace: $trace"; |
| 41 Expect.fail(msg); |
39 }); | 42 }); |
40 | 43 |
41 int count = 0; | 44 int count = 0; |
42 HttpClient client = new HttpClient(); | 45 HttpClient client = new HttpClient(); |
43 for (int i = 0; i < totalConnections; i++) { | 46 for (int i = 0; i < totalConnections; i++) { |
44 client.get("127.0.0.1", server.port, "/") | 47 client.get("127.0.0.1", server.port, "/") |
45 .then((request) { | 48 .then((request) { |
46 if (explicitContentLength) { | 49 if (explicitContentLength) { |
47 request.contentLength = 0; | 50 request.contentLength = 0; |
48 } | 51 } |
49 return request.close(); | 52 return request.close(); |
50 }) | 53 }) |
51 .then((response) { | 54 .then((response) { |
52 Expect.equals("0", response.headers.value('content-length')); | 55 Expect.equals("0", response.headers.value('content-length')); |
53 Expect.equals(0, response.contentLength); | 56 Expect.equals(0, response.contentLength); |
54 response.listen( | 57 response.listen( |
55 (d) {}, | 58 (d) {}, |
56 onDone: () { | 59 onDone: () { |
57 if (++count == totalConnections) { | 60 if (++count == totalConnections) { |
58 client.close(); | 61 client.close(); |
59 server.close(); | 62 server.close(); |
60 } | 63 } |
61 }); | 64 }); |
62 }) | 65 }) |
63 .catchError((e) { | 66 .catchError((e) { |
64 Expect.fail("Unexpected error $e"); | 67 String msg = "Unexpected error $e"; |
65 }); | 68 var trace = getAttachedStackTrace(e); |
| 69 if (trace != null) msg += "\nStackTrace: $trace"; |
| 70 Expect.fail(msg); |
| 71 }); |
66 } | 72 } |
67 }); | 73 }); |
68 } | 74 } |
69 | 75 |
70 void testBody(int totalConnections, bool useHeader) { | 76 void testBody(int totalConnections, bool useHeader) { |
71 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { | 77 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { |
72 int serverCount = 0; | 78 int serverCount = 0; |
73 server.listen( | 79 server.listen( |
74 (HttpRequest request) { | 80 (HttpRequest request) { |
75 Expect.equals("2", request.headers.value('content-length')); | 81 Expect.equals("2", request.headers.value('content-length')); |
76 Expect.equals(2, request.contentLength); | 82 Expect.equals(2, request.contentLength); |
77 var response = request.response; | 83 var response = request.response; |
78 if (useHeader) { | 84 if (useHeader) { |
79 response.contentLength = 2; | 85 response.contentLength = 2; |
80 } else { | 86 } else { |
81 response.headers.set("content-length", 2); | 87 response.headers.set("content-length", 2); |
82 } | 88 } |
83 request.listen( | 89 request.listen( |
84 (d) {}, | 90 (d) {}, |
85 onDone: () { | 91 onDone: () { |
86 response.write("x"); | 92 response.write("x"); |
87 Expect.throws(() => response.contentLength = 3, | 93 Expect.throws(() => response.contentLength = 3, |
88 (e) => e is HttpException); | 94 (e) => e is HttpException); |
89 response.write("x"); | 95 response.write("x"); |
90 response.write("x"); | 96 response.write("x"); |
91 response.done | 97 response.done |
92 .then((_) { | 98 .then((_) { |
93 Expect.fail("Unexpected successful response completion"); | 99 Expect.fail("Unexpected successful response completion"); |
94 }) | 100 }) |
95 .catchError((e) { | 101 .catchError((error) { |
96 Expect.isTrue(e.error is HttpException, "[$e]"); | 102 Expect.isTrue(error is HttpException, "[$error]"); |
97 if (++serverCount == totalConnections) { | 103 if (++serverCount == totalConnections) { |
98 server.close(); | 104 server.close(); |
99 } | 105 } |
100 }); | 106 }); |
101 response.close(); | 107 response.close(); |
102 Expect.throws(() => response.write("x"), | 108 Expect.throws(() => response.write("x"), |
103 (e) => e is StateError); | 109 (e) => e is StateError); |
104 }); | 110 }); |
105 }, | 111 }, |
106 onError: (e) => Expect.fail("Unexpected error $e")); | 112 onError: (e) { |
| 113 String msg = "Unexpected error $e"; |
| 114 var trace = getAttachedStackTrace(e); |
| 115 if (trace != null) msg += "\nStackTrace: $trace"; |
| 116 Expect.fail(msg); |
| 117 }); |
107 | 118 |
108 int clientCount = 0; | 119 int clientCount = 0; |
109 HttpClient client = new HttpClient(); | 120 HttpClient client = new HttpClient(); |
110 for (int i = 0; i < totalConnections; i++) { | 121 for (int i = 0; i < totalConnections; i++) { |
111 client.get("127.0.0.1", server.port, "/") | 122 client.get("127.0.0.1", server.port, "/") |
112 .then((request) { | 123 .then((request) { |
113 if (useHeader) { | 124 if (useHeader) { |
114 request.contentLength = 2; | 125 request.contentLength = 2; |
115 } else { | 126 } else { |
116 request.headers.add(HttpHeaders.CONTENT_LENGTH, "7"); | 127 request.headers.add(HttpHeaders.CONTENT_LENGTH, "7"); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 Expect.throws( | 172 Expect.throws( |
162 () => response.headers.chunkedTransferEncoding = false, | 173 () => response.headers.chunkedTransferEncoding = false, |
163 (e) => e is HttpException); | 174 (e) => e is HttpException); |
164 response.write("x"); | 175 response.write("x"); |
165 response.write("x"); | 176 response.write("x"); |
166 response.close(); | 177 response.close(); |
167 Expect.throws(() => response.write("x"), | 178 Expect.throws(() => response.write("x"), |
168 (e) => e is StateError); | 179 (e) => e is StateError); |
169 }); | 180 }); |
170 }, | 181 }, |
171 onError: (e) => Expect.fail("Unexpected error $e")); | 182 onError: (e) { |
| 183 String msg = "Unexpected error $e"; |
| 184 var trace = getAttachedStackTrace(e); |
| 185 if (trace != null) msg += "\nStackTrace: $trace"; |
| 186 Expect.fail(msg); |
| 187 }); |
172 | 188 |
173 int count = 0; | 189 int count = 0; |
174 HttpClient client = new HttpClient(); | 190 HttpClient client = new HttpClient(); |
175 for (int i = 0; i < totalConnections; i++) { | 191 for (int i = 0; i < totalConnections; i++) { |
176 client.get("127.0.0.1", server.port, "/") | 192 client.get("127.0.0.1", server.port, "/") |
177 .then((request) { | 193 .then((request) { |
178 if (useHeader) { | 194 if (useHeader) { |
179 request.contentLength = 2; | 195 request.contentLength = 2; |
180 request.headers.chunkedTransferEncoding = true; | 196 request.headers.chunkedTransferEncoding = true; |
181 } else { | 197 } else { |
(...skipping 12 matching lines...) Expand all Loading... |
194 Expect.equals(-1, response.contentLength); | 210 Expect.equals(-1, response.contentLength); |
195 response.listen( | 211 response.listen( |
196 (d) {}, | 212 (d) {}, |
197 onDone: () { | 213 onDone: () { |
198 if (++count == totalConnections) { | 214 if (++count == totalConnections) { |
199 client.close(); | 215 client.close(); |
200 server.close(); | 216 server.close(); |
201 } | 217 } |
202 }); | 218 }); |
203 }) | 219 }) |
204 .catchError((e) => Expect.fail("Unexpected error $e")); | 220 .catchError((e) { |
| 221 String msg = "Unexpected error $e"; |
| 222 var trace = getAttachedStackTrace(e); |
| 223 if (trace != null) msg += "\nStackTrace: $trace"; |
| 224 Expect.fail(msg); |
| 225 }); |
205 } | 226 } |
206 }); | 227 }); |
207 } | 228 } |
208 | 229 |
209 void testSetContentLength() { | 230 void testSetContentLength() { |
210 HttpServer.bind().then((server) { | 231 HttpServer.bind().then((server) { |
211 server.listen( | 232 server.listen( |
212 (HttpRequest request) { | 233 (HttpRequest request) { |
213 var response = request.response; | 234 var response = request.response; |
214 Expect.isNull(response.headers.value('content-length')); | 235 Expect.isNull(response.headers.value('content-length')); |
(...skipping 21 matching lines...) Expand all Loading... |
236 | 257 |
237 void main() { | 258 void main() { |
238 testNoBody(5, false); | 259 testNoBody(5, false); |
239 testNoBody(5, true); | 260 testNoBody(5, true); |
240 testBody(5, false); | 261 testBody(5, false); |
241 testBody(5, true); | 262 testBody(5, true); |
242 testBodyChunked(5, false); | 263 testBodyChunked(5, false); |
243 testBodyChunked(5, true); | 264 testBodyChunked(5, true); |
244 testSetContentLength(); | 265 testSetContentLength(); |
245 } | 266 } |
OLD | NEW |