Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: tests/standalone/io/http_content_length_test.dart

Issue 12504006: Make IOSink implement StringSink (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed second round of review comments Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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:isolate"; 5 import "dart:isolate";
6 import "dart:io"; 6 import "dart:io";
7 7
8 void testNoBody(int totalConnections, bool explicitContentLength) { 8 void testNoBody(int totalConnections, bool explicitContentLength) {
9 var errors = 0; 9 var errors = 0;
10 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { 10 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) {
11 server.listen( 11 server.listen(
12 (HttpRequest request) { 12 (HttpRequest request) {
13 Expect.equals("0", request.headers.value('content-length')); 13 Expect.equals("0", request.headers.value('content-length'));
14 Expect.equals(0, request.contentLength); 14 Expect.equals(0, request.contentLength);
15 var response = request.response; 15 var response = request.response;
16 response.contentLength = 0; 16 response.contentLength = 0;
17 response.done 17 response.done
18 .then((_) { 18 .then((_) {
19 Expect.fail("Unexpected successful response completion"); 19 Expect.fail("Unexpected successful response completion");
20 }) 20 })
21 .catchError((e) { 21 .catchError((e) {
22 Expect.isTrue(e.error is HttpException); 22 Expect.isTrue(e.error is HttpException);
23 }); 23 });
24 // addString with content length 0 closes the connection and 24 // write with content length 0 closes the connection and
25 // reports an error. 25 // reports an error.
26 response.addString("x"); 26 response.write("x");
27 // Subsequent addString are ignored as there is already an 27 // Subsequent write are ignored as there is already an
28 // error. 28 // error.
29 response.addString("x"); 29 response.write("x");
30 // After an explicit close, addString becomes a state error 30 // After an explicit close, write becomes a state error
31 // because we have said we will not add more. 31 // because we have said we will not add more.
32 response.close(); 32 response.close();
33 Expect.throws(() => response.addString("x"), 33 Expect.throws(() => response.write("x"),
34 (e) => e is StateError); 34 (e) => e is StateError);
35 }, 35 },
36 onError: (e) { 36 onError: (e) {
37 Expect.fail("Unexpected server error $e"); 37 Expect.fail("Unexpected server error $e");
38 }); 38 });
39 39
40 int count = 0; 40 int count = 0;
41 HttpClient client = new HttpClient(); 41 HttpClient client = new HttpClient();
42 for (int i = 0; i < totalConnections; i++) { 42 for (int i = 0; i < totalConnections; i++) {
43 client.get("127.0.0.1", server.port, "/") 43 client.get("127.0.0.1", server.port, "/")
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 Expect.equals(2, request.contentLength); 75 Expect.equals(2, request.contentLength);
76 var response = request.response; 76 var response = request.response;
77 if (useHeader) { 77 if (useHeader) {
78 response.contentLength = 2; 78 response.contentLength = 2;
79 } else { 79 } else {
80 response.headers.set("content-length", 2); 80 response.headers.set("content-length", 2);
81 } 81 }
82 request.listen( 82 request.listen(
83 (d) {}, 83 (d) {},
84 onDone: () { 84 onDone: () {
85 response.addString("x"); 85 response.write("x");
86 Expect.throws(() => response.contentLength = 3, 86 Expect.throws(() => response.contentLength = 3,
87 (e) => e is HttpException); 87 (e) => e is HttpException);
88 response.addString("x"); 88 response.write("x");
89 response.addString("x"); 89 response.write("x");
90 response.done 90 response.done
91 .then((_) { 91 .then((_) {
92 Expect.fail("Unexpected successful response completion"); 92 Expect.fail("Unexpected successful response completion");
93 }) 93 })
94 .catchError((e) { 94 .catchError((e) {
95 Expect.isTrue(e.error is HttpException, "[$e]"); 95 Expect.isTrue(e.error is HttpException, "[$e]");
96 if (++serverCount == totalConnections) { 96 if (++serverCount == totalConnections) {
97 server.close(); 97 server.close();
98 } 98 }
99 }); 99 });
100 response.close(); 100 response.close();
101 Expect.throws(() => response.addString("x"), 101 Expect.throws(() => response.write("x"),
102 (e) => e is StateError); 102 (e) => e is StateError);
103 }); 103 });
104 }, 104 },
105 onError: (e) => Expect.fail("Unexpected error $e")); 105 onError: (e) => Expect.fail("Unexpected error $e"));
106 106
107 int clientCount = 0; 107 int clientCount = 0;
108 HttpClient client = new HttpClient(); 108 HttpClient client = new HttpClient();
109 for (int i = 0; i < totalConnections; i++) { 109 for (int i = 0; i < totalConnections; i++) {
110 client.get("127.0.0.1", server.port, "/") 110 client.get("127.0.0.1", server.port, "/")
111 .then((request) { 111 .then((request) {
112 if (useHeader) { 112 if (useHeader) {
113 request.contentLength = 2; 113 request.contentLength = 2;
114 } else { 114 } else {
115 request.headers.add(HttpHeaders.CONTENT_LENGTH, "7"); 115 request.headers.add(HttpHeaders.CONTENT_LENGTH, "7");
116 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); 116 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
117 } 117 }
118 request.addString("x"); 118 request.write("x");
119 Expect.throws(() => request.contentLength = 3, 119 Expect.throws(() => request.contentLength = 3,
120 (e) => e is HttpException); 120 (e) => e is HttpException);
121 request.addString("x"); 121 request.write("x");
122 return request.close(); 122 return request.close();
123 }) 123 })
124 .then((response) { 124 .then((response) {
125 Expect.equals("2", response.headers.value('content-length')); 125 Expect.equals("2", response.headers.value('content-length'));
126 Expect.equals(2, response.contentLength); 126 Expect.equals(2, response.contentLength);
127 response.listen( 127 response.listen(
128 (d) {}, 128 (d) {},
129 onDone: () { 129 onDone: () {
130 if (++clientCount == totalConnections) { 130 if (++clientCount == totalConnections) {
131 client.close(); 131 client.close();
(...skipping 14 matching lines...) Expand all
146 if (useHeader) { 146 if (useHeader) {
147 response.contentLength = 2; 147 response.contentLength = 2;
148 response.headers.chunkedTransferEncoding = true; 148 response.headers.chunkedTransferEncoding = true;
149 } else { 149 } else {
150 response.headers.set("content-length", 2); 150 response.headers.set("content-length", 2);
151 response.headers.set("transfer-encoding", "chunked"); 151 response.headers.set("transfer-encoding", "chunked");
152 } 152 }
153 request.listen( 153 request.listen(
154 (d) {}, 154 (d) {},
155 onDone: () { 155 onDone: () {
156 response.addString("x"); 156 response.write("x");
157 Expect.throws( 157 Expect.throws(
158 () => response.headers.chunkedTransferEncoding = false, 158 () => response.headers.chunkedTransferEncoding = false,
159 (e) => e is HttpException); 159 (e) => e is HttpException);
160 response.addString("x"); 160 response.write("x");
161 response.addString("x"); 161 response.write("x");
162 response.close(); 162 response.close();
163 Expect.throws(() => response.addString("x"), 163 Expect.throws(() => response.write("x"),
164 (e) => e is StateError); 164 (e) => e is StateError);
165 }); 165 });
166 }, 166 },
167 onError: (e) => Expect.fail("Unexpected error $e")); 167 onError: (e) => Expect.fail("Unexpected error $e"));
168 168
169 int count = 0; 169 int count = 0;
170 HttpClient client = new HttpClient(); 170 HttpClient client = new HttpClient();
171 for (int i = 0; i < totalConnections; i++) { 171 for (int i = 0; i < totalConnections; i++) {
172 client.get("127.0.0.1", server.port, "/") 172 client.get("127.0.0.1", server.port, "/")
173 .then((request) { 173 .then((request) {
174 if (useHeader) { 174 if (useHeader) {
175 request.contentLength = 2; 175 request.contentLength = 2;
176 request.headers.chunkedTransferEncoding = true; 176 request.headers.chunkedTransferEncoding = true;
177 } else { 177 } else {
178 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); 178 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
179 request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); 179 request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
180 } 180 }
181 request.addString("x"); 181 request.write("x");
182 Expect.throws(() => request.headers.chunkedTransferEncoding = false, 182 Expect.throws(() => request.headers.chunkedTransferEncoding = false,
183 (e) => e is HttpException); 183 (e) => e is HttpException);
184 request.addString("x"); 184 request.write("x");
185 request.addString("x"); 185 request.write("x");
186 return request.close(); 186 return request.close();
187 }) 187 })
188 .then((response) { 188 .then((response) {
189 Expect.isNull(response.headers.value('content-length')); 189 Expect.isNull(response.headers.value('content-length'));
190 Expect.equals(-1, response.contentLength); 190 Expect.equals(-1, response.contentLength);
191 response.listen( 191 response.listen(
192 (d) {}, 192 (d) {},
193 onDone: () { 193 onDone: () {
194 if (++count == totalConnections) { 194 if (++count == totalConnections) {
195 client.close(); 195 client.close();
196 server.close(); 196 server.close();
197 } 197 }
198 }); 198 });
199 }) 199 })
200 .catchError((e) => Expect.fail("Unexpected error $e")); 200 .catchError((e) => Expect.fail("Unexpected error $e"));
201 } 201 }
202 }); 202 });
203 } 203 }
204 204
205 void testSetContentLength() { 205 void testSetContentLength() {
206 HttpServer.bind().then((server) { 206 HttpServer.bind().then((server) {
207 server.listen( 207 server.listen(
208 (HttpRequest request) { 208 (HttpRequest request) {
209 var response = request.response; 209 var response = request.response;
210 Expect.isNull(response.headers.value('content-length')); 210 Expect.isNull(response.headers.value('content-length'));
211 Expect.equals(-1, response.contentLength); 211 Expect.equals(-1, response.contentLength);
212 response.headers.set("content-length", 3); 212 response.headers.set("content-length", 3);
213 Expect.equals("3", response.headers.value('content-length')); 213 Expect.equals("3", response.headers.value('content-length'));
214 Expect.equals(3, response.contentLength); 214 Expect.equals(3, response.contentLength);
215 response.addString("xxx"); 215 response.write("xxx");
216 response.close(); 216 response.close();
217 }); 217 });
218 218
219 var client = new HttpClient(); 219 var client = new HttpClient();
220 client.get("127.0.0.1", server.port, "/") 220 client.get("127.0.0.1", server.port, "/")
221 .then((request) => request.close()) 221 .then((request) => request.close())
222 .then((response) { 222 .then((response) {
223 response.listen( 223 response.listen(
224 (_) { }, 224 (_) { },
225 onDone: () { 225 onDone: () {
226 client.close(); 226 client.close();
227 server.close(); 227 server.close();
228 }); 228 });
229 }); 229 });
230 }); 230 });
231 } 231 }
232 232
233 void main() { 233 void main() {
234 testNoBody(5, false); 234 testNoBody(5, false);
235 testNoBody(5, true); 235 testNoBody(5, true);
236 testBody(5, false); 236 testBody(5, false);
237 testBody(5, true); 237 testBody(5, true);
238 testBodyChunked(5, false); 238 testBodyChunked(5, false);
239 testBodyChunked(5, true); 239 testBodyChunked(5, true);
240 testSetContentLength(); 240 testSetContentLength();
241 } 241 }
OLDNEW
« no previous file with comments | « tests/standalone/io/http_connection_close_test.dart ('k') | tests/standalone/io/http_detach_socket_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698