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

Unified Diff: tests/standalone/io/http_body_test.dart

Issue 14019002: Introduce new HttpBodyHandler to easily extract full body of HttpRequest and HttpClientResponse. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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 side-by-side diff with in-line comments
Download patch
« sdk/lib/io/http_body.dart ('K') | « sdk/lib/io/iolib_sources.gypi ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..a25945bfd896ce2d22f261a84e668247c496f115
--- /dev/null
+++ b/tests/standalone/io/http_body_test.dart
@@ -0,0 +1,160 @@
+// 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,
+ HttpBodyType 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 HttpBodyType.TEXT:
+ Expect.equals(expectedBody, body.body);
+ break;
+
+ case HttpBodyType.JSON:
+ Expect.mapEquals(expectedBody, body.body);
+ 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", HttpBodyType.TEXT);
+ test("text/plain; charset=utf-8",
+ "body".codeUnits,
+ "body",
+ HttpBodyType.TEXT);
+ test("text/plain; charset=iso-8859-1",
+ "body".codeUnits,
+ "body",
+ HttpBodyType.TEXT);
+ test("text/plain; charset=us-ascii",
+ "body".codeUnits,
+ "body",
+ HttpBodyType.TEXT);
+ test("text/plain; charset=utf-8", [42], "*", HttpBodyType.TEXT);
+ test("text/plain; charset=us-ascii", [142], "?", HttpBodyType.TEXT);
+ test("text/plain; charset=utf-8",
+ [142],
+ new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]),
+ HttpBodyType.TEXT);
+
+ test("application/json",
+ '{"val": 5}'.codeUnits,
+ { "val" : 5 },
+ HttpBodyType.JSON);
+ test("application/json",
+ '{ bad json }'.codeUnits,
+ null,
+ HttpBodyType.JSON,
+ true);
+}
+
+void testHttpServerRequestBody() {
+ void test(String mimeType,
+ List<int> content,
+ dynamic expectedBody,
+ int type,
+ [bool shouldFail = false]) {
+ HttpServer.bind().then((server) {
+ server.transform(new HttpBodyHandler())
+ .listen((body) {
+ if (shouldFail) Expect.fail("Error expected");
+ Expect.equals(type, body.type);
+ switch (type) {
+ case HttpBodyType.TEXT:
+ Expect.equals(expectedBody, body.body);
+ break;
+
+ case HttpBodyType.JSON:
+ Expect.mapEquals(expectedBody, body.body);
+ 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", HttpBodyType.TEXT);
+ test("text/plain; charset=utf-8",
+ "body".codeUnits,
+ "body",
+ HttpBodyType.TEXT);
+ test("text/plain; charset=utf-8", [42], "*", HttpBodyType.TEXT);
+ test("text/plain; charset=us-ascii", [142], "?", HttpBodyType.TEXT);
+ test("text/plain; charset=utf-8",
+ [142],
+ new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]),
+ HttpBodyType.TEXT);
+
+ test("application/json",
+ '{"val": 5}'.codeUnits,
+ { "val" : 5 },
+ HttpBodyType.JSON);
+ test("application/json",
+ '{ bad json }'.codeUnits,
+ null,
+ HttpBodyType.JSON,
+ true);
+}
+
+
+void main() {
+ testHttpClientResponseBody();
+ testHttpServerRequestBody();
+}
« sdk/lib/io/http_body.dart ('K') | « sdk/lib/io/iolib_sources.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698