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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 dynamic expectedBody, | 91 dynamic expectedBody, |
92 String type, | 92 String type, |
93 [bool shouldFail = false]) { | 93 [bool shouldFail = false]) { |
94 HttpServer.bind().then((server) { | 94 HttpServer.bind().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(expectedBody, body.body); | 102 Expect.equals(expectedBody, body.body); |
102 break; | 103 break; |
103 | 104 |
104 case "json": | 105 case "json": |
| 106 Expect.equals(body.mimeType, "application/json"); |
105 Expect.mapEquals(expectedBody, body.body); | 107 Expect.mapEquals(expectedBody, body.body); |
106 break; | 108 break; |
107 | 109 |
| 110 case "binary": |
| 111 Expect.equals(body.mimeType, null); |
| 112 Expect.listEquals(expectedBody, body.body); |
| 113 break; |
| 114 |
108 default: | 115 default: |
109 Expect.fail("bad body type"); | 116 Expect.fail("bad body type"); |
110 } | 117 } |
111 body.response.close(); | 118 body.response.close(); |
112 }, onError: (error) { | 119 }, onError: (error) { |
113 if (!shouldFail) Expect.fail("Error unexpected"); | 120 if (!shouldFail) Expect.fail("Error unexpected"); |
114 }); | 121 }); |
115 | 122 |
116 var client = new HttpClient(); | 123 var client = new HttpClient(); |
117 client.post("127.0.0.1", server.port, "/") | 124 client.post("127.0.0.1", server.port, "/") |
118 .then((request) { | 125 .then((request) { |
119 request.headers.contentType = | 126 if (mimeType != null) { |
120 new ContentType.fromString(mimeType); | 127 request.headers.contentType = |
| 128 new ContentType.fromString(mimeType); |
| 129 } |
121 request.add(content); | 130 request.add(content); |
122 return request.close(); | 131 return request.close(); |
123 }) | 132 }) |
124 .then((response) { | 133 .then((response) { |
125 if (shouldFail) { | 134 if (shouldFail) { |
126 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | 135 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
127 } | 136 } |
128 response.fold(null, (x, y) {}); | 137 response.fold(null, (x, y) {}); |
129 client.close(); | 138 client.close(); |
130 server.close(); | 139 server.close(); |
(...skipping 14 matching lines...) Expand all Loading... |
145 | 154 |
146 test("application/json", | 155 test("application/json", |
147 '{"val": 5}'.codeUnits, | 156 '{"val": 5}'.codeUnits, |
148 { "val" : 5 }, | 157 { "val" : 5 }, |
149 "json"); | 158 "json"); |
150 test("application/json", | 159 test("application/json", |
151 '{ bad json }'.codeUnits, | 160 '{ bad json }'.codeUnits, |
152 null, | 161 null, |
153 "json", | 162 "json", |
154 true); | 163 true); |
| 164 |
| 165 test(null, "body".codeUnits, "body".codeUnits, "binary"); |
155 } | 166 } |
156 | 167 |
157 | 168 |
158 void main() { | 169 void main() { |
159 testHttpClientResponseBody(); | 170 testHttpClientResponseBody(); |
160 testHttpServerRequestBody(); | 171 testHttpServerRequestBody(); |
161 } | 172 } |
OLD | NEW |