<commit_before>library shelf_static.basic_file_test;

import 'dart:async';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:scheduled_test/scheduled_test.dart';

import 'package:shelf/shelf.dart';
import 'package:shelf_static/shelf_static.dart';
import 'package:shelf_static/src/util.dart';

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));
      });
    });

    // Content-Type:text/html
    // Date:Fri, 02 May 2014 22:29:02 GMT
  });

  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);
      });
    });

    // Content-Type: ???
    // Date:Fri, 02 May 2014 22:29:02 GMT
  });
}

Future _expectCompletesWithBytes(Response response, List<int> expectedBytes) {
  return response.read().toList().then((List<List<int>> bytes) {
    var flatBytes = bytes.expand((e) => e);
    expect(flatBytes, orderedEquals(expectedBytes));
  });
}

Future<Response> _request(Request request) {<commit_msg>DRY'd up file content tests, added check for contentLength<commit_after>library shelf_static.basic_file_test;

import 'dart:async';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:scheduled_test/scheduled_test.dart';

import 'package:shelf/shelf.dart';
import 'package:shelf_static/shelf_static.dart';
import 'package:shelf_static/src/util.dart';

void main() {
  group('/index.html', () {
    test('body is correct', () {
      return _testFileContents('index.html');
    });

    // Content-Type:text/html
    // Date:Fri, 02 May 2014 22:29:02 GMT
  });

  group('/favicon.ico', () {
    test('body is correct', () {
      return _testFileContents('favicon.ico');
    });

    // Content-Type: ???
    // Date:Fri, 02 May 2014 22:29:02 GMT
  });
}

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);
    expect(flatBytes, orderedEquals(expectedBytes));
  });
}

Future<Response> _request(Request request) {
{'fraction_non_alphanumeric': 0.1610912633972069, 'fraction_numerical': 0.015589477102955504, 'mean_word_length': 4.211505922165821, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '621777', 'n_tokens_mistral': 1061, 'n_tokens_neox': 984, 'n_words': 194}