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

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

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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) 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 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 HttpServer server = new HttpServer(); 9 var errors = 0;
10 server.onError = (e) => Expect.fail("Unexpected error $e"); 10 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) {
11 server.listen("127.0.0.1", 0, backlog: totalConnections); 11 server.listen(
12 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { 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 response.contentLength = 0; 15 var response = request.response;
16 OutputStream stream = response.outputStream; 16 response.contentLength = 0;
17 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); 17 response.done
18 stream.close(); 18 .then((_) {
19 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); 19 Expect.fail("Unexpected successful response completion");
20 }; 20 })
21 21 .catchError((e) {
22 int count = 0; 22 Expect.isTrue(e.error is HttpException);
23 HttpClient client = new HttpClient(); 23 });
24 for (int i = 0; i < totalConnections; i++) { 24 // addString with content length 0 closes the connection and
25 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); 25 // reports an error.
26 conn.onError = (e) => Expect.fail("Unexpected error $e"); 26 response.addString("x");
27 conn.onRequest = (HttpClientRequest request) { 27 // Subsequent addString are ignored as there is already an
28 OutputStream stream = request.outputStream; 28 // error.
29 if (explicitContentLength) { 29 response.addString("x");
30 request.contentLength = 0; 30 // After an explicit close, addString becomes a state error
31 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); 31 // because we have said we will not add more.
32 } 32 response.close();
33 stream.close(); 33 Expect.throws(() => response.addString("x"),
34 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); 34 (e) => e is StateError);
35 }; 35 },
36 conn.onResponse = (HttpClientResponse response) { 36 onError: (e) {
37 Expect.equals("0", response.headers.value('content-length')); 37 Expect.fail("Unexpected server error $e");
38 Expect.equals(0, response.contentLength); 38 });
39 response.inputStream.onData = response.inputStream.read; 39
40 response.inputStream.onClosed = () { 40 int count = 0;
41 if (++count == totalConnections) { 41 HttpClient client = new HttpClient();
42 client.shutdown(); 42 for (int i = 0; i < totalConnections; i++) {
43 server.close(); 43 client.get("127.0.0.1", server.port, "/")
44 } 44 .then((request) {
45 }; 45 if (explicitContentLength) {
46 }; 46 request.contentLength = 0;
47 } 47 }
48 return request.close();
49 })
50 .then((response) {
51 Expect.equals("0", response.headers.value('content-length'));
52 Expect.equals(0, response.contentLength);
53 response.listen(
54 (d) {},
55 onDone: () {
56 if (++count == totalConnections) {
57 client.close();
58 server.close();
59 }
60 });
61 })
62 .catchError((e) {
63 Expect.fail("Unexpected error $e");
64 });
65 }
66 });
48 } 67 }
49 68
50 void testBody(int totalConnections, bool useHeader) { 69 void testBody(int totalConnections, bool useHeader) {
51 HttpServer server = new HttpServer(); 70 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) {
52 server.onError = (e) => Expect.fail("Unexpected error $e"); 71 server.listen(
53 server.listen("127.0.0.1", 0, backlog: totalConnections); 72 (HttpRequest request) {
54 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { 73 Expect.equals("2", request.headers.value('content-length'));
55 Expect.equals("2", request.headers.value('content-length')); 74 Expect.equals(2, request.contentLength);
56 Expect.equals(2, request.contentLength); 75 var response = request.response;
57 if (useHeader) { 76 if (useHeader) {
58 response.contentLength = 2; 77 response.contentLength = 2;
59 } else { 78 } else {
60 response.headers.set("content-length", 2); 79 response.headers.set("content-length", 2);
80 }
81 request.listen(
82 (d) {},
83 onDone: () {
84 response.addString("x");
85 Expect.throws(() => response.contentLength = 3,
86 (e) => e is HttpException);
87 response.addString("x");
88 response.addString("x");
89 response.done
90 .then((_) {
91 Expect.fail("Unexpected successful response completion");
92 })
93 .catchError((e) {
94 Expect.isTrue(e.error is HttpException);
95 });
96 response.close();
97 Expect.throws(() => response.addString("x"),
98 (e) => e is StateError);
99 });
100 },
101 onError: (e) => Expect.fail("Unexpected error $e"));
102
103 int count = 0;
104 HttpClient client = new HttpClient();
105 for (int i = 0; i < totalConnections; i++) {
106 client.get("127.0.0.1", server.port, "/")
107 .then((request) {
108 if (useHeader) {
109 request.contentLength = 2;
110 } else {
111 request.headers.add(HttpHeaders.CONTENT_LENGTH, "7");
112 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
113 }
114 request.addString("x");
115 Expect.throws(() => request.contentLength = 3,
116 (e) => e is HttpException);
117 request.addString("x");
118 return request.close();
119 })
120 .then((response) {
121 Expect.equals("2", response.headers.value('content-length'));
122 Expect.equals(2, response.contentLength);
123 response.listen(
124 (d) {},
125 onDone: () {
126 if (++count == totalConnections) {
127 client.close();
128 server.close();
129 }
130 });
131 });
61 } 132 }
62 request.inputStream.onData = request.inputStream.read; 133 });
63 request.inputStream.onClosed = () {
64 OutputStream stream = response.outputStream;
65 stream.writeString("x");
66 Expect.throws(() => response.contentLength = 3, (e) => e is HttpException) ;
67 stream.writeString("x");
68 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException);
69 stream.close();
70 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException);
71 };
72 };
73
74 int count = 0;
75 HttpClient client = new HttpClient();
76 for (int i = 0; i < totalConnections; i++) {
77 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
78 conn.onError = (e) => Expect.fail("Unexpected error $e");
79 conn.onRequest = (HttpClientRequest request) {
80 if (useHeader) {
81 request.contentLength = 2;
82 } else {
83 request.headers.add(HttpHeaders.CONTENT_LENGTH, "7");
84 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
85 }
86 OutputStream stream = request.outputStream;
87 stream.writeString("x");
88 Expect.throws(() => request.contentLength = 3, (e) => e is HttpException);
89 stream.writeString("x");
90 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException);
91 stream.close();
92 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException);
93 };
94 conn.onResponse = (HttpClientResponse response) {
95 Expect.equals("2", response.headers.value('content-length'));
96 Expect.equals(2, response.contentLength);
97 response.inputStream.onData = response.inputStream.read;
98 response.inputStream.onClosed = () {
99 if (++count == totalConnections) {
100 client.shutdown();
101 server.close();
102 }
103 };
104 };
105 }
106 } 134 }
107 135
108 void testBodyChunked(int totalConnections, bool useHeader) { 136 void testBodyChunked(int totalConnections, bool useHeader) {
109 HttpServer server = new HttpServer(); 137 HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) {
110 server.onError = (e) => Expect.fail("Unexpected error $e"); 138 server.listen(
111 server.listen("127.0.0.1", 0, backlog: totalConnections); 139 (HttpRequest request) {
112 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { 140 Expect.isNull(request.headers.value('content-length'));
113 Expect.isNull(request.headers.value('content-length')); 141 Expect.equals(-1, request.contentLength);
114 Expect.equals(-1, request.contentLength); 142 var response = request.response;
115 if (useHeader) { 143 if (useHeader) {
116 response.contentLength = 2; 144 response.contentLength = 2;
117 response.headers.chunkedTransferEncoding = true; 145 response.headers.chunkedTransferEncoding = true;
118 } else { 146 } else {
119 response.headers.set("content-length", 2); 147 response.headers.set("content-length", 2);
120 response.headers.set("transfer-encoding", "chunked"); 148 response.headers.set("transfer-encoding", "chunked");
149 }
150 request.listen(
151 (d) {},
152 onDone: () {
153 response.addString("x");
154 Expect.throws(
155 () => response.headers.chunkedTransferEncoding = false,
156 (e) => e is HttpException);
157 response.addString("x");
158 response.addString("x");
159 response.close();
160 Expect.throws(() => response.addString("x"),
161 (e) => e is StateError);
162 });
163 },
164 onError: (e) => Expect.fail("Unexpected error $e"));
165
166 int count = 0;
167 HttpClient client = new HttpClient();
168 for (int i = 0; i < totalConnections; i++) {
169 client.get("127.0.0.1", server.port, "/")
170 .then((request) {
171 if (useHeader) {
172 request.contentLength = 2;
173 request.headers.chunkedTransferEncoding = true;
174 } else {
175 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
176 request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
177 }
178 request.addString("x");
179 Expect.throws(() => request.headers.chunkedTransferEncoding = false,
180 (e) => e is HttpException);
181 request.addString("x");
182 request.addString("x");
183 return request.close();
184 })
185 .then((response) {
186 Expect.isNull(response.headers.value('content-length'));
187 Expect.equals(-1, response.contentLength);
188 response.listen(
189 (d) {},
190 onDone: () {
191 if (++count == totalConnections) {
192 client.close();
193 server.close();
194 }
195 });
196 })
197 .catchError((e) => Expect.fail("Unexpected error $e"));
121 } 198 }
122 request.inputStream.onData = request.inputStream.read; 199 });
123 request.inputStream.onClosed = () {
124 OutputStream stream = response.outputStream;
125 stream.writeString("x");
126 Expect.throws(() => response.headers.chunkedTransferEncoding = false,
127 (e) => e is HttpException);
128 stream.writeString("x");
129 stream.writeString("x");
130 stream.close();
131 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException);
132 };
133 };
134
135 int count = 0;
136 HttpClient client = new HttpClient();
137 for (int i = 0; i < totalConnections; i++) {
138 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
139 conn.onError = (e) => Expect.fail("Unexpected error $e");
140 conn.onRequest = (HttpClientRequest request) {
141 if (useHeader) {
142 request.contentLength = 2;
143 request.headers.chunkedTransferEncoding = true;
144 } else {
145 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2");
146 request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
147 }
148 OutputStream stream = request.outputStream;
149 stream.writeString("x");
150 Expect.throws(() => request.headers.chunkedTransferEncoding = false,
151 (e) => e is HttpException);
152 stream.writeString("x");
153 stream.writeString("x");
154 stream.close();
155 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException);
156 };
157 conn.onResponse = (HttpClientResponse response) {
158 Expect.isNull(response.headers.value('content-length'));
159 Expect.equals(-1, response.contentLength);
160 response.inputStream.onData = response.inputStream.read;
161 response.inputStream.onClosed = () {
162 if (++count == totalConnections) {
163 client.shutdown();
164 server.close();
165 }
166 };
167 };
168 }
169 } 200 }
170 201
171 void testHttp10() { 202 void testHttp10() {
172 HttpServer server = new HttpServer(); 203 HttpServer.bind("127.0.0.1", 0, 5).then((server) {
173 server.onError = (e) => Expect.fail("Unexpected error $e"); 204 server.listen(
174 server.listen("127.0.0.1", 0, backlog: 5); 205 (HttpRequest request) {
175 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { 206 Expect.isNull(request.headers.value('content-length'));
176 Expect.isNull(request.headers.value('content-length')); 207 Expect.equals(-1, request.contentLength);
177 Expect.equals(-1, request.contentLength); 208 var response = request.response;
178 response.contentLength = 0; 209 response.contentLength = 0;
179 OutputStream stream = response.outputStream; 210 Expect.equals("1.0", request.protocolVersion);
180 Expect.equals("1.0", request.protocolVersion); 211 response.done
181 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); 212 .then((_) => Expect.fail("Unexpected response completion"))
182 stream.close(); 213 .catchError((e) => Expect.isTrue(e.error is HttpException));
183 }; 214 response.addString("x");
184 215 response.close();
185 Socket socket = new Socket("127.0.0.1", server.port); 216 Expect.throws(() => response.addString("x"),
186 socket.onConnect = () { 217 (e) => e is StateError);
187 List<int> buffer = new List<int>.fixedLength(1024); 218 },
188 socket.outputStream.writeString("GET / HTTP/1.0\r\n\r\n"); 219 onError: (e) => Expect.fail("Unexpected error $e"));
189 socket.onData = () => socket.readList(buffer, 0, buffer.length); 220
190 socket.onClosed = () { 221 Socket.connect("127.0.0.1", server.port).then((socket) {
191 socket.close(true); 222 socket.addString("GET / HTTP/1.0\r\n\r\n");
192 server.close(); 223 socket.close();
193 }; 224 socket.listen(
194 }; 225 (d) { },
226 onDone: () {
227 socket.destroy();
228 server.close();
229 });
230 });
231 });
195 } 232 }
196 233
197 void main() { 234 void main() {
198 testNoBody(5, false); 235 testNoBody(5, false);
199 testNoBody(5, true); 236 testNoBody(5, true);
200 testBody(5, false); 237 testBody(5, false);
201 testBody(5, true); 238 testBody(5, true);
202 testBodyChunked(5, false); 239 testBodyChunked(5, false);
203 testBodyChunked(5, true); 240 testBodyChunked(5, true);
204 testHttp10(); 241 testHttp10();
205 } 242 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698