| OLD | NEW |
| 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:expect/expect.dart'; |
| 9 | 9 |
| 10 void testHttpClientResponseBody() { | 10 void testHttpClientResponseBody() { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 null, | 83 null, |
| 84 "json", | 84 "json", |
| 85 true); | 85 true); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void testHttpServerRequestBody() { | 88 void testHttpServerRequestBody() { |
| 89 void test(String mimeType, | 89 void test(String mimeType, |
| 90 List<int> content, | 90 List<int> content, |
| 91 dynamic expectedBody, | 91 dynamic expectedBody, |
| 92 String type, | 92 String type, |
| 93 [bool shouldFail = false]) { | 93 {bool shouldFail: false}) { |
| 94 HttpServer.bind("127.0.0.1", 0).then((server) { | 94 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 95 server.transform(new HttpBodyHandler()) | 95 server.transform(new HttpBodyHandler()) |
| 96 .listen((body) { | 96 .listen((body) { |
| 97 if (shouldFail) Expect.fail("Error expected"); | 97 if (shouldFail) Expect.fail("Error expected"); |
| 98 Expect.equals(type, body.type); | 98 Expect.equals(type, body.type); |
| 99 switch (type) { | 99 switch (type) { |
| 100 case "text": | 100 case "text": |
| 101 Expect.equals(body.mimeType, "text/plain"); | 101 Expect.equals(body.contentType.mimeType, "text/plain"); |
| 102 Expect.equals(expectedBody, body.body); | 102 Expect.equals(expectedBody, body.body); |
| 103 break; | 103 break; |
| 104 | 104 |
| 105 case "json": | 105 case "json": |
| 106 Expect.equals(body.mimeType, "application/json"); | 106 Expect.equals(body.contentType.mimeType, "application/json"); |
| 107 Expect.mapEquals(expectedBody, body.body); | 107 Expect.mapEquals(expectedBody, body.body); |
| 108 break; | 108 break; |
| 109 | 109 |
| 110 case "binary": | 110 case "binary": |
| 111 Expect.equals(body.mimeType, null); | 111 Expect.equals(body.contentType, null); |
| 112 Expect.listEquals(expectedBody, body.body); | 112 Expect.listEquals(expectedBody, body.body); |
| 113 break; | 113 break; |
| 114 | 114 |
| 115 case "form": |
| 116 Expect.equals(body.contentType.mimeType, 'multipart/form-data'); |
| 117 Expect.setEquals(expectedBody.keys.toSet(), |
| 118 body.body.keys.toSet()); |
| 119 for (var key in expectedBody.keys) { |
| 120 if (body.body[key] is HttpBodyFileUpload) { |
| 121 Expect.equals(expectedBody[key]['contentType'], |
| 122 body.body[key].contentType.toString()); |
| 123 Expect.equals(expectedBody[key]['filename'], |
| 124 body.body[key].filename); |
| 125 if (body.body[key].content is String) { |
| 126 Expect.equals(expectedBody[key]['content'], |
| 127 body.body[key].content); |
| 128 } else { |
| 129 Expect.listEquals(expectedBody[key]['content'], |
| 130 body.body[key].content); |
| 131 } |
| 132 } else { |
| 133 Expect.equals(expectedBody[key], body.body[key]); |
| 134 } |
| 135 } |
| 136 break; |
| 137 |
| 115 default: | 138 default: |
| 116 Expect.fail("bad body type"); | 139 Expect.fail("bad body type"); |
| 117 } | 140 } |
| 118 body.response.close(); | 141 body.response.close(); |
| 119 }, onError: (error) { | 142 }, onError: (error) { |
| 120 if (!shouldFail) Expect.fail("Error unexpected"); | 143 if (!shouldFail) throw error; |
| 121 }); | 144 }); |
| 122 | 145 |
| 123 var client = new HttpClient(); | 146 var client = new HttpClient(); |
| 124 client.post("127.0.0.1", server.port, "/") | 147 client.post("127.0.0.1", server.port, "/") |
| 125 .then((request) { | 148 .then((request) { |
| 126 if (mimeType != null) { | 149 if (mimeType != null) { |
| 127 request.headers.contentType = | 150 request.headers.contentType = |
| 128 ContentType.parse(mimeType); | 151 ContentType.parse(mimeType); |
| 129 } | 152 } |
| 130 request.add(content); | 153 request.add(content); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 153 "text"); | 176 "text"); |
| 154 | 177 |
| 155 test("application/json", | 178 test("application/json", |
| 156 '{"val": 5}'.codeUnits, | 179 '{"val": 5}'.codeUnits, |
| 157 { "val" : 5 }, | 180 { "val" : 5 }, |
| 158 "json"); | 181 "json"); |
| 159 test("application/json", | 182 test("application/json", |
| 160 '{ bad json }'.codeUnits, | 183 '{ bad json }'.codeUnits, |
| 161 null, | 184 null, |
| 162 "json", | 185 "json", |
| 163 true); | 186 shouldFail: true); |
| 164 | 187 |
| 165 test(null, "body".codeUnits, "body".codeUnits, "binary"); | 188 test(null, "body".codeUnits, "body".codeUnits, "binary"); |
| 189 |
| 190 test("multipart/form-data; boundary=AaB03x", |
| 191 ''' |
| 192 --AaB03x\r |
| 193 Content-Disposition: form-data; name="name"\r |
| 194 \r |
| 195 Larry\r |
| 196 --AaB03x--\r\n'''.codeUnits, |
| 197 { "name": "Larry" }, |
| 198 "form"); |
| 199 |
| 200 test("multipart/form-data; boundary=AaB03x", |
| 201 ''' |
| 202 --AaB03x\r |
| 203 Content-Disposition: form-data; name="files"; filename="myfile"\r |
| 204 Content-Type: application/octet-stream\r |
| 205 \r |
| 206 File content\r |
| 207 --AaB03x--\r\n'''.codeUnits, |
| 208 { "files": { 'filename': 'myfile', |
| 209 'contentType': 'application/octet-stream', |
| 210 'content': 'File content'.codeUnits} }, |
| 211 "form"); |
| 212 |
| 213 test("multipart/form-data; boundary=AaB03x", |
| 214 ''' |
| 215 --AaB03x\r |
| 216 Content-Disposition: form-data; name="files"; filename="myfile"\r |
| 217 Content-Type: application/octet-stream\r |
| 218 \r |
| 219 File content\r |
| 220 --AaB03x\r |
| 221 Content-Disposition: form-data; name="files"; filename="myfile"\r |
| 222 Content-Type: text/plain\r |
| 223 \r |
| 224 File content\r |
| 225 --AaB03x--\r\n'''.codeUnits, |
| 226 { "files": { 'filename': 'myfile', |
| 227 'contentType': 'text/plain', |
| 228 'content': 'File content'} }, |
| 229 "form"); |
| 230 |
| 231 test("multipart/form-data; boundary=AaB03x", |
| 232 ''' |
| 233 --AaB03x\r |
| 234 Content-Disposition: form-data; name="files"; filename="myfile"\r |
| 235 Content-Type: application/json\r |
| 236 \r |
| 237 File content\r |
| 238 --AaB03x--\r\n'''.codeUnits, |
| 239 { "files": { 'filename': 'myfile', |
| 240 'contentType': 'application/json', |
| 241 'content': 'File content'} }, |
| 242 "form"); |
| 166 } | 243 } |
| 167 | 244 |
| 168 void main() { | 245 void main() { |
| 169 testHttpClientResponseBody(); | 246 testHttpClientResponseBody(); |
| 170 testHttpServerRequestBody(); | 247 testHttpServerRequestBody(); |
| 171 } | 248 } |
| OLD | NEW |