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