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:convert'; | 6 import 'dart:convert'; |
7 | 7 |
8 import 'package:http_server/http_server.dart'; | 8 import 'package:http_server/http_server.dart'; |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 | 10 |
11 void testHttpClientResponseBody() { | 11 void testHttpClientResponseBody() { |
12 void test(String mimeType, | 12 void test(String mimeType, |
13 List<int> content, | 13 List<int> content, |
14 dynamic expectedBody, | 14 dynamic expectedBody, |
15 String type, | 15 String type, |
16 [bool shouldFail = false]) { | 16 [bool shouldFail = false]) { |
17 HttpServer.bind("127.0.0.1", 0).then((server) { | 17 HttpServer.bind("localhost", 0).then((server) { |
Bill Hesse
2017/06/23 13:49:08
Consider if you want to use InternetAddress.lookup
keertip
2017/06/27 17:51:06
Acknowledged.
| |
18 server.listen((request) { | 18 server.listen((request) { |
19 request.listen( | 19 request.listen( |
20 (_) {}, | 20 (_) {}, |
21 onDone: () { | 21 onDone: () { |
22 request.response.headers.contentType = | 22 request.response.headers.contentType = |
23 ContentType.parse(mimeType); | 23 ContentType.parse(mimeType); |
24 request.response.add(content); | 24 request.response.add(content); |
25 request.response.close(); | 25 request.response.close(); |
26 }); | 26 }); |
27 }); | 27 }); |
28 | 28 |
29 var client = new HttpClient(); | 29 var client = new HttpClient(); |
30 client.get("127.0.0.1", server.port, "/") | 30 client.get("localhost", server.port, "/") |
31 .then((request) => request.close()) | 31 .then((request) => request.close()) |
32 .then(HttpBodyHandler.processResponse) | 32 .then(HttpBodyHandler.processResponse) |
33 .then((body) { | 33 .then((body) { |
34 expect(shouldFail, isFalse); | 34 expect(shouldFail, isFalse); |
35 expect(body.type, equals(type)); | 35 expect(body.type, equals(type)); |
36 expect(body.response, isNotNull); | 36 expect(body.response, isNotNull); |
37 switch (type) { | 37 switch (type) { |
38 case "text": | 38 case "text": |
39 case "json": | 39 case "json": |
40 expect(body.body, equals(expectedBody)); | 40 expect(body.body, equals(expectedBody)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 true); | 80 true); |
81 } | 81 } |
82 | 82 |
83 void testHttpServerRequestBody() { | 83 void testHttpServerRequestBody() { |
84 void test(String mimeType, | 84 void test(String mimeType, |
85 List<int> content, | 85 List<int> content, |
86 dynamic expectedBody, | 86 dynamic expectedBody, |
87 String type, | 87 String type, |
88 {bool shouldFail: false, | 88 {bool shouldFail: false, |
89 Encoding defaultEncoding: UTF8}) { | 89 Encoding defaultEncoding: UTF8}) { |
90 HttpServer.bind("127.0.0.1", 0).then((server) { | 90 HttpServer.bind("localhost", 0).then((server) { |
91 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding)) | 91 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding)) |
92 .listen((body) { | 92 .listen((body) { |
93 if (shouldFail) return; | 93 if (shouldFail) return; |
94 expect(shouldFail, isFalse); | 94 expect(shouldFail, isFalse); |
95 expect(body.type, equals(type)); | 95 expect(body.type, equals(type)); |
96 switch (type) { | 96 switch (type) { |
97 case "text": | 97 case "text": |
98 expect(body.request.headers.contentType.mimeType, | 98 expect(body.request.headers.contentType.mimeType, |
99 equals("text/plain")); | 99 equals("text/plain")); |
100 expect(body.body, equals(expectedBody)); | 100 expect(body.body, equals(expectedBody)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 | 136 |
137 default: | 137 default: |
138 throw "bad body type"; | 138 throw "bad body type"; |
139 } | 139 } |
140 body.request.response.close(); | 140 body.request.response.close(); |
141 }, onError: (error) { | 141 }, onError: (error) { |
142 if (!shouldFail) throw error; | 142 if (!shouldFail) throw error; |
143 }); | 143 }); |
144 | 144 |
145 var client = new HttpClient(); | 145 var client = new HttpClient(); |
146 client.post("127.0.0.1", server.port, "/") | 146 client.post("localhost", server.port, "/") |
147 .then((request) { | 147 .then((request) { |
148 if (mimeType != null) { | 148 if (mimeType != null) { |
149 request.headers.contentType = | 149 request.headers.contentType = |
150 ContentType.parse(mimeType); | 150 ContentType.parse(mimeType); |
151 } | 151 } |
152 request.add(content); | 152 request.add(content); |
153 return request.close(); | 153 return request.close(); |
154 }) | 154 }) |
155 .then((response) { | 155 .then((response) { |
156 if (shouldFail) { | 156 if (shouldFail) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 test('application/x-www-form-urlencoded', | 332 test('application/x-www-form-urlencoded', |
333 'name=%C8%A4%C8%A4'.codeUnits, | 333 'name=%C8%A4%C8%A4'.codeUnits, |
334 { 'name' : 'ȤȤ' }, | 334 { 'name' : 'ȤȤ' }, |
335 "form"); | 335 "form"); |
336 } | 336 } |
337 | 337 |
338 void main() { | 338 void main() { |
339 testHttpClientResponseBody(); | 339 testHttpClientResponseBody(); |
340 testHttpServerRequestBody(); | 340 testHttpServerRequestBody(); |
341 } | 341 } |
OLD | NEW |