OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 #import("dart:isolate"); | 10 #import("dart:isolate"); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // Echo the request content back to the response. | 95 // Echo the request content back to the response. |
96 void _echoHandler(HttpRequest request, HttpResponse response) { | 96 void _echoHandler(HttpRequest request, HttpResponse response) { |
97 Expect.equals("POST", request.method); | 97 Expect.equals("POST", request.method); |
98 response.contentLength = request.contentLength; | 98 response.contentLength = request.contentLength; |
99 request.inputStream.pipe(response.outputStream); | 99 request.inputStream.pipe(response.outputStream); |
100 } | 100 } |
101 | 101 |
102 // Echo the request content back to the response. | 102 // Echo the request content back to the response. |
103 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { | 103 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { |
104 Expect.equals("GET", request.method); | 104 Expect.equals("GET", request.method); |
105 request.inputStream.dataHandler = () {}; | 105 request.inputStream.onData = () {}; |
106 request.inputStream.closeHandler = () { | 106 request.inputStream.onClose = () { |
107 response.writeString("01234567890"); | 107 response.writeString("01234567890"); |
108 response.outputStream.close(); | 108 response.outputStream.close(); |
109 }; | 109 }; |
110 } | 110 } |
111 | 111 |
112 // Return a 404. | 112 // Return a 404. |
113 void _notFoundHandler(HttpRequest request, HttpResponse response) { | 113 void _notFoundHandler(HttpRequest request, HttpResponse response) { |
114 response.statusCode = HttpStatus.NOT_FOUND; | 114 response.statusCode = HttpStatus.NOT_FOUND; |
115 response.setHeader("Content-Type", "text/html; charset=UTF-8"); | 115 response.setHeader("Content-Type", "text/html; charset=UTF-8"); |
116 response.writeString("Page not found"); | 116 response.writeString("Page not found"); |
117 response.outputStream.close(); | 117 response.outputStream.close(); |
118 } | 118 } |
119 | 119 |
120 void main() { | 120 void main() { |
121 // Setup request handlers. | 121 // Setup request handlers. |
122 _requestHandlers = new Map(); | 122 _requestHandlers = new Map(); |
123 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | 123 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
124 _echoHandler(request, response); | 124 _echoHandler(request, response); |
125 }; | 125 }; |
126 _requestHandlers["/0123456789"] = | 126 _requestHandlers["/0123456789"] = |
127 (HttpRequest request, HttpResponse response) { | 127 (HttpRequest request, HttpResponse response) { |
128 _zeroToTenHandler(request, response); | 128 _zeroToTenHandler(request, response); |
129 }; | 129 }; |
130 | 130 |
131 this.port.receive((var message, SendPort replyTo) { | 131 this.port.receive((var message, SendPort replyTo) { |
132 if (message.isStart) { | 132 if (message.isStart) { |
133 _server = new HttpServer(); | 133 _server = new HttpServer(); |
134 try { | 134 try { |
135 _server.listen("127.0.0.1", 0); | 135 _server.listen("127.0.0.1", 0); |
136 _server.requestHandler = (HttpRequest req, HttpResponse rsp) { | 136 _server.onRequest = (HttpRequest req, HttpResponse rsp) { |
137 _requestReceivedHandler(req, rsp); | 137 _requestReceivedHandler(req, rsp); |
138 }; | 138 }; |
139 replyTo.send(new TestServerStatus.started(_server.port), null); | 139 replyTo.send(new TestServerStatus.started(_server.port), null); |
140 } catch (var e) { | 140 } catch (var e) { |
141 replyTo.send(new TestServerStatus.error(), null); | 141 replyTo.send(new TestServerStatus.error(), null); |
142 } | 142 } |
143 } else if (message.isStop) { | 143 } else if (message.isStop) { |
144 _server.close(); | 144 _server.close(); |
145 this.port.close(); | 145 this.port.close(); |
146 replyTo.send(new TestServerStatus.stopped(), null); | 146 replyTo.send(new TestServerStatus.stopped(), null); |
(...skipping 26 matching lines...) Expand all Loading... |
173 testServerMain.start(); | 173 testServerMain.start(); |
174 } | 174 } |
175 | 175 |
176 | 176 |
177 void testGET() { | 177 void testGET() { |
178 TestServerMain testServerMain = new TestServerMain(); | 178 TestServerMain testServerMain = new TestServerMain(); |
179 testServerMain.setServerStartedHandler((int port) { | 179 testServerMain.setServerStartedHandler((int port) { |
180 HttpClient httpClient = new HttpClient(); | 180 HttpClient httpClient = new HttpClient(); |
181 HttpClientConnection conn = | 181 HttpClientConnection conn = |
182 httpClient.get("127.0.0.1", port, "/0123456789"); | 182 httpClient.get("127.0.0.1", port, "/0123456789"); |
183 conn.responseHandler = (HttpClientResponse response) { | 183 conn.onResponse = (HttpClientResponse response) { |
184 Expect.equals(HttpStatus.OK, response.statusCode); | 184 Expect.equals(HttpStatus.OK, response.statusCode); |
185 StringInputStream stream = new StringInputStream(response.inputStream); | 185 StringInputStream stream = new StringInputStream(response.inputStream); |
186 StringBuffer body = new StringBuffer(); | 186 StringBuffer body = new StringBuffer(); |
187 stream.dataHandler = () => body.add(stream.read()); | 187 stream.onData = () => body.add(stream.read()); |
188 stream.closeHandler = () { | 188 stream.onClose = () { |
189 Expect.equals("01234567890", body.toString()); | 189 Expect.equals("01234567890", body.toString()); |
190 httpClient.shutdown(); | 190 httpClient.shutdown(); |
191 testServerMain.shutdown(); | 191 testServerMain.shutdown(); |
192 }; | 192 }; |
193 }; | 193 }; |
194 }); | 194 }); |
195 testServerMain.start(); | 195 testServerMain.start(); |
196 } | 196 } |
197 | 197 |
198 | 198 |
199 void testPOST(bool chunkedEncoding) { | 199 void testPOST(bool chunkedEncoding) { |
200 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; | 200 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; |
201 final int kMessageCount = 10; | 201 final int kMessageCount = 10; |
202 | 202 |
203 TestServerMain testServerMain = new TestServerMain(); | 203 TestServerMain testServerMain = new TestServerMain(); |
204 | 204 |
205 void runTest(int port) { | 205 void runTest(int port) { |
206 int count = 0; | 206 int count = 0; |
207 HttpClient httpClient = new HttpClient(); | 207 HttpClient httpClient = new HttpClient(); |
208 void sendRequest() { | 208 void sendRequest() { |
209 HttpClientConnection conn = | 209 HttpClientConnection conn = |
210 httpClient.post("127.0.0.1", port, "/echo"); | 210 httpClient.post("127.0.0.1", port, "/echo"); |
211 conn.requestHandler = (HttpClientRequest request) { | 211 conn.onRequest = (HttpClientRequest request) { |
212 if (chunkedEncoding) { | 212 if (chunkedEncoding) { |
213 request.writeString(data.substring(0, 10)); | 213 request.writeString(data.substring(0, 10)); |
214 request.writeString(data.substring(10, data.length)); | 214 request.writeString(data.substring(10, data.length)); |
215 } else { | 215 } else { |
216 request.contentLength = data.length; | 216 request.contentLength = data.length; |
217 request.outputStream.write(data.charCodes()); | 217 request.outputStream.write(data.charCodes()); |
218 } | 218 } |
219 request.outputStream.close(); | 219 request.outputStream.close(); |
220 }; | 220 }; |
221 conn.responseHandler = (HttpClientResponse response) { | 221 conn.onResponse = (HttpClientResponse response) { |
222 Expect.equals(HttpStatus.OK, response.statusCode); | 222 Expect.equals(HttpStatus.OK, response.statusCode); |
223 StringInputStream stream = new StringInputStream(response.inputStream); | 223 StringInputStream stream = new StringInputStream(response.inputStream); |
224 StringBuffer body = new StringBuffer(); | 224 StringBuffer body = new StringBuffer(); |
225 stream.dataHandler = () => body.add(stream.read()); | 225 stream.onData = () => body.add(stream.read()); |
226 stream.closeHandler = () { | 226 stream.onClose = () { |
227 Expect.equals(data, body.toString()); | 227 Expect.equals(data, body.toString()); |
228 count++; | 228 count++; |
229 if (count < kMessageCount) { | 229 if (count < kMessageCount) { |
230 sendRequest(); | 230 sendRequest(); |
231 } else { | 231 } else { |
232 httpClient.shutdown(); | 232 httpClient.shutdown(); |
233 testServerMain.shutdown(); | 233 testServerMain.shutdown(); |
234 } | 234 } |
235 }; | 235 }; |
236 }; | 236 }; |
(...skipping 15 matching lines...) Expand all Loading... |
252 final int kMessageCount = 10; | 252 final int kMessageCount = 10; |
253 | 253 |
254 TestServerMain testServerMain = new TestServerMain(); | 254 TestServerMain testServerMain = new TestServerMain(); |
255 | 255 |
256 void runTest(int port) { | 256 void runTest(int port) { |
257 int count = 0; | 257 int count = 0; |
258 HttpClient httpClient = new HttpClient(); | 258 HttpClient httpClient = new HttpClient(); |
259 void sendRequest() { | 259 void sendRequest() { |
260 HttpClientConnection conn = | 260 HttpClientConnection conn = |
261 httpClient.post("127.0.0.1", port, "/echo"); | 261 httpClient.post("127.0.0.1", port, "/echo"); |
262 conn.requestHandler = (HttpClientRequest request) { | 262 conn.onRequest = (HttpClientRequest request) { |
263 if (chunkedEncoding) { | 263 if (chunkedEncoding) { |
264 request.writeString(data.substring(0, 10)); | 264 request.writeString(data.substring(0, 10)); |
265 request.writeString(data.substring(10, data.length)); | 265 request.writeString(data.substring(10, data.length)); |
266 } else { | 266 } else { |
267 request.contentLength = data.length; | 267 request.contentLength = data.length; |
268 request.outputStream.write(data.charCodes()); | 268 request.outputStream.write(data.charCodes()); |
269 } | 269 } |
270 request.outputStream.close(); | 270 request.outputStream.close(); |
271 }; | 271 }; |
272 conn.responseHandler = (HttpClientResponse response) { | 272 conn.onResponse = (HttpClientResponse response) { |
273 Expect.equals(HttpStatus.OK, response.statusCode); | 273 Expect.equals(HttpStatus.OK, response.statusCode); |
274 InputStream stream = response.inputStream; | 274 InputStream stream = response.inputStream; |
275 List<int> body = new List<int>(); | 275 List<int> body = new List<int>(); |
276 stream.dataHandler = () { | 276 stream.onData = () { |
277 List tmp = new List(3); | 277 List tmp = new List(3); |
278 int bytes = stream.readInto(tmp); | 278 int bytes = stream.readInto(tmp); |
279 body.addAll(tmp.getRange(0, bytes)); | 279 body.addAll(tmp.getRange(0, bytes)); |
280 }; | 280 }; |
281 stream.closeHandler = () { | 281 stream.onClose = () { |
282 Expect.equals(data, new String.fromCharCodes(body)); | 282 Expect.equals(data, new String.fromCharCodes(body)); |
283 count++; | 283 count++; |
284 if (count < kMessageCount) { | 284 if (count < kMessageCount) { |
285 sendRequest(); | 285 sendRequest(); |
286 } else { | 286 } else { |
287 httpClient.shutdown(); | 287 httpClient.shutdown(); |
288 testServerMain.shutdown(); | 288 testServerMain.shutdown(); |
289 } | 289 } |
290 }; | 290 }; |
291 }; | 291 }; |
(...skipping 15 matching lines...) Expand all Loading... |
307 final int kMessageCount = 10; | 307 final int kMessageCount = 10; |
308 | 308 |
309 TestServerMain testServerMain = new TestServerMain(); | 309 TestServerMain testServerMain = new TestServerMain(); |
310 | 310 |
311 void runTest(int port) { | 311 void runTest(int port) { |
312 int count = 0; | 312 int count = 0; |
313 HttpClient httpClient = new HttpClient(); | 313 HttpClient httpClient = new HttpClient(); |
314 void sendRequest() { | 314 void sendRequest() { |
315 HttpClientConnection conn = | 315 HttpClientConnection conn = |
316 httpClient.post("127.0.0.1", port, "/echo"); | 316 httpClient.post("127.0.0.1", port, "/echo"); |
317 conn.requestHandler = (HttpClientRequest request) { | 317 conn.onRequest = (HttpClientRequest request) { |
318 if (chunkedEncoding) { | 318 if (chunkedEncoding) { |
319 request.writeString(data.substring(0, 10)); | 319 request.writeString(data.substring(0, 10)); |
320 request.writeString(data.substring(10, data.length)); | 320 request.writeString(data.substring(10, data.length)); |
321 } else { | 321 } else { |
322 request.contentLength = data.length; | 322 request.contentLength = data.length; |
323 request.outputStream.write(data.charCodes()); | 323 request.outputStream.write(data.charCodes()); |
324 } | 324 } |
325 request.outputStream.close(); | 325 request.outputStream.close(); |
326 }; | 326 }; |
327 conn.responseHandler = (HttpClientResponse response) { | 327 conn.onResponse = (HttpClientResponse response) { |
328 Expect.equals(HttpStatus.OK, response.statusCode); | 328 Expect.equals(HttpStatus.OK, response.statusCode); |
329 InputStream stream = response.inputStream; | 329 InputStream stream = response.inputStream; |
330 List<int> body = new List<int>(); | 330 List<int> body = new List<int>(); |
331 stream.dataHandler = () { | 331 stream.onData = () { |
332 List tmp = stream.read(2); | 332 List tmp = stream.read(2); |
333 body.addAll(tmp); | 333 body.addAll(tmp); |
334 }; | 334 }; |
335 stream.closeHandler = () { | 335 stream.onClose = () { |
336 Expect.equals(data, new String.fromCharCodes(body)); | 336 Expect.equals(data, new String.fromCharCodes(body)); |
337 count++; | 337 count++; |
338 if (count < kMessageCount) { | 338 if (count < kMessageCount) { |
339 sendRequest(); | 339 sendRequest(); |
340 } else { | 340 } else { |
341 httpClient.shutdown(); | 341 httpClient.shutdown(); |
342 testServerMain.shutdown(); | 342 testServerMain.shutdown(); |
343 } | 343 } |
344 }; | 344 }; |
345 }; | 345 }; |
346 } | 346 } |
347 | 347 |
348 sendRequest(); | 348 sendRequest(); |
349 } | 349 } |
350 | 350 |
351 testServerMain.setServerStartedHandler(runTest); | 351 testServerMain.setServerStartedHandler(runTest); |
352 if (chunkedEncoding) { | 352 if (chunkedEncoding) { |
353 testServerMain.chunkedEncoding(); | 353 testServerMain.chunkedEncoding(); |
354 } | 354 } |
355 testServerMain.start(); | 355 testServerMain.start(); |
356 } | 356 } |
357 | 357 |
358 | 358 |
359 void test404() { | 359 void test404() { |
360 TestServerMain testServerMain = new TestServerMain(); | 360 TestServerMain testServerMain = new TestServerMain(); |
361 testServerMain.setServerStartedHandler((int port) { | 361 testServerMain.setServerStartedHandler((int port) { |
362 HttpClient httpClient = new HttpClient(); | 362 HttpClient httpClient = new HttpClient(); |
363 HttpClientConnection conn = | 363 HttpClientConnection conn = |
364 httpClient.get("127.0.0.1", port, "/thisisnotfound"); | 364 httpClient.get("127.0.0.1", port, "/thisisnotfound"); |
365 conn.responseHandler = (HttpClientResponse response) { | 365 conn.onResponse = (HttpClientResponse response) { |
366 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | 366 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); |
367 httpClient.shutdown(); | 367 httpClient.shutdown(); |
368 testServerMain.shutdown(); | 368 testServerMain.shutdown(); |
369 }; | 369 }; |
370 }); | 370 }); |
371 testServerMain.start(); | 371 testServerMain.start(); |
372 } | 372 } |
373 | 373 |
374 | 374 |
375 void main() { | 375 void main() { |
376 testStartStop(); | 376 testStartStop(); |
377 testGET(); | 377 testGET(); |
378 testPOST(true); | 378 testPOST(true); |
379 testPOST(false); | 379 testPOST(false); |
380 testReadInto(true); | 380 testReadInto(true); |
381 testReadInto(false); | 381 testReadInto(false); |
382 testReadShort(true); | 382 testReadShort(true); |
383 testReadShort(false); | 383 testReadShort(false); |
384 test404(); | 384 test404(); |
385 } | 385 } |
OLD | NEW |