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

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: 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";
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";
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";
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(
Bill Hesse 2013/07/03 11:30:27 expect(mimeType, anyOf(equals('multipart/form-data
Anders Johnsen 2013/07/12 10:45:57 Done.
119 mimeType == 'multipart/form-data' || 117 mimeType == 'multipart/form-data' ||
120 mimeType == 'application/x-www-form-urlencoded'); 118 mimeType == 'application/x-www-form-urlencoded',
121 Expect.setEquals(expectedBody.keys.toSet(), 119 isTrue);
122 body.body.keys.toSet()); 120 expect(body.body.keys.toSet(),
121 equals(expectedBody.keys.toSet()));
123 for (var key in expectedBody.keys) { 122 for (var key in expectedBody.keys) {
124 if (body.body[key] is HttpBodyFileUpload) { 123 if (body.body[key] is HttpBodyFileUpload) {
125 Expect.equals(expectedBody[key]['contentType'], 124 expect(body.body[key].contentType.toString(),
126 body.body[key].contentType.toString()); 125 equals(expectedBody[key]['contentType']));
127 Expect.equals(expectedBody[key]['filename'], 126 expect(body.body[key].filename,
128 body.body[key].filename); 127 equals(expectedBody[key]['filename']));
129 if (body.body[key].content is String) { 128 expect(body.body[key].content,
130 Expect.equals(expectedBody[key]['content'], 129 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 { 130 } else {
137 Expect.equals(expectedBody[key], body.body[key]); 131 expect(body.body[key], equals(expectedBody[key]));
138 } 132 }
139 } 133 }
140 break; 134 break;
141 135
142 default: 136 default:
143 Expect.fail("bad body type"); 137 throw "bad body type";
144 } 138 }
145 body.response.close(); 139 body.response.close();
146 }, onError: (error) { 140 }, onError: (error) {
147 if (!shouldFail) throw error; 141 if (!shouldFail) throw error;
148 }); 142 });
149 143
150 var client = new HttpClient(); 144 var client = new HttpClient();
151 client.post("127.0.0.1", server.port, "/") 145 client.post("127.0.0.1", server.port, "/")
152 .then((request) { 146 .then((request) {
153 if (mimeType != null) { 147 if (mimeType != null) {
154 request.headers.contentType = 148 request.headers.contentType =
155 ContentType.parse(mimeType); 149 ContentType.parse(mimeType);
156 } 150 }
157 request.add(content); 151 request.add(content);
158 return request.close(); 152 return request.close();
159 }) 153 })
160 .then((response) { 154 .then((response) {
161 if (shouldFail) { 155 if (shouldFail) {
162 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); 156 expect(response.statusCode, equals(HttpStatus.BAD_REQUEST));
163 } 157 }
164 response.fold(null, (x, y) {}); 158 response.fold(null, (x, y) {});
165 client.close(); 159 client.close();
166 server.close(); 160 server.close();
167 }); 161 });
168 }); 162 });
169 } 163 }
170 164
171 test("text/plain", "body".codeUnits, "body", "text"); 165 test("text/plain", "body".codeUnits, "body", "text");
172 test("text/plain; charset=utf-8", 166 test("text/plain; charset=utf-8",
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 test('application/x-www-form-urlencoded', 310 test('application/x-www-form-urlencoded',
317 'name=%C8%A4%C8%A4'.codeUnits, 311 'name=%C8%A4%C8%A4'.codeUnits,
318 { 'name' : 'ȤȤ' }, 312 { 'name' : 'ȤȤ' },
319 "form"); 313 "form");
320 } 314 }
321 315
322 void main() { 316 void main() {
323 testHttpClientResponseBody(); 317 testHttpClientResponseBody();
324 testHttpServerRequestBody(); 318 testHttpServerRequestBody();
325 } 319 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698