Chromium Code Reviews| Index: tests/standalone/io/http_body_test.dart |
| diff --git a/tests/standalone/io/http_body_test.dart b/tests/standalone/io/http_body_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cf8cc0c34899e8c17da928b961e4e64eda04b641 |
| --- /dev/null |
| +++ b/tests/standalone/io/http_body_test.dart |
| @@ -0,0 +1,150 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'dart:io'; |
| +import 'dart:utf'; |
| + |
| +import 'package:expect/expect.dart'; |
| + |
| +void testHttpClientResponseBody() { |
| + void test(String mimeType, |
| + List<int> content, |
| + dynamic expectedBody, |
| + int type, |
| + [bool shouldFail = false]) { |
| + HttpServer.bind().then((server) { |
| + server.listen((request) { |
| + request.listen( |
| + (_) {}, |
| + onDone: () { |
| + request.response.headers.contentType = |
| + new ContentType.fromString(mimeType); |
| + request.response.writeBytes(content); |
| + request.response.close(); |
| + }); |
| + }); |
| + |
| + var client = new HttpClient(); |
| + client.get("127.0.0.1", server.port, "/") |
| + .then((request) => request.close()) |
| + .then(HttpBodyHandler.processResponse) |
| + .then((body) { |
| + if (shouldFail) Expect.fail("Error expected"); |
| + Expect.equals(type, body.type); |
| + switch (type) { |
| + case HttpBody.TEXT: |
| + Expect.equals(expectedBody, body.body); |
| + Expect.equals(expectedBody, body.text); |
| + break; |
| + |
| + case HttpBody.JSON: |
| + Expect.mapEquals(expectedBody, body.body); |
| + Expect.mapEquals(expectedBody, body.json); |
| + break; |
| + |
| + default: |
| + Expect.fail("bad body type"); |
| + } |
| + }, onError: (error) { |
| + if (!shouldFail) Expect.fail("Error unexpected"); |
| + }) |
| + .whenComplete(() { |
| + client.close(); |
| + server.close(); |
| + }); |
| + }); |
| + } |
| + test("text/plain", "body".codeUnits, "body", HttpBody.TEXT); |
| + 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.
|
| + test("text/plain; charset=utf-8", [42], "*", HttpBody.TEXT); |
| + test("text/plain; charset=us-ascii", [142], "?", HttpBody.TEXT); |
| + test("text/plain; charset=utf-8", |
| + [142], |
| + new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), |
| + HttpBody.TEXT); |
| + |
| + test("application/json", |
| + '{"val": 5}'.codeUnits, |
| + { "val" : 5 }, |
| + HttpBody.JSON); |
| + test("application/json", |
| + '{ bad json }'.codeUnits, |
| + null, |
| + HttpBody.JSON, |
| + true); |
| +} |
| + |
| +void testHttpServerRequestBody() { |
| + void test(String mimeType, |
| + List<int> content, |
| + dynamic expectedBody, |
| + int type, |
| + [bool shouldFail = false]) { |
| + HttpServer.bind().then((server) { |
|
Søren Gjesse
2013/04/10 07:09:11
Shouldn't we have two modes for this test. One tha
|
| + server.transform(new HttpBodyHandler()) |
| + .listen((body) { |
| + if (shouldFail) Expect.fail("Error expected"); |
| + Expect.equals(type, body.type); |
| + switch (type) { |
| + case HttpBody.TEXT: |
| + Expect.equals(expectedBody, body.body); |
| + Expect.equals(expectedBody, body.text); |
| + break; |
| + |
| + case HttpBody.JSON: |
| + Expect.mapEquals(expectedBody, body.body); |
| + Expect.mapEquals(expectedBody, body.json); |
| + break; |
| + |
| + default: |
| + Expect.fail("bad body type"); |
| + } |
| + body.response.close(); |
| + }, onError: (error) { |
| + if (!shouldFail) Expect.fail("Error unexpected"); |
| + }); |
| + |
| + var client = new HttpClient(); |
| + client.post("127.0.0.1", server.port, "/") |
| + .then((request) { |
| + request.headers.contentType = |
| + new ContentType.fromString(mimeType); |
| + request.writeBytes(content); |
| + return request.close(); |
| + }) |
| + .then((response) { |
| + if (shouldFail) { |
| + Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| + } |
| + response.fold(null, (x, y) {}); |
| + client.close(); |
| + server.close(); |
| + }); |
| + }); |
| + } |
| + test("text/plain", "body".codeUnits, "body", HttpBody.TEXT); |
| + test("text/plain; charset=utf-8", "body".codeUnits, "body", HttpBody.TEXT); |
|
Søren Gjesse
2013/04/10 07:09:11
Ditto.
|
| + test("text/plain; charset=utf-8", [42], "*", HttpBody.TEXT); |
| + test("text/plain; charset=us-ascii", [142], "?", HttpBody.TEXT); |
| + test("text/plain; charset=utf-8", |
| + [142], |
| + new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), |
| + HttpBody.TEXT); |
| + |
| + test("application/json", |
| + '{"val": 5}'.codeUnits, |
| + { "val" : 5 }, |
| + HttpBody.JSON); |
| + test("application/json", |
| + '{ bad json }'.codeUnits, |
| + null, |
| + HttpBody.JSON, |
| + true); |
| +} |
| + |
| + |
| +void main() { |
| + testHttpClientResponseBody(); |
| + testHttpServerRequestBody(); |
| +} |