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

Side by Side Diff: pkg/http_server/test/utils.dart

Issue 721213002: Fix a number of issues with the Range header handling for serving files (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 6 years, 1 month 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
« no previous file with comments | « pkg/http_server/test/http_mock.dart ('k') | pkg/http_server/test/virtual_directory_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 library utils; 5 library utils;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:io'; 9 import 'dart:io';
10 import "package:unittest/unittest.dart"; 10 import "package:unittest/unittest.dart";
(...skipping 24 matching lines...) Expand all
35 var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); 35 var dir = Directory.systemTemp.createTempSync('http_server_virtual_');
36 36
37 return func(dir) 37 return func(dir)
38 .whenComplete(() { 38 .whenComplete(() {
39 return dir.delete(recursive: true); 39 return dir.delete(recursive: true);
40 }); 40 });
41 }); 41 });
42 } 42 }
43 43
44 Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir, 44 Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir,
45 String path, 45 String path,
46 {String host, 46 {String host,
47 bool secure: false, 47 bool secure: false,
48 DateTime ifModifiedSince, 48 DateTime ifModifiedSince,
49 bool rawPath: false, 49 bool rawPath: false,
50 bool followRedirects: true}) { 50 bool followRedirects: true,
51 int from,
52 int to}) {
51 53
52 // if this is a mock test, then run the mock code path 54 // if this is a mock test, then run the mock code path
53 if(_isMockTestExpando[currentTestCase]) { 55 if(_isMockTestExpando[currentTestCase]) {
54 var uri = _getUri(0, path, secure: secure, rawPath: rawPath); 56 var uri = _getUri(0, path, secure: secure, rawPath: rawPath);
55 57
56 var request = new MockHttpRequest(uri, followRedirects: followRedirects, 58 var request = new MockHttpRequest(uri, followRedirects: followRedirects,
57 ifModifiedSince: ifModifiedSince); 59 ifModifiedSince: ifModifiedSince);
60 _addRangeHeader(request, from, to);
58 61
59 return _withMockRequest(virtualDir, request) 62 return _withMockRequest(virtualDir, request)
60 .then((response) { 63 .then((response) {
61 return response.statusCode; 64 return response.statusCode;
62 }); 65 });
63 }; 66 };
64 67
65 assert(_isMockTestExpando[currentTestCase] == false); 68 assert(_isMockTestExpando[currentTestCase] == false);
66 69
67 return _withServer(virtualDir, (port) { 70 return _withServer(virtualDir, (port) {
68 return getStatusCode(port, path, host: host, secure: secure, 71 return getStatusCode(port, path, host: host, secure: secure,
69 ifModifiedSince: ifModifiedSince, rawPath: rawPath, 72 ifModifiedSince: ifModifiedSince, rawPath: rawPath,
70 followRedirects: followRedirects); 73 followRedirects: followRedirects, from: from, to: to);
71 }); 74 });
72 } 75 }
73 76
74 Future<int> getStatusCode(int port, 77 Future<int> getStatusCode(int port,
75 String path, 78 String path,
76 {String host, 79 {String host,
77 bool secure: false, 80 bool secure: false,
78 DateTime ifModifiedSince, 81 DateTime ifModifiedSince,
79 bool rawPath: false, 82 bool rawPath: false,
80 bool followRedirects: true}) { 83 bool followRedirects: true,
84 int from,
85 int to}) {
81 var uri = _getUri(port, path, secure: secure, rawPath: rawPath); 86 var uri = _getUri(port, path, secure: secure, rawPath: rawPath);
82 87
83 return new HttpClient().getUrl(uri) 88 var client = new HttpClient();
89 return client.getUrl(uri)
84 .then((request) { 90 .then((request) {
85 if (!followRedirects) request.followRedirects = false; 91 if (!followRedirects) request.followRedirects = false;
86 if (host != null) request.headers.host = host; 92 if (host != null) request.headers.host = host;
87 if (ifModifiedSince != null) { 93 if (ifModifiedSince != null) {
88 request.headers.ifModifiedSince = ifModifiedSince; 94 request.headers.ifModifiedSince = ifModifiedSince;
89 } 95 }
96 _addRangeHeader(request, from, to);
90 return request.close(); 97 return request.close();
91 }) 98 })
92 .then((response) => response.drain().then( 99 .then((response) => response.drain()
93 (_) => response.statusCode)); 100 .then((_) => response.statusCode))
101 .whenComplete(() => client.close());
94 } 102 }
95 103
96 Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) { 104 Future<HttpHeaders> getHeaders(
105 VirtualDirectory virDir, String path, {int from, int to}) {
97 106
98 // if this is a mock test, then run the mock code path 107 // if this is a mock test, then run the mock code path
99 if(_isMockTestExpando[currentTestCase]) { 108 if(_isMockTestExpando[currentTestCase]) {
100 var uri = _getUri(0, path); 109 var uri = _getUri(0, path);
101 110
102 var request = new MockHttpRequest(uri); 111 var request = new MockHttpRequest(uri);
112 _addRangeHeader(request, from, to);
103 113
104 return _withMockRequest(virDir, request) 114 return _withMockRequest(virDir, request)
105 .then((response) { 115 .then((response) {
106 return response.headers; 116 return response.headers;
107 }); 117 });
108 } 118 }
109 119
110 assert(_isMockTestExpando[currentTestCase] == false); 120 assert(_isMockTestExpando[currentTestCase] == false);
111 121
112 return _withServer(virDir, (port) { 122 return _withServer(virDir, (port) {
113 return _getHeaders(port, path); 123 return _getHeaders(port, path, from, to);
114 }); 124 });
115 } 125 }
116 126
117 Future<String> getAsString(VirtualDirectory virtualDir, String path) { 127 Future<String> getAsString(VirtualDirectory virtualDir, String path) {
118 128
119 // if this is a mock test, then run the mock code path 129 // if this is a mock test, then run the mock code path
120 if(_isMockTestExpando[currentTestCase]) { 130 if(_isMockTestExpando[currentTestCase]) {
121 var uri = _getUri(0, path); 131 var uri = _getUri(0, path);
122 132
123 var request = new MockHttpRequest(uri); 133 var request = new MockHttpRequest(uri);
124 134
125 return _withMockRequest(virtualDir, request) 135 return _withMockRequest(virtualDir, request)
126 .then((response) { 136 .then((response) {
127 return response.mockContent; 137 return response.mockContent;
128 }); 138 });
129 }; 139 };
130 140
131 assert(_isMockTestExpando[currentTestCase] == false); 141 assert(_isMockTestExpando[currentTestCase] == false);
132 142
133 return _withServer(virtualDir, (int port) { 143 return _withServer(virtualDir, (int port) {
134 return _getAsString(port, path); 144 return _getAsString(port, path);
135 }); 145 });
136 } 146 }
137 147
148 Future<List<int>> getAsBytes(
149 VirtualDirectory virtualDir, String path, {int from, int to}) {
150
151 // if this is a mock test, then run the mock code path
152 if (_isMockTestExpando[currentTestCase]) {
153 var uri = _getUri(0, path);
154
155 var request = new MockHttpRequest(uri);
156 _addRangeHeader(request, from, to);
157
158 return _withMockRequest(virtualDir, request)
159 .then((response) {
160 return response.mockContentBinary;
161 });
162 };
163
164 assert(_isMockTestExpando[currentTestCase] == false);
165
166 return _withServer(virtualDir, (int port) {
167 return _getAsBytes(port, path, from, to);
168 });
169 }
170
171 Future<List> getContentAndResponse(
172 VirtualDirectory virtualDir, String path, {int from, int to}) {
173 // if this is a mock test, then run the mock code path
174 if (_isMockTestExpando[currentTestCase]) {
175 var uri = _getUri(0, path);
176
177 var request = new MockHttpRequest(uri);
178 _addRangeHeader(request, from, to);
179
180 return _withMockRequest(virtualDir, request)
181 .then((response) {
182 return [response.mockContentBinary,
183 response];
184 });
185 };
186
187 assert(_isMockTestExpando[currentTestCase] == false);
188
189 return _withServer(virtualDir, (int port) {
190 return _getContentAndResponse(port, path, from, to);
191 });
192 }
193
138 Future<MockHttpResponse> _withMockRequest(VirtualDirectory virDir, 194 Future<MockHttpResponse> _withMockRequest(VirtualDirectory virDir,
139 MockHttpRequest request) { 195 MockHttpRequest request) {
140 return virDir.serveRequest(request).then((value) { 196 return virDir.serveRequest(request).then((value) {
141 expect(value, isNull); 197 expect(value, isNull);
142 expect(request.response.mockDone, isTrue); 198 expect(request.response.mockDone, isTrue);
143 return request.response; 199 return request.response;
144 }) 200 })
145 .then((HttpResponse response) { 201 .then((HttpResponse response) {
146 if(response.statusCode == HttpStatus.MOVED_PERMANENTLY || 202 if(response.statusCode == HttpStatus.MOVED_PERMANENTLY ||
147 response.statusCode == HttpStatus.MOVED_TEMPORARILY) { 203 response.statusCode == HttpStatus.MOVED_TEMPORARILY) {
(...skipping 12 matching lines...) Expand all
160 HttpServer server; 216 HttpServer server;
161 return HttpServer.bind('localhost', 0) 217 return HttpServer.bind('localhost', 0)
162 .then((value) { 218 .then((value) {
163 server = value; 219 server = value;
164 virDir.serve(server); 220 virDir.serve(server);
165 return func(server.port); 221 return func(server.port);
166 }) 222 })
167 .whenComplete(() => server.close()); 223 .whenComplete(() => server.close());
168 } 224 }
169 225
170 Future<HttpHeaders> _getHeaders(int port, String path) => 226 Future<HttpHeaders> _getHeaders(int port, String path, int from, int to) {
171 new HttpClient() 227 var client = new HttpClient();
172 .get('localhost', port, path) 228 return client.get('localhost', port, path)
229 .then((request) {
230 _addRangeHeader(request, from, to);
231 return request.close();
232 })
233 .then((response) => response.drain()
234 .then((_) => response.headers))
235 .whenComplete(() => client.close());
236 }
237
238 Future<String> _getAsString(int port, String path) {
239 var client = new HttpClient();
240 return client.get('localhost', port, path)
173 .then((request) => request.close()) 241 .then((request) => request.close())
174 .then((response) => response.drain().then( 242 .then((response) => UTF8.decodeStream(response))
175 (_) => response.headers)); 243 .whenComplete(() => client.close());
244 }
176 245
177 Future<String> _getAsString(int port, String path) => 246 Future<List<int>> _getAsBytes(int port, String path, int from, int to) {
178 new HttpClient() 247 var client = new HttpClient();
179 .get('localhost', port, path) 248 return client.get('localhost', port, path)
180 .then((request) => request.close()) 249 .then((request) {
181 .then((response) => UTF8.decodeStream(response)); 250 _addRangeHeader(request, from, to);
251 return request.close();
252 })
253 .then((response) => response.fold([], (p, e) => p..addAll(e)))
254 .whenComplete(() => client.close());
255 }
256
257 Future<List> _getContentAndResponse(int port, String path, int from, int to) {
258 var client = new HttpClient();
259 return client.get('localhost', port, path)
260 .then((request) {
261 _addRangeHeader(request, from, to);
262 return request.close();
263 })
264 .then((response) => response.fold([], (p, e) => p..addAll(e))
265 .then((bytes) => [bytes, response]))
266 .whenComplete(() => client.close());
267 }
182 268
183 Uri _getUri(int port, 269 Uri _getUri(int port,
184 String path, 270 String path,
185 {bool secure: false, 271 {bool secure: false,
186 bool rawPath: false}) { 272 bool rawPath: false}) {
187 if (rawPath) { 273 if (rawPath) {
188 return new Uri(scheme: secure ? 'https' : 'http', 274 return new Uri(scheme: secure ? 'https' : 'http',
189 host: 'localhost', 275 host: 'localhost',
190 port: port, 276 port: port,
191 path: path); 277 path: path);
192 } else { 278 } else {
193 return (secure ? 279 return (secure ?
194 new Uri.https('localhost:$port', path) : 280 new Uri.https('localhost:$port', path) :
195 new Uri.http('localhost:$port', path)); 281 new Uri.http('localhost:$port', path));
196 } 282 }
197 } 283 }
198 284
285 void _addRangeHeader(request, int from, int to) {
286 var fromStr = from != null ? '$from' : '';
287 var toStr = to != null ? '$to' : '';
288 if (fromStr.isNotEmpty || toStr.isNotEmpty) {
289 request.headers.set(HttpHeaders.RANGE, 'bytes=$fromStr-$toStr');
290 }
291 }
292
199 const CERTIFICATE = "localhost_cert"; 293 const CERTIFICATE = "localhost_cert";
200 294
201 295
202 void setupSecure() { 296 void setupSecure() {
203 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); 297 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath();
204 SecureSocket.initialize(database: certificateDatabase, 298 SecureSocket.initialize(database: certificateDatabase,
205 password: 'dartdart'); 299 password: 'dartdart');
206 } 300 }
OLDNEW
« no previous file with comments | « pkg/http_server/test/http_mock.dart ('k') | pkg/http_server/test/virtual_directory_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698