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

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: Remove import. 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 expect(shouldFail, isFalse);
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 fail("bad body type");
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 expect(shouldFail, isFalse);
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) {
124 if (body.body[key] is HttpBodyFileUpload) { 122 var found = body.body[key];
125 Expect.equals(expectedBody[key]['contentType'], 123 var expected = expectedBody[key];
126 body.body[key].contentType.toString()); 124 if (found is HttpBodyFileUpload) {
127 Expect.equals(expectedBody[key]['filename'], 125 expect(found.contentType.toString(),
128 body.body[key].filename); 126 equals(expected['contentType']));
129 if (body.body[key].content is String) { 127 expect(found.filename,
130 Expect.equals(expectedBody[key]['content'], 128 equals(expected['filename']));
131 body.body[key].content); 129 expect(found.content,
132 } else { 130 equals(expected['content']));
133 Expect.listEquals(expectedBody[key]['content'],
134 body.body[key].content);
135 }
136 } else { 131 } else {
137 Expect.equals(expectedBody[key], body.body[key]); 132 expect(found, equals(expected));
138 } 133 }
139 } 134 }
140 break; 135 break;
141 136
142 default: 137 default:
143 Expect.fail("bad body type"); 138 throw "bad body type";
144 } 139 }
145 body.response.close(); 140 body.response.close();
146 }, onError: (error) { 141 }, onError: (error) {
147 if (!shouldFail) throw error; 142 if (!shouldFail) throw error;
148 }); 143 });
149 144
150 var client = new HttpClient(); 145 var client = new HttpClient();
151 client.post("127.0.0.1", server.port, "/") 146 client.post("127.0.0.1", server.port, "/")
152 .then((request) { 147 .then((request) {
153 if (mimeType != null) { 148 if (mimeType != null) {
154 request.headers.contentType = 149 request.headers.contentType =
155 ContentType.parse(mimeType); 150 ContentType.parse(mimeType);
156 } 151 }
157 request.add(content); 152 request.add(content);
158 return request.close(); 153 return request.close();
159 }) 154 })
160 .then((response) { 155 .then((response) {
161 if (shouldFail) { 156 if (shouldFail) {
162 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); 157 expect(response.statusCode, equals(HttpStatus.BAD_REQUEST));
163 } 158 }
164 response.fold(null, (x, y) {}); 159 response.fold(null, (x, y) {});
165 client.close(); 160 client.close();
166 server.close(); 161 server.close();
167 }); 162 });
168 }); 163 });
169 } 164 }
170 165
171 test("text/plain", "body".codeUnits, "body", "text"); 166 test("text/plain", "body".codeUnits, "body", "text");
172 test("text/plain; charset=utf-8", 167 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', 326 test('application/x-www-form-urlencoded',
332 'name=%C8%A4%C8%A4'.codeUnits, 327 'name=%C8%A4%C8%A4'.codeUnits,
333 { 'name' : 'ȤȤ' }, 328 { 'name' : 'ȤȤ' },
334 "form"); 329 "form");
335 } 330 }
336 331
337 void main() { 332 void main() {
338 testHttpClientResponseBody(); 333 testHttpClientResponseBody();
339 testHttpServerRequestBody(); 334 testHttpServerRequestBody();
340 } 335 }
OLDNEW
« no previous file with comments | « pkg/http_server/lib/src/http_multipart_form_data_impl.dart ('k') | pkg/http_server/test/http_multipart_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698