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

Side by Side Diff: pkg/http_server/test/http_body_test.dart

Issue 18438005: Move MimeMultipartTransformer and HttpBodyHandler to mime and http_server packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removed _BufferList copy and MimeMultipartException reference in pub. Created 7 years, 5 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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 import 'dart:io'; 5 import 'dart:io';
6 import 'dart:utf'; 6 import 'dart:utf';
7 7
8 import 'package:expect/expect.dart'; 8 import 'package:http_server/http_server.dart';
9 import 'package:unittest/unittest.dart';
9 10
10 void testHttpClientResponseBody() { 11 void testHttpClientResponseBody() {
11 void test(String mimeType, 12 void test(String mimeType,
12 List<int> content, 13 List<int> content,
13 dynamic expectedBody, 14 dynamic expectedBody,
14 String type, 15 String type,
15 [bool shouldFail = false]) { 16 [bool shouldFail = false]) {
16 HttpServer.bind("127.0.0.1", 0).then((server) { 17 HttpServer.bind("127.0.0.1", 0).then((server) {
17 server.listen((request) { 18 server.listen((request) {
18 request.listen( 19 request.listen(
19 (_) {}, 20 (_) {},
20 onDone: () { 21 onDone: () {
21 request.response.headers.contentType = 22 request.response.headers.contentType =
22 ContentType.parse(mimeType); 23 ContentType.parse(mimeType);
23 request.response.add(content); 24 request.response.add(content);
24 request.response.close(); 25 request.response.close();
25 }); 26 });
26 }); 27 });
27 28
28 var client = new HttpClient(); 29 var client = new HttpClient();
29 client.get("127.0.0.1", server.port, "/") 30 client.get("127.0.0.1", server.port, "/")
30 .then((request) => request.close()) 31 .then((request) => request.close())
31 .then(HttpBodyHandler.processResponse) 32 .then(HttpBodyHandler.processResponse)
32 .then((body) { 33 .then((body) {
33 if (shouldFail) Expect.fail("Error expected"); 34 if (shouldFail) throw "Error expected";
Bill Hesse 2013/07/12 11:43:33 expect(shouldFail, isFalse)
Anders Johnsen 2013/07/15 08:21:32 Done.
34 Expect.equals(type, body.type); 35 expect(body.type, equals(type));
35 Expect.isNotNull(body.response); 36 expect(body.response, isNotNull);
36 switch (type) { 37 switch (type) {
37 case "text": 38 case "text":
38 Expect.equals(expectedBody, body.body);
39 break;
40
41 case "json": 39 case "json":
42 Expect.mapEquals(expectedBody, body.body); 40 expect(body.body, equals(expectedBody));
43 break; 41 break;
44 42
45 default: 43 default:
46 Expect.fail("bad body type"); 44 throw "bad body type";
Bill Hesse 2013/07/12 11:43:33 fail("bad body type");
Anders Johnsen 2013/07/15 08:21:32 Done.
47 } 45 }
48 }, onError: (error) { 46 }, onError: (error) {
49 if (!shouldFail) throw error; 47 if (!shouldFail) throw error;
50 }) 48 })
51 .whenComplete(() { 49 .whenComplete(() {
52 client.close(); 50 client.close();
53 server.close(); 51 server.close();
54 }); 52 });
55 }); 53 });
56 } 54 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 void testHttpServerRequestBody() { 86 void testHttpServerRequestBody() {
89 void test(String mimeType, 87 void test(String mimeType,
90 List<int> content, 88 List<int> content,
91 dynamic expectedBody, 89 dynamic expectedBody,
92 String type, 90 String type,
93 {bool shouldFail: false, 91 {bool shouldFail: false,
94 Encoding defaultEncoding: Encoding.UTF_8}) { 92 Encoding defaultEncoding: Encoding.UTF_8}) {
95 HttpServer.bind("127.0.0.1", 0).then((server) { 93 HttpServer.bind("127.0.0.1", 0).then((server) {
96 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding)) 94 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding))
97 .listen((body) { 95 .listen((body) {
98 if (shouldFail) Expect.fail("Error expected"); 96 if (shouldFail) throw "Error expected";
Bill Hesse 2013/07/12 11:43:33 ditto
Anders Johnsen 2013/07/15 08:21:32 Done.
99 Expect.equals(type, body.type); 97 expect(body.type, equals(type));
100 switch (type) { 98 switch (type) {
101 case "text": 99 case "text":
102 Expect.equals(body.contentType.mimeType, "text/plain"); 100 expect(body.contentType.mimeType, equals("text/plain"));
103 Expect.equals(expectedBody, body.body); 101 expect(body.body, equals(expectedBody));
104 break; 102 break;
105 103
106 case "json": 104 case "json":
107 Expect.equals(body.contentType.mimeType, "application/json"); 105 expect(body.contentType.mimeType, equals("application/json"));
108 Expect.mapEquals(expectedBody, body.body); 106 expect(body.body, equals(expectedBody));
109 break; 107 break;
110 108
111 case "binary": 109 case "binary":
112 Expect.equals(body.contentType, null); 110 expect(body.contentType, isNull);
113 Expect.listEquals(expectedBody, body.body); 111 expect(body.body, equals(expectedBody));
114 break; 112 break;
115 113
116 case "form": 114 case "form":
117 var mimeType = body.contentType.mimeType; 115 var mimeType = body.contentType.mimeType;
118 Expect.isTrue( 116 expect(mimeType,
119 mimeType == 'multipart/form-data' || 117 anyOf(equals('multipart/form-data'),
120 mimeType == 'application/x-www-form-urlencoded'); 118 equals('application/x-www-form-urlencoded')));
121 Expect.setEquals(expectedBody.keys.toSet(), 119 expect(body.body.keys.toSet(),
122 body.body.keys.toSet()); 120 equals(expectedBody.keys.toSet()));
123 for (var key in expectedBody.keys) { 121 for (var key in expectedBody.keys) {
Bill Hesse 2013/07/12 11:43:33 var found = body.body[key]; var expected = expecte
Anders Johnsen 2013/07/15 08:21:32 Done.
124 if (body.body[key] is HttpBodyFileUpload) { 122 if (body.body[key] is HttpBodyFileUpload) {
125 Expect.equals(expectedBody[key]['contentType'], 123 expect(body.body[key].contentType.toString(),
126 body.body[key].contentType.toString()); 124 equals(expectedBody[key]['contentType']));
127 Expect.equals(expectedBody[key]['filename'], 125 expect(body.body[key].filename,
128 body.body[key].filename); 126 equals(expectedBody[key]['filename']));
129 if (body.body[key].content is String) { 127 expect(body.body[key].content,
130 Expect.equals(expectedBody[key]['content'], 128 equals(expectedBody[key]['content']));
131 body.body[key].content);
132 } else {
133 Expect.listEquals(expectedBody[key]['content'],
134 body.body[key].content);
135 }
136 } else { 129 } else {
137 Expect.equals(expectedBody[key], body.body[key]); 130 expect(body.body[key], equals(expectedBody[key]));
138 } 131 }
139 } 132 }
140 break; 133 break;
141 134
142 default: 135 default:
143 Expect.fail("bad body type"); 136 throw "bad body type";
144 } 137 }
145 body.response.close(); 138 body.response.close();
146 }, onError: (error) { 139 }, onError: (error) {
147 if (!shouldFail) throw error; 140 if (!shouldFail) throw error;
148 }); 141 });
149 142
150 var client = new HttpClient(); 143 var client = new HttpClient();
151 client.post("127.0.0.1", server.port, "/") 144 client.post("127.0.0.1", server.port, "/")
152 .then((request) { 145 .then((request) {
153 if (mimeType != null) { 146 if (mimeType != null) {
154 request.headers.contentType = 147 request.headers.contentType =
155 ContentType.parse(mimeType); 148 ContentType.parse(mimeType);
156 } 149 }
157 request.add(content); 150 request.add(content);
158 return request.close(); 151 return request.close();
159 }) 152 })
160 .then((response) { 153 .then((response) {
161 if (shouldFail) { 154 if (shouldFail) {
162 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); 155 expect(response.statusCode, equals(HttpStatus.BAD_REQUEST));
163 } 156 }
164 response.fold(null, (x, y) {}); 157 response.fold(null, (x, y) {});
165 client.close(); 158 client.close();
166 server.close(); 159 server.close();
167 }); 160 });
168 }); 161 });
169 } 162 }
170 163
171 test("text/plain", "body".codeUnits, "body", "text"); 164 test("text/plain", "body".codeUnits, "body", "text");
172 test("text/plain; charset=utf-8", 165 test("text/plain; charset=utf-8",
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 test('application/x-www-form-urlencoded', 324 test('application/x-www-form-urlencoded',
332 'name=%C8%A4%C8%A4'.codeUnits, 325 'name=%C8%A4%C8%A4'.codeUnits,
333 { 'name' : 'ȤȤ' }, 326 { 'name' : 'ȤȤ' },
334 "form"); 327 "form");
335 } 328 }
336 329
337 void main() { 330 void main() {
338 testHttpClientResponseBody(); 331 testHttpClientResponseBody();
339 testHttpServerRequestBody(); 332 testHttpServerRequestBody();
340 } 333 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698