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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/http_server/test/utils.dart
diff --git a/pkg/http_server/test/utils.dart b/pkg/http_server/test/utils.dart
index c0b3c5ca49e154a1d138b8f0178f230f31401838..80c16ca20020cf591444c110df9416cf9288e09b 100644
--- a/pkg/http_server/test/utils.dart
+++ b/pkg/http_server/test/utils.dart
@@ -42,12 +42,14 @@ void _testVirtualDir(String name, bool useMocks, Future func(Directory dir)) {
}
Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir,
- String path,
- {String host,
- bool secure: false,
- DateTime ifModifiedSince,
- bool rawPath: false,
- bool followRedirects: true}) {
+ String path,
+ {String host,
+ bool secure: false,
+ DateTime ifModifiedSince,
+ bool rawPath: false,
+ bool followRedirects: true,
+ int from,
+ int to}) {
// if this is a mock test, then run the mock code path
if(_isMockTestExpando[currentTestCase]) {
@@ -55,6 +57,7 @@ Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir,
var request = new MockHttpRequest(uri, followRedirects: followRedirects,
ifModifiedSince: ifModifiedSince);
+ _addRangeHeader(request, from, to);
return _withMockRequest(virtualDir, request)
.then((response) {
@@ -67,7 +70,7 @@ Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir,
return _withServer(virtualDir, (port) {
return getStatusCode(port, path, host: host, secure: secure,
ifModifiedSince: ifModifiedSince, rawPath: rawPath,
- followRedirects: followRedirects);
+ followRedirects: followRedirects, from: from, to: to);
});
}
@@ -77,29 +80,36 @@ Future<int> getStatusCode(int port,
bool secure: false,
DateTime ifModifiedSince,
bool rawPath: false,
- bool followRedirects: true}) {
+ bool followRedirects: true,
+ int from,
+ int to}) {
var uri = _getUri(port, path, secure: secure, rawPath: rawPath);
- return new HttpClient().getUrl(uri)
+ var client = new HttpClient();
+ return client.getUrl(uri)
.then((request) {
if (!followRedirects) request.followRedirects = false;
if (host != null) request.headers.host = host;
if (ifModifiedSince != null) {
request.headers.ifModifiedSince = ifModifiedSince;
}
+ _addRangeHeader(request, from, to);
return request.close();
})
- .then((response) => response.drain().then(
- (_) => response.statusCode));
+ .then((response) => response.drain()
+ .then((_) => response.statusCode))
+ .whenComplete(() => client.close());
}
-Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) {
+Future<HttpHeaders> getHeaders(
+ VirtualDirectory virDir, String path, {int from, int to}) {
// if this is a mock test, then run the mock code path
if(_isMockTestExpando[currentTestCase]) {
var uri = _getUri(0, path);
var request = new MockHttpRequest(uri);
+ _addRangeHeader(request, from, to);
return _withMockRequest(virDir, request)
.then((response) {
@@ -110,7 +120,7 @@ Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) {
assert(_isMockTestExpando[currentTestCase] == false);
return _withServer(virDir, (port) {
- return _getHeaders(port, path);
+ return _getHeaders(port, path, from, to);
});
}
@@ -135,6 +145,52 @@ Future<String> getAsString(VirtualDirectory virtualDir, String path) {
});
}
+Future<List<int>> getAsBytes(
+ VirtualDirectory virtualDir, String path, {int from, int to}) {
+
+ // if this is a mock test, then run the mock code path
+ if (_isMockTestExpando[currentTestCase]) {
+ var uri = _getUri(0, path);
+
+ var request = new MockHttpRequest(uri);
+ _addRangeHeader(request, from, to);
+
+ return _withMockRequest(virtualDir, request)
+ .then((response) {
+ return response.mockContentBinary;
+ });
+ };
+
+ assert(_isMockTestExpando[currentTestCase] == false);
+
+ return _withServer(virtualDir, (int port) {
+ return _getAsBytes(port, path, from, to);
+ });
+}
+
+Future<List> getContentAndResponse(
+ VirtualDirectory virtualDir, String path, {int from, int to}) {
+ // if this is a mock test, then run the mock code path
+ if (_isMockTestExpando[currentTestCase]) {
+ var uri = _getUri(0, path);
+
+ var request = new MockHttpRequest(uri);
+ _addRangeHeader(request, from, to);
+
+ return _withMockRequest(virtualDir, request)
+ .then((response) {
+ return [response.mockContentBinary,
+ response];
+ });
+ };
+
+ assert(_isMockTestExpando[currentTestCase] == false);
+
+ return _withServer(virtualDir, (int port) {
+ return _getContentAndResponse(port, path, from, to);
+ });
+}
+
Future<MockHttpResponse> _withMockRequest(VirtualDirectory virDir,
MockHttpRequest request) {
return virDir.serveRequest(request).then((value) {
@@ -167,18 +223,48 @@ Future _withServer(VirtualDirectory virDir, Future func(int port)) {
.whenComplete(() => server.close());
}
-Future<HttpHeaders> _getHeaders(int port, String path) =>
- new HttpClient()
- .get('localhost', port, path)
- .then((request) => request.close())
- .then((response) => response.drain().then(
- (_) => response.headers));
+Future<HttpHeaders> _getHeaders(int port, String path, int from, int to) {
+ var client = new HttpClient();
+ return client.get('localhost', port, path)
+ .then((request) {
+ _addRangeHeader(request, from, to);
+ return request.close();
+ })
+ .then((response) => response.drain()
+ .then((_) => response.headers))
+ .whenComplete(() => client.close());
+}
-Future<String> _getAsString(int port, String path) =>
- new HttpClient()
- .get('localhost', port, path)
+Future<String> _getAsString(int port, String path) {
+ var client = new HttpClient();
+ return client.get('localhost', port, path)
.then((request) => request.close())
- .then((response) => UTF8.decodeStream(response));
+ .then((response) => UTF8.decodeStream(response))
+ .whenComplete(() => client.close());
+}
+
+Future<List<int>> _getAsBytes(int port, String path, int from, int to) {
+ var client = new HttpClient();
+ return client.get('localhost', port, path)
+ .then((request) {
+ _addRangeHeader(request, from, to);
+ return request.close();
+ })
+ .then((response) => response.fold([], (p, e) => p..addAll(e)))
+ .whenComplete(() => client.close());
+}
+
+Future<List> _getContentAndResponse(int port, String path, int from, int to) {
+ var client = new HttpClient();
+ return client.get('localhost', port, path)
+ .then((request) {
+ _addRangeHeader(request, from, to);
+ return request.close();
+ })
+ .then((response) => response.fold([], (p, e) => p..addAll(e))
+ .then((bytes) => [bytes, response]))
+ .whenComplete(() => client.close());
+}
Uri _getUri(int port,
String path,
@@ -196,6 +282,14 @@ Uri _getUri(int port,
}
}
+void _addRangeHeader(request, int from, int to) {
+ var fromStr = from != null ? '$from' : '';
+ var toStr = to != null ? '$to' : '';
+ if (fromStr.isNotEmpty || toStr.isNotEmpty) {
+ request.headers.set(HttpHeaders.RANGE, 'bytes=$fromStr-$toStr');
+ }
+}
+
const CERTIFICATE = "localhost_cert";
« 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