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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 // Set the "Expires" header. | 147 // Set the "Expires" header. |
148 void _expires2Handler(HttpRequest request, HttpResponse response) { | 148 void _expires2Handler(HttpRequest request, HttpResponse response) { |
149 response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT"); | 149 response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT"); |
150 Date date = | 150 Date date = |
151 new Date.withTimeZone( | 151 new Date.withTimeZone( |
152 1999, Date.JUN, 11, 18, 46, 53, 0, new TimeZone.utc()); | 152 1999, Date.JUN, 11, 18, 46, 53, 0, new TimeZone.utc()); |
153 Expect.equals(date, response.headers.expires); | 153 Expect.equals(date, response.headers.expires); |
154 response.outputStream.close(); | 154 response.outputStream.close(); |
155 } | 155 } |
156 | 156 |
| 157 void _contentType1Handler(HttpRequest request, HttpResponse response) { |
| 158 Expect.equals("text/html", request.headers.contentType.value); |
| 159 Expect.equals("text", request.headers.contentType.primaryType); |
| 160 Expect.equals("html", request.headers.contentType.subType); |
| 161 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); |
| 162 |
| 163 ContentType contentType = new ContentType("text", "html"); |
| 164 contentType.parameters["charset"] = "utf-8"; |
| 165 response.headers.contentType = contentType; |
| 166 response.outputStream.close(); |
| 167 } |
| 168 |
| 169 void _contentType2Handler(HttpRequest request, HttpResponse response) { |
| 170 Expect.equals("text/html", request.headers.contentType.value); |
| 171 Expect.equals("text", request.headers.contentType.primaryType); |
| 172 Expect.equals("html", request.headers.contentType.subType); |
| 173 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); |
| 174 |
| 175 response.headers.set(HttpHeaders.CONTENT_TYPE, |
| 176 "text/html; charset = utf-8"); |
| 177 response.outputStream.close(); |
| 178 } |
| 179 |
157 void main() { | 180 void main() { |
158 // Setup request handlers. | 181 // Setup request handlers. |
159 _requestHandlers = new Map(); | 182 _requestHandlers = new Map(); |
160 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | 183 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
161 _echoHandler(request, response); | 184 _echoHandler(request, response); |
162 }; | 185 }; |
163 _requestHandlers["/0123456789"] = | 186 _requestHandlers["/0123456789"] = |
164 (HttpRequest request, HttpResponse response) { | 187 (HttpRequest request, HttpResponse response) { |
165 _zeroToTenHandler(request, response); | 188 _zeroToTenHandler(request, response); |
166 }; | 189 }; |
167 _requestHandlers["/reasonformoving"] = | 190 _requestHandlers["/reasonformoving"] = |
168 (HttpRequest request, HttpResponse response) { | 191 (HttpRequest request, HttpResponse response) { |
169 _reasonForMovingHandler(request, response); | 192 _reasonForMovingHandler(request, response); |
170 }; | 193 }; |
171 _requestHandlers["/host"] = | 194 _requestHandlers["/host"] = |
172 (HttpRequest request, HttpResponse response) { | 195 (HttpRequest request, HttpResponse response) { |
173 _hostHandler(request, response); | 196 _hostHandler(request, response); |
174 }; | 197 }; |
175 _requestHandlers["/expires1"] = | 198 _requestHandlers["/expires1"] = |
176 (HttpRequest request, HttpResponse response) { | 199 (HttpRequest request, HttpResponse response) { |
177 _expires1Handler(request, response); | 200 _expires1Handler(request, response); |
178 }; | 201 }; |
179 _requestHandlers["/expires2"] = | 202 _requestHandlers["/expires2"] = |
180 (HttpRequest request, HttpResponse response) { | 203 (HttpRequest request, HttpResponse response) { |
181 _expires2Handler(request, response); | 204 _expires2Handler(request, response); |
182 }; | 205 }; |
| 206 _requestHandlers["/contenttype1"] = |
| 207 (HttpRequest request, HttpResponse response) { |
| 208 _contentType1Handler(request, response); |
| 209 }; |
| 210 _requestHandlers["/contenttype2"] = |
| 211 (HttpRequest request, HttpResponse response) { |
| 212 _contentType2Handler(request, response); |
| 213 }; |
183 | 214 |
184 this.port.receive((var message, SendPort replyTo) { | 215 this.port.receive((var message, SendPort replyTo) { |
185 if (message.isStart) { | 216 if (message.isStart) { |
186 _server = new HttpServer(); | 217 _server = new HttpServer(); |
187 try { | 218 try { |
188 _server.listen("127.0.0.1", 0); | 219 _server.listen("127.0.0.1", 0); |
189 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | 220 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { |
190 _requestReceivedHandler(req, rsp); | 221 _requestReceivedHandler(req, rsp); |
191 }; | 222 }; |
192 replyTo.send(new TestServerStatus.started(_server.port), null); | 223 replyTo.send(new TestServerStatus.started(_server.port), null); |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 processResponse(response); | 538 processResponse(response); |
508 }; | 539 }; |
509 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); | 540 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); |
510 conn2.onResponse = (HttpClientResponse response) { | 541 conn2.onResponse = (HttpClientResponse response) { |
511 processResponse(response); | 542 processResponse(response); |
512 }; | 543 }; |
513 }); | 544 }); |
514 testServerMain.start(); | 545 testServerMain.start(); |
515 } | 546 } |
516 | 547 |
| 548 void testContentType() { |
| 549 TestServerMain testServerMain = new TestServerMain(); |
| 550 testServerMain.setServerStartedHandler((int port) { |
| 551 int responses = 0; |
| 552 HttpClient httpClient = new HttpClient(); |
| 553 |
| 554 void processResponse(HttpClientResponse response) { |
| 555 Expect.equals(HttpStatus.OK, response.statusCode); |
| 556 Expect.equals("text/html; charset=utf-8", |
| 557 response.headers.contentType.toString()); |
| 558 Expect.equals("text/html", response.headers.contentType.value); |
| 559 Expect.equals("text", response.headers.contentType.primaryType); |
| 560 Expect.equals("html", response.headers.contentType.subType); |
| 561 Expect.equals("utf-8", |
| 562 response.headers.contentType.parameters["charset"]); |
| 563 responses++; |
| 564 if (responses == 2) { |
| 565 httpClient.shutdown(); |
| 566 testServerMain.shutdown(); |
| 567 } |
| 568 } |
| 569 |
| 570 HttpClientConnection conn1 = |
| 571 httpClient.get("127.0.0.1", port, "/contenttype1"); |
| 572 conn1.onRequest = (HttpClientRequest request) { |
| 573 ContentType contentType = new ContentType(); |
| 574 contentType.value = "text/html"; |
| 575 contentType.parameters["charset"] = "utf-8"; |
| 576 request.headers.contentType = contentType; |
| 577 request.outputStream.close(); |
| 578 }; |
| 579 conn1.onResponse = (HttpClientResponse response) { |
| 580 processResponse(response); |
| 581 }; |
| 582 HttpClientConnection conn2 = |
| 583 httpClient.get("127.0.0.1", port, "/contenttype2"); |
| 584 conn2.onRequest = (HttpClientRequest request) { |
| 585 request.headers.set(HttpHeaders.CONTENT_TYPE, |
| 586 "text/html; charset = utf-8"); |
| 587 request.outputStream.close(); |
| 588 }; |
| 589 conn2.onResponse = (HttpClientResponse response) { |
| 590 processResponse(response); |
| 591 }; |
| 592 }); |
| 593 testServerMain.start(); |
| 594 } |
| 595 |
517 | 596 |
518 void main() { | 597 void main() { |
519 testStartStop(); | 598 testStartStop(); |
520 testGET(); | 599 testGET(); |
521 testPOST(true); | 600 testPOST(true); |
522 testPOST(false); | 601 testPOST(false); |
523 testReadInto(true); | 602 testReadInto(true); |
524 testReadInto(false); | 603 testReadInto(false); |
525 testReadShort(true); | 604 testReadShort(true); |
526 testReadShort(false); | 605 testReadShort(false); |
527 test404(); | 606 test404(); |
528 testReasonPhrase(); | 607 testReasonPhrase(); |
529 testHost(); | 608 testHost(); |
530 testExpires(); | 609 testExpires(); |
| 610 testContentType(); |
531 } | 611 } |
OLD | NEW |