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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/iolib_sources.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 new HttpBodyHandler();
12 void test(String mimeType,
13 List<int> content,
14 dynamic expectedBody,
15 String type,
16 [bool shouldFail = false]) {
17 HttpServer.bind().then((server) {
18 server.listen((request) {
19 request.listen(
20 (_) {},
21 onDone: () {
22 request.response.headers.contentType =
23 new ContentType.fromString(mimeType);
24 request.response.writeBytes(content);
25 request.response.close();
26 });
27 });
28
29 var client = new HttpClient();
30 client.get("127.0.0.1", server.port, "/")
31 .then((request) => request.close())
32 .then(HttpBodyHandler.processResponse)
33 .then((body) {
34 if (shouldFail) Expect.fail("Error expected");
35 Expect.equals(type, body.type);
36 switch (type) {
37 case "text":
38 Expect.equals(expectedBody, body.body);
39 break;
40
41 case "json":
42 Expect.mapEquals(expectedBody, body.body);
43 break;
44
45 default:
46 Expect.fail("bad body type");
47 }
48 }, onError: (error) {
49 if (!shouldFail) Expect.fail("Error unexpected");
50 })
51 .whenComplete(() {
52 client.close();
53 server.close();
54 });
55 });
56 }
57 test("text/plain", "body".codeUnits, "body", "text");
58 test("text/plain; charset=utf-8",
59 "body".codeUnits,
60 "body",
61 "text");
62 test("text/plain; charset=iso-8859-1",
63 "body".codeUnits,
64 "body",
65 "text");
66 test("text/plain; charset=us-ascii",
67 "body".codeUnits,
68 "body",
69 "text");
70 test("text/plain; charset=utf-8", [42], "*", "text");
71 test("text/plain; charset=us-ascii", [142], "?", "text");
72 test("text/plain; charset=utf-8",
73 [142],
74 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]),
75 "text");
76
77 test("application/json",
78 '{"val": 5}'.codeUnits,
79 { "val" : 5 },
80 "json");
81 test("application/json",
82 '{ bad json }'.codeUnits,
83 null,
84 "json",
85 true);
86 }
87
88 void testHttpServerRequestBody() {
89 void test(String mimeType,
90 List<int> content,
91 dynamic expectedBody,
92 String type,
93 [bool shouldFail = false]) {
94 HttpServer.bind().then((server) {
95 server.transform(new HttpBodyHandler())
96 .listen((body) {
97 if (shouldFail) Expect.fail("Error expected");
98 Expect.equals(type, body.type);
99 switch (type) {
100 case "text":
101 Expect.equals(expectedBody, body.body);
102 break;
103
104 case "json":
105 Expect.mapEquals(expectedBody, body.body);
106 break;
107
108 default:
109 Expect.fail("bad body type");
110 }
111 body.response.close();
112 }, onError: (error) {
113 if (!shouldFail) Expect.fail("Error unexpected");
114 });
115
116 var client = new HttpClient();
117 client.post("127.0.0.1", server.port, "/")
118 .then((request) {
119 request.headers.contentType =
120 new ContentType.fromString(mimeType);
121 request.writeBytes(content);
122 return request.close();
123 })
124 .then((response) {
125 if (shouldFail) {
126 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode);
127 }
128 response.fold(null, (x, y) {});
129 client.close();
130 server.close();
131 });
132 });
133 }
134 test("text/plain", "body".codeUnits, "body", "text");
135 test("text/plain; charset=utf-8",
136 "body".codeUnits,
137 "body",
138 "text");
139 test("text/plain; charset=utf-8", [42], "*", "text");
140 test("text/plain; charset=us-ascii", [142], "?", "text");
141 test("text/plain; charset=utf-8",
142 [142],
143 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]),
144 "text");
145
146 test("application/json",
147 '{"val": 5}'.codeUnits,
148 { "val" : 5 },
149 "json");
150 test("application/json",
151 '{ bad json }'.codeUnits,
152 null,
153 "json",
154 true);
155 }
156
157
158 void main() {
159 testHttpClientResponseBody();
160 testHttpServerRequestBody();
161 }
OLDNEW
« no previous file with comments | « sdk/lib/io/iolib_sources.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698