Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:io'; | |
| 6 import 'dart:utf'; | |
| 7 | |
| 8 import 'package:expect/expect.dart'; | |
| 9 | |
| 10 void testHttpClientResponseBody() { | |
| 11 void test(String mimeType, | |
| 12 List<int> content, | |
| 13 dynamic expectedBody, | |
| 14 int type, | |
| 15 [bool shouldFail = false]) { | |
| 16 HttpServer.bind().then((server) { | |
| 17 server.listen((request) { | |
| 18 request.listen( | |
| 19 (_) {}, | |
| 20 onDone: () { | |
| 21 request.response.headers.contentType = | |
| 22 new ContentType.fromString(mimeType); | |
| 23 request.response.writeBytes(content); | |
| 24 request.response.close(); | |
| 25 }); | |
| 26 }); | |
| 27 | |
| 28 var client = new HttpClient(); | |
| 29 client.get("127.0.0.1", server.port, "/") | |
| 30 .then((request) => request.close()) | |
| 31 .then(HttpBodyHandler.processResponse) | |
| 32 .then((body) { | |
| 33 if (shouldFail) Expect.fail("Error expected"); | |
| 34 Expect.equals(type, body.type); | |
| 35 switch (type) { | |
| 36 case HttpBody.TEXT: | |
| 37 Expect.equals(expectedBody, body.body); | |
| 38 Expect.equals(expectedBody, body.text); | |
| 39 break; | |
| 40 | |
| 41 case HttpBody.JSON: | |
| 42 Expect.mapEquals(expectedBody, body.body); | |
| 43 Expect.mapEquals(expectedBody, body.json); | |
| 44 break; | |
| 45 | |
| 46 default: | |
| 47 Expect.fail("bad body type"); | |
| 48 } | |
| 49 }, onError: (error) { | |
| 50 if (!shouldFail) Expect.fail("Error unexpected"); | |
| 51 }) | |
| 52 .whenComplete(() { | |
| 53 client.close(); | |
| 54 server.close(); | |
| 55 }); | |
| 56 }); | |
| 57 } | |
| 58 test("text/plain", "body".codeUnits, "body", HttpBody.TEXT); | |
| 59 test("text/plain; charset=utf-8", "body".codeUnits, "body", HttpBody.TEXT); | |
|
Søren Gjesse
2013/04/10 07:09:11
Test this with charset iso-8859-1 and us-ascii as
Anders Johnsen
2013/04/10 12:52:49
Done.
| |
| 60 test("text/plain; charset=utf-8", [42], "*", HttpBody.TEXT); | |
| 61 test("text/plain; charset=us-ascii", [142], "?", HttpBody.TEXT); | |
| 62 test("text/plain; charset=utf-8", | |
| 63 [142], | |
| 64 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), | |
| 65 HttpBody.TEXT); | |
| 66 | |
| 67 test("application/json", | |
| 68 '{"val": 5}'.codeUnits, | |
| 69 { "val" : 5 }, | |
| 70 HttpBody.JSON); | |
| 71 test("application/json", | |
| 72 '{ bad json }'.codeUnits, | |
| 73 null, | |
| 74 HttpBody.JSON, | |
| 75 true); | |
| 76 } | |
| 77 | |
| 78 void testHttpServerRequestBody() { | |
| 79 void test(String mimeType, | |
| 80 List<int> content, | |
| 81 dynamic expectedBody, | |
| 82 int type, | |
| 83 [bool shouldFail = false]) { | |
| 84 HttpServer.bind().then((server) { | |
|
Søren Gjesse
2013/04/10 07:09:11
Shouldn't we have two modes for this test. One tha
| |
| 85 server.transform(new HttpBodyHandler()) | |
| 86 .listen((body) { | |
| 87 if (shouldFail) Expect.fail("Error expected"); | |
| 88 Expect.equals(type, body.type); | |
| 89 switch (type) { | |
| 90 case HttpBody.TEXT: | |
| 91 Expect.equals(expectedBody, body.body); | |
| 92 Expect.equals(expectedBody, body.text); | |
| 93 break; | |
| 94 | |
| 95 case HttpBody.JSON: | |
| 96 Expect.mapEquals(expectedBody, body.body); | |
| 97 Expect.mapEquals(expectedBody, body.json); | |
| 98 break; | |
| 99 | |
| 100 default: | |
| 101 Expect.fail("bad body type"); | |
| 102 } | |
| 103 body.response.close(); | |
| 104 }, onError: (error) { | |
| 105 if (!shouldFail) Expect.fail("Error unexpected"); | |
| 106 }); | |
| 107 | |
| 108 var client = new HttpClient(); | |
| 109 client.post("127.0.0.1", server.port, "/") | |
| 110 .then((request) { | |
| 111 request.headers.contentType = | |
| 112 new ContentType.fromString(mimeType); | |
| 113 request.writeBytes(content); | |
| 114 return request.close(); | |
| 115 }) | |
| 116 .then((response) { | |
| 117 if (shouldFail) { | |
| 118 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | |
| 119 } | |
| 120 response.fold(null, (x, y) {}); | |
| 121 client.close(); | |
| 122 server.close(); | |
| 123 }); | |
| 124 }); | |
| 125 } | |
| 126 test("text/plain", "body".codeUnits, "body", HttpBody.TEXT); | |
| 127 test("text/plain; charset=utf-8", "body".codeUnits, "body", HttpBody.TEXT); | |
|
Søren Gjesse
2013/04/10 07:09:11
Ditto.
| |
| 128 test("text/plain; charset=utf-8", [42], "*", HttpBody.TEXT); | |
| 129 test("text/plain; charset=us-ascii", [142], "?", HttpBody.TEXT); | |
| 130 test("text/plain; charset=utf-8", | |
| 131 [142], | |
| 132 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), | |
| 133 HttpBody.TEXT); | |
| 134 | |
| 135 test("application/json", | |
| 136 '{"val": 5}'.codeUnits, | |
| 137 { "val" : 5 }, | |
| 138 HttpBody.JSON); | |
| 139 test("application/json", | |
| 140 '{ bad json }'.codeUnits, | |
| 141 null, | |
| 142 HttpBody.JSON, | |
| 143 true); | |
| 144 } | |
| 145 | |
| 146 | |
| 147 void main() { | |
| 148 testHttpClientResponseBody(); | |
| 149 testHttpServerRequestBody(); | |
| 150 } | |
| OLD | NEW |