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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
11 import 'dart:io'; | 11 import 'dart:io'; |
12 import 'dart:typed_data'; | 12 import 'dart:typed_data'; |
13 | 13 |
14 void testServerCompress({bool clientAutoUncompress: true}) { | 14 void testServerCompress({bool clientAutoUncompress: true}) { |
15 void test(List<int> data) { | 15 void test(List<int> data) { |
16 HttpServer.bind("127.0.0.1", 0).then((server) { | 16 HttpServer.bind("127.0.0.1", 0).then((server) { |
17 server.autoCompress = true; | 17 server.autoCompress = true; |
18 server.listen((request) { | 18 server.listen((request) { |
19 request.response.add(data); | 19 request.response.add(data); |
20 request.response.close(); | 20 request.response.close(); |
21 }); | 21 }); |
22 var client = new HttpClient(); | 22 var client = new HttpClient(); |
23 client.autoUncompress = clientAutoUncompress; | 23 client.autoUncompress = clientAutoUncompress; |
24 client.get("127.0.0.1", server.port, "/") | 24 client.get("127.0.0.1", server.port, "/").then((request) { |
25 .then((request) { | 25 request.headers.set(HttpHeaders.ACCEPT_ENCODING, "gzip,deflate"); |
26 request.headers.set(HttpHeaders.ACCEPT_ENCODING, "gzip,deflate"); | 26 return request.close(); |
27 return request.close(); | 27 }).then((response) { |
28 }) | 28 Expect.equals( |
29 .then((response) { | 29 "gzip", response.headers.value(HttpHeaders.CONTENT_ENCODING)); |
30 Expect.equals("gzip", | 30 response.fold([], (list, b) { |
31 response.headers.value(HttpHeaders.CONTENT_ENCODING)); | 31 list.addAll(b); |
32 response | 32 return list; |
33 .fold([], (list, b) { | 33 }).then((list) { |
34 list.addAll(b); | 34 if (clientAutoUncompress) { |
35 return list; | 35 Expect.listEquals(data, list); |
36 }).then((list) { | 36 } else { |
37 if (clientAutoUncompress) { | 37 Expect.listEquals(data, GZIP.decode(list)); |
38 Expect.listEquals(data, list); | 38 } |
39 } else { | 39 server.close(); |
40 Expect.listEquals(data, GZIP.decode(list)); | 40 client.close(); |
41 } | 41 }); |
42 server.close(); | 42 }); |
43 client.close(); | |
44 }); | |
45 }); | |
46 }); | 43 }); |
47 } | 44 } |
| 45 |
48 test("My raw server provided data".codeUnits); | 46 test("My raw server provided data".codeUnits); |
49 var longBuffer = new Uint8List(1024 * 1024); | 47 var longBuffer = new Uint8List(1024 * 1024); |
50 for (int i = 0; i < longBuffer.length; i++) { | 48 for (int i = 0; i < longBuffer.length; i++) { |
51 longBuffer[i] = i & 0xFF; | 49 longBuffer[i] = i & 0xFF; |
52 } | 50 } |
53 test(longBuffer); | 51 test(longBuffer); |
54 } | 52 } |
55 | 53 |
56 void testAcceptEncodingHeader() { | 54 void testAcceptEncodingHeader() { |
57 void test(String encoding, bool valid) { | 55 void test(String encoding, bool valid) { |
58 HttpServer.bind("127.0.0.1", 0).then((server) { | 56 HttpServer.bind("127.0.0.1", 0).then((server) { |
59 server.autoCompress = true; | 57 server.autoCompress = true; |
60 server.listen((request) { | 58 server.listen((request) { |
61 request.response.write("data"); | 59 request.response.write("data"); |
62 request.response.close(); | 60 request.response.close(); |
63 }); | 61 }); |
64 var client = new HttpClient(); | 62 var client = new HttpClient(); |
65 client.get("127.0.0.1", server.port, "/") | 63 client.get("127.0.0.1", server.port, "/").then((request) { |
66 .then((request) { | 64 request.headers.set(HttpHeaders.ACCEPT_ENCODING, encoding); |
67 request.headers.set(HttpHeaders.ACCEPT_ENCODING, encoding); | 65 return request.close(); |
68 return request.close(); | 66 }).then((response) { |
69 }) | 67 Expect.equals(valid, |
70 .then((response) { | 68 ("gzip" == response.headers.value(HttpHeaders.CONTENT_ENCODING))); |
71 Expect.equals( | 69 response.listen((_) {}, onDone: () { |
72 valid, | 70 server.close(); |
73 ("gzip" == response.headers.value(HttpHeaders.CONTENT_ENCODING))); | 71 client.close(); |
74 response.listen( | 72 }); |
75 (_) {}, | 73 }); |
76 onDone: () { | |
77 server.close(); | |
78 client.close(); | |
79 }); | |
80 }); | |
81 }); | 74 }); |
82 } | 75 } |
| 76 |
83 test('gzip', true); | 77 test('gzip', true); |
84 test('deflate', false); | 78 test('deflate', false); |
85 test('gzip, deflate', true); | 79 test('gzip, deflate', true); |
86 test('gzip ,deflate', true); | 80 test('gzip ,deflate', true); |
87 test('gzip , deflate', true); | 81 test('gzip , deflate', true); |
88 test('deflate,gzip', true); | 82 test('deflate,gzip', true); |
89 test('deflate, gzip', true); | 83 test('deflate, gzip', true); |
90 test('deflate ,gzip', true); | 84 test('deflate ,gzip', true); |
91 test('deflate , gzip', true); | 85 test('deflate , gzip', true); |
92 test('abc,deflate , gzip,def,,,ghi ,jkl', true); | 86 test('abc,deflate , gzip,def,,,ghi ,jkl', true); |
93 test('xgzip', false); | 87 test('xgzip', false); |
94 test('gzipx;', false); | 88 test('gzipx;', false); |
95 } | 89 } |
96 | 90 |
97 void testDisableCompressTest() { | 91 void testDisableCompressTest() { |
98 HttpServer.bind("127.0.0.1", 0).then((server) { | 92 HttpServer.bind("127.0.0.1", 0).then((server) { |
99 Expect.equals(false, server.autoCompress); | 93 Expect.equals(false, server.autoCompress); |
100 server.listen((request) { | 94 server.listen((request) { |
101 Expect.equals('gzip', request.headers.value(HttpHeaders.ACCEPT_ENCODING)); | 95 Expect.equals('gzip', request.headers.value(HttpHeaders.ACCEPT_ENCODING)); |
102 request.response.write("data"); | 96 request.response.write("data"); |
103 request.response.close(); | 97 request.response.close(); |
104 }); | 98 }); |
105 var client = new HttpClient(); | 99 var client = new HttpClient(); |
106 client.get("127.0.0.1", server.port, "/") | 100 client |
| 101 .get("127.0.0.1", server.port, "/") |
107 .then((request) => request.close()) | 102 .then((request) => request.close()) |
108 .then((response) { | 103 .then((response) { |
109 Expect.equals(null, | 104 Expect.equals(null, response.headers.value(HttpHeaders.CONTENT_ENCODING)); |
110 response.headers.value(HttpHeaders.CONTENT_ENCODING)); | 105 response.listen((_) {}, onDone: () { |
111 response.listen( | 106 server.close(); |
112 (_) {}, | 107 client.close(); |
113 onDone: () { | 108 }); |
114 server.close(); | 109 }); |
115 client.close(); | |
116 }); | |
117 }); | |
118 }); | 110 }); |
119 } | 111 } |
120 | 112 |
121 void main() { | 113 void main() { |
122 testServerCompress(); | 114 testServerCompress(); |
123 testServerCompress(clientAutoUncompress: false); | 115 testServerCompress(clientAutoUncompress: false); |
124 testAcceptEncodingHeader(); | 116 testAcceptEncodingHeader(); |
125 testDisableCompressTest(); | 117 testDisableCompressTest(); |
126 } | 118 } |
OLD | NEW |