| OLD | NEW |
| 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 import "package:unittest/unittest.dart"; | 5 import 'dart:math' as math; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 6 import 'package:mime/mime.dart'; | 8 import 'package:mime/mime.dart'; |
| 9 import 'package:mime/src/magic_number.dart'; |
| 7 | 10 |
| 8 void expectMimeType(String path, | 11 void _expectMimeType(String path, |
| 9 String expectedMimeType, | 12 String expectedMimeType, |
| 10 {List<int> headerBytes, | 13 {List<int> headerBytes, |
| 11 MimeTypeResolver resolver}) { | 14 MimeTypeResolver resolver}) { |
| 12 String mimeType; | 15 String mimeType; |
| 13 if (resolver == null) { | 16 if (resolver == null) { |
| 14 mimeType = lookupMimeType(path, headerBytes: headerBytes); | 17 mimeType = lookupMimeType(path, headerBytes: headerBytes); |
| 15 } else { | 18 } else { |
| 16 mimeType = resolver.lookup(path, headerBytes: headerBytes); | 19 mimeType = resolver.lookup(path, headerBytes: headerBytes); |
| 17 } | 20 } |
| 18 | 21 |
| 19 if (mimeType != expectedMimeType) { | 22 expect(mimeType, expectedMimeType); |
| 20 throw "Expect '$expectedMimeType' but got '$mimeType'"; | |
| 21 } | |
| 22 } | 23 } |
| 23 | 24 |
| 24 void main() { | 25 void main() { |
| 25 group('global-lookup', () { | 26 group('global-lookup', () { |
| 26 test('by-path', () { | 27 test('by-path', () { |
| 27 expectMimeType('file.dart', 'application/dart'); | 28 _expectMimeType('file.dart', 'application/dart'); |
| 28 // Test mixed-case | 29 // Test mixed-case |
| 29 expectMimeType('file.DaRT', 'application/dart'); | 30 _expectMimeType('file.DaRT', 'application/dart'); |
| 30 expectMimeType('file.html', 'text/html'); | 31 _expectMimeType('file.html', 'text/html'); |
| 31 expectMimeType('file.xhtml', 'application/xhtml+xml'); | 32 _expectMimeType('file.xhtml', 'application/xhtml+xml'); |
| 32 expectMimeType('file.jpeg', 'image/jpeg'); | 33 _expectMimeType('file.jpeg', 'image/jpeg'); |
| 33 expectMimeType('file.jpg', 'image/jpeg'); | 34 _expectMimeType('file.jpg', 'image/jpeg'); |
| 34 expectMimeType('file.png', 'image/png'); | 35 _expectMimeType('file.png', 'image/png'); |
| 35 expectMimeType('file.gif', 'image/gif'); | 36 _expectMimeType('file.gif', 'image/gif'); |
| 36 expectMimeType('file.cc', 'text/x-c'); | 37 _expectMimeType('file.cc', 'text/x-c'); |
| 37 expectMimeType('file.c', 'text/x-c'); | 38 _expectMimeType('file.c', 'text/x-c'); |
| 38 expectMimeType('file.css', 'text/css'); | 39 _expectMimeType('file.css', 'text/css'); |
| 39 expectMimeType('file.js', 'application/javascript'); | 40 _expectMimeType('file.js', 'application/javascript'); |
| 40 expectMimeType('file.ps', 'application/postscript'); | 41 _expectMimeType('file.ps', 'application/postscript'); |
| 41 expectMimeType('file.pdf', 'application/pdf'); | 42 _expectMimeType('file.pdf', 'application/pdf'); |
| 42 expectMimeType('file.tiff', 'image/tiff'); | 43 _expectMimeType('file.tiff', 'image/tiff'); |
| 43 expectMimeType('file.tif', 'image/tiff'); | 44 _expectMimeType('file.tif', 'image/tiff'); |
| 44 }); | 45 }); |
| 45 | 46 |
| 46 test('unknown-mime-type', () { | 47 test('unknown-mime-type', () { |
| 47 expectMimeType('file.unsupported-extension', null); | 48 _expectMimeType('file.unsupported-extension', null); |
| 48 }); | 49 }); |
| 49 | 50 |
| 50 test('by-header-bytes', () { | 51 test('by-header-bytes', () { |
| 51 expectMimeType('file.jpg', | 52 _expectMimeType('file.jpg', |
| 52 'image/png', | 53 'image/png', |
| 53 headerBytes: [0x89, 0x50, 0x4E, 0x47, | 54 headerBytes: [0x89, 0x50, 0x4E, 0x47, |
| 54 0x0D, 0x0A, 0x1A, 0x0A]); | 55 0x0D, 0x0A, 0x1A, 0x0A]); |
| 55 expectMimeType('file.jpg', | 56 _expectMimeType('file.jpg', |
| 56 'image/gif', | 57 'image/gif', |
| 57 headerBytes: [0x47, 0x49, 0x46, 0x38, 0x39, | 58 headerBytes: [0x47, 0x49, 0x46, 0x38, 0x39, |
| 58 0x61, 0x0D, 0x0A, 0x1A, 0x0A]); | 59 0x61, 0x0D, 0x0A, 0x1A, 0x0A]); |
| 59 expectMimeType('file.gif', | 60 _expectMimeType('file.gif', |
| 60 'image/jpeg', | 61 'image/jpeg', |
| 61 headerBytes: [0xFF, 0xD8, 0x46, 0x38, 0x39, | 62 headerBytes: [0xFF, 0xD8, 0x46, 0x38, 0x39, |
| 62 0x61, 0x0D, 0x0A, 0x1A, 0x0A]); | 63 0x61, 0x0D, 0x0A, 0x1A, 0x0A]); |
| 63 expectMimeType('file.mp4', | 64 _expectMimeType('file.mp4', |
| 64 'video/mp4', | 65 'video/mp4', |
| 65 headerBytes: [0x00, 0x00, 0x00, 0x04, 0x66, 0x74, | 66 headerBytes: [0x00, 0x00, 0x00, 0x04, 0x66, 0x74, |
| 66 0x79, 0x70, 0x33, 0x67, 0x70, 0x35]); | 67 0x79, 0x70, 0x33, 0x67, 0x70, 0x35]); |
| 67 }); | 68 }); |
| 68 }); | 69 }); |
| 69 | 70 |
| 70 group('custom-resolver', () { | 71 group('custom-resolver', () { |
| 71 test('override-extension', () { | 72 test('override-extension', () { |
| 72 var resolver = new MimeTypeResolver(); | 73 var resolver = new MimeTypeResolver(); |
| 73 resolver.addExtension('jpg', 'my-mime-type'); | 74 resolver.addExtension('jpg', 'my-mime-type'); |
| 74 expectMimeType('file.jpg', 'my-mime-type', resolver: resolver); | 75 _expectMimeType('file.jpg', 'my-mime-type', resolver: resolver); |
| 75 }); | 76 }); |
| 76 | 77 |
| 77 test('fallthrough-extension', () { | 78 test('fallthrough-extension', () { |
| 78 var resolver = new MimeTypeResolver(); | 79 var resolver = new MimeTypeResolver(); |
| 79 resolver.addExtension('jpg2', 'my-mime-type'); | 80 resolver.addExtension('jpg2', 'my-mime-type'); |
| 80 expectMimeType('file.jpg', 'image/jpeg', resolver: resolver); | 81 _expectMimeType('file.jpg', 'image/jpeg', resolver: resolver); |
| 81 }); | 82 }); |
| 82 | 83 |
| 83 test('with-mask', () { | 84 test('with-mask', () { |
| 84 var resolver = new MimeTypeResolver.empty(); | 85 var resolver = new MimeTypeResolver.empty(); |
| 85 resolver.addMagicNumber([0x01, 0x02, 0x03], | 86 resolver.addMagicNumber([0x01, 0x02, 0x03], |
| 86 'my-mime-type', | 87 'my-mime-type', |
| 87 mask: [0x01, 0xFF, 0xFE]); | 88 mask: [0x01, 0xFF, 0xFE]); |
| 88 expectMimeType('file', | 89 _expectMimeType('file', |
| 89 'my-mime-type', | 90 'my-mime-type', |
| 90 headerBytes: [0x01, 0x02, 0x03], | 91 headerBytes: [0x01, 0x02, 0x03], |
| 91 resolver: resolver); | 92 resolver: resolver); |
| 92 expectMimeType('file', | 93 _expectMimeType('file', |
| 93 null, | 94 null, |
| 94 headerBytes: [0x01, 0x03, 0x03], | 95 headerBytes: [0x01, 0x03, 0x03], |
| 95 resolver: resolver); | 96 resolver: resolver); |
| 96 expectMimeType('file', | 97 _expectMimeType('file', |
| 97 'my-mime-type', | 98 'my-mime-type', |
| 98 headerBytes: [0xFF, 0x02, 0x02], | 99 headerBytes: [0xFF, 0x02, 0x02], |
| 99 resolver: resolver); | 100 resolver: resolver); |
| 100 }); | 101 }); |
| 101 }); | 102 }); |
| 103 |
| 104 test('default magic number', () { |
| 105 var actualMaxBytes = DEFAULT_MAGIC_NUMBERS.fold(0, (previous, magic) { |
| 106 return math.max(previous, magic.numbers.length); |
| 107 }); |
| 108 |
| 109 expect(defaultMagicNumbersMaxLength, actualMaxBytes); |
| 110 }); |
| 102 } | 111 } |
| 103 | |
| OLD | NEW |