DRY'd up file content tests, added check for contentLength
diff --git a/test/sample_test.dart b/test/sample_test.dart
index 3c3abf1..0c68871 100644
--- a/test/sample_test.dart
+++ b/test/sample_test.dart
@@ -12,13 +12,7 @@
void main() {
group('/index.html', () {
test('body is correct', () {
- var uri = Uri.parse('http://localhost/index.html');
- var filePath = p.join(_samplePath, 'index.html');
- var fileContents = new File(filePath).readAsStringSync();
-
- return _request(new Request('GET', uri)).then((response) {
- expect(response.readAsString(), completion(fileContents));
- });
+ return _testFileContents('index.html');
});
// Content-Type:text/html
@@ -27,13 +21,7 @@
group('/favicon.ico', () {
test('body is correct', () {
- var uri = Uri.parse('http://localhost/favicon.ico');
- var filePath = p.join(_samplePath, 'favicon.ico');
- var fileContents = new File(filePath).readAsBytesSync();
-
- return _request(new Request('GET', uri)).then((response) {
- return _expectCompletesWithBytes(response, fileContents);
- });
+ return _testFileContents('favicon.ico');
});
// Content-Type: ???
@@ -41,6 +29,19 @@
});
}
+Future _testFileContents(String filename) {
+ var uri = Uri.parse('http://localhost/$filename');
+ var filePath = p.join(_samplePath, filename);
+ var file = new File(filePath);
+ var fileContents = file.readAsBytesSync();
+ var length = file.statSync().size;
+
+ return _request(new Request('GET', uri)).then((response) {
+ expect(response.contentLength, length);
+ return _expectCompletesWithBytes(response, fileContents);
+ });
+}
+
Future _expectCompletesWithBytes(Response response, List<int> expectedBytes) {
return response.read().toList().then((List<List<int>> bytes) {
var flatBytes = bytes.expand((e) => e);