OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library shelf_static.sample_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:path/path.dart' as p; |
| 11 import 'package:scheduled_test/scheduled_test.dart'; |
| 12 |
| 13 import 'package:shelf/shelf.dart'; |
| 14 import 'package:shelf_static/shelf_static.dart'; |
| 15 |
| 16 import 'test_util.dart'; |
| 17 |
| 18 void main() { |
| 19 group('/index.html', () { |
| 20 test('body is correct', () { |
| 21 return _testFileContents('index.html'); |
| 22 }); |
| 23 |
| 24 test('mimeType is text/html', () { |
| 25 return _requestFile('index.html').then((response) { |
| 26 expect(response.mimeType, 'text/html'); |
| 27 }); |
| 28 }); |
| 29 }); |
| 30 |
| 31 group('/favicon.ico', () { |
| 32 test('body is correct', () { |
| 33 return _testFileContents('favicon.ico'); |
| 34 }); |
| 35 |
| 36 test('mimeType is text/html', () { |
| 37 return _requestFile('favicon.ico').then((response) { |
| 38 expect(response.mimeType, 'image/x-icon'); |
| 39 }); |
| 40 }); |
| 41 }); |
| 42 |
| 43 group('/dart.png', () { |
| 44 test('body is correct', () { |
| 45 return _testFileContents('dart.png'); |
| 46 }); |
| 47 |
| 48 test('mimeType is image/png', () { |
| 49 return _requestFile('dart.png').then((response) { |
| 50 expect(response.mimeType, 'image/png'); |
| 51 }); |
| 52 }); |
| 53 }); |
| 54 } |
| 55 |
| 56 Future<Response> _requestFile(String filename) { |
| 57 var uri = Uri.parse('http://localhost/$filename'); |
| 58 |
| 59 return _request(new Request('GET', uri)); |
| 60 } |
| 61 |
| 62 Future _testFileContents(String filename) { |
| 63 var filePath = p.join(_samplePath, filename); |
| 64 var file = new File(filePath); |
| 65 var fileContents = file.readAsBytesSync(); |
| 66 var fileStat = file.statSync(); |
| 67 |
| 68 return _requestFile(filename).then((response) { |
| 69 expect(response.contentLength, fileStat.size); |
| 70 expect(response.lastModified, atSameTimeToSecond(fileStat.changed.toUtc())); |
| 71 return _expectCompletesWithBytes(response, fileContents); |
| 72 }); |
| 73 } |
| 74 |
| 75 Future _expectCompletesWithBytes(Response response, List<int> expectedBytes) { |
| 76 return response.read().toList().then((List<List<int>> bytes) { |
| 77 var flatBytes = bytes.expand((e) => e); |
| 78 expect(flatBytes, orderedEquals(expectedBytes)); |
| 79 }); |
| 80 } |
| 81 |
| 82 Future<Response> _request(Request request) { |
| 83 var handler = createStaticHandler(_samplePath); |
| 84 |
| 85 return new Future.sync(() => handler(request)); |
| 86 } |
| 87 |
| 88 String get _samplePath { |
| 89 var sampleDir = p.join(p.current, 'example', 'files'); |
| 90 assert(FileSystemEntity.isDirectorySync(sampleDir)); |
| 91 return sampleDir; |
| 92 } |
OLD | NEW |