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:io"; | 6 import "dart:io"; |
7 import "dart:typed_data"; | 7 import "dart:typed_data"; |
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 |
12 void testClientRequest(Future handler(request)) { | 13 void testClientRequest(Future handler(request)) { |
13 HttpServer.bind("127.0.0.1", 0).then((server) { | 14 HttpServer.bind("127.0.0.1", 0).then((server) { |
14 server.listen((request) { | 15 server.listen((request) { |
15 request.drain() | 16 request.drain() |
16 .then((_) => request.response.close()) | 17 .then((_) => request.response.close()) |
17 .catchError((_) {}); | 18 .catchError((_) {}); |
18 }); | 19 }); |
19 | 20 |
20 var client = new HttpClient(); | 21 var client = new HttpClient(); |
21 client.get("127.0.0.1", server.port, "/") | 22 client.get("127.0.0.1", server.port, "/") |
22 .then((request) { | 23 .then((request) { |
23 return handler(request); | 24 return handler(request); |
24 }) | 25 }) |
25 .then((response) => response.drain()) | 26 .then((response) => response.drain()) |
26 .catchError((_) {}) | 27 .catchError((_) {}) |
27 .whenComplete(() { | 28 .whenComplete(() { |
28 client.close(); | 29 client.close(); |
29 server.close(); | 30 server.close(); |
30 }); | 31 }); |
31 }); | 32 }); |
32 } | 33 } |
33 | 34 |
| 35 |
34 void testResponseDone() { | 36 void testResponseDone() { |
35 testClientRequest((request) { | 37 testClientRequest((request) { |
36 request.close().then((res1) { | 38 request.close().then((res1) { |
37 request.done.then((res2) { | 39 request.done.then((res2) { |
38 Expect.equals(res1, res2); | 40 Expect.equals(res1, res2); |
39 }); | 41 }); |
40 }); | 42 }); |
41 return request.done; | 43 return request.done; |
42 }); | 44 }); |
43 } | 45 } |
44 | 46 |
| 47 |
45 void testBadResponseAdd() { | 48 void testBadResponseAdd() { |
46 asyncStart(); | 49 asyncStart(); |
47 testClientRequest((request) { | 50 testClientRequest((request) { |
48 request.contentLength = 0; | 51 request.contentLength = 0; |
49 request.add([0]); | 52 request.add([0]); |
50 request.close(); | 53 request.close(); |
51 request.done.catchError((error) { | 54 request.done.catchError((error) { |
52 asyncEnd(); | 55 asyncEnd(); |
53 }, test: (e) => e is HttpException); | 56 }, test: (e) => e is HttpException); |
54 return request.done; | 57 return request.done; |
(...skipping 18 matching lines...) Expand all Loading... |
73 request.add(new Uint8List(64 * 1024)); | 76 request.add(new Uint8List(64 * 1024)); |
74 request.add(new Uint8List(64 * 1024)); | 77 request.add(new Uint8List(64 * 1024)); |
75 request.close(); | 78 request.close(); |
76 request.done.catchError((error) { | 79 request.done.catchError((error) { |
77 asyncEnd(); | 80 asyncEnd(); |
78 }, test: (e) => e is HttpException); | 81 }, test: (e) => e is HttpException); |
79 return request.done; | 82 return request.done; |
80 }); | 83 }); |
81 } | 84 } |
82 | 85 |
| 86 |
83 void testBadResponseClose() { | 87 void testBadResponseClose() { |
84 asyncStart(); | 88 asyncStart(); |
85 testClientRequest((request) { | 89 testClientRequest((request) { |
86 request.contentLength = 5; | 90 request.contentLength = 5; |
87 request.close(); | 91 request.close(); |
88 request.done.catchError((error) { | 92 request.done.catchError((error) { |
89 asyncEnd(); | 93 asyncEnd(); |
90 }, test: (e) => e is HttpException); | 94 }, test: (e) => e is HttpException); |
91 return request.done; | 95 return request.done; |
92 }); | 96 }); |
93 | 97 |
94 asyncStart(); | 98 asyncStart(); |
95 testClientRequest((request) { | 99 testClientRequest((request) { |
96 request.contentLength = 5; | 100 request.contentLength = 5; |
97 request.add([0]); | 101 request.add([0]); |
98 request.close(); | 102 request.close(); |
99 request.done.catchError((error) { | 103 request.done.catchError((error) { |
100 asyncEnd(); | 104 asyncEnd(); |
101 }, test: (e) => e is HttpException); | 105 }, test: (e) => e is HttpException); |
102 return request.done; | 106 return request.done; |
103 }); | 107 }); |
104 } | 108 } |
105 | 109 |
| 110 |
| 111 void testBadHeaders() { |
| 112 asyncStart(); |
| 113 testClientRequest((request) { |
| 114 var value = "a"; |
| 115 for (int i = 0; i < 8 * 1024; i++) { |
| 116 value += 'a'; |
| 117 } |
| 118 request.headers.set('name', value); |
| 119 request.done.catchError((error) { |
| 120 asyncEnd(); |
| 121 }, test: (e) => e is HttpException); |
| 122 return request.close(); |
| 123 }); |
| 124 } |
| 125 |
| 126 |
106 void main() { | 127 void main() { |
107 testResponseDone(); | 128 testResponseDone(); |
108 testBadResponseAdd(); | 129 testBadResponseAdd(); |
109 testBadResponseClose(); | 130 testBadResponseClose(); |
| 131 testBadHeaders(); |
110 } | 132 } |
OLD | NEW |