test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. 'use strict';
  2. var mime = require('..');
  3. var mimeTypes = require('../node_modules/mime-types');
  4. var assert = require('assert');
  5. var chalk = require('chalk');
  6. describe('class Mime', function() {
  7. it('mime and mime/lite coexist', function() {
  8. assert.doesNotThrow(function() {
  9. require('../lite');
  10. });
  11. });
  12. it('new constructor()', function() {
  13. var Mime = require('../Mime');
  14. var mime = new Mime(
  15. {'text/a': ['a', 'a1']},
  16. {'text/b': ['b', 'b1']}
  17. );
  18. assert.deepEqual(mime._types, {
  19. a: 'text/a',
  20. a1: 'text/a',
  21. b: 'text/b',
  22. b1: 'text/b',
  23. });
  24. assert.deepEqual(mime._extensions, {
  25. 'text/a': 'a',
  26. 'text/b': 'b',
  27. });
  28. });
  29. it('define()', function() {
  30. var Mime = require('../Mime');
  31. var mime = new Mime({'text/a': ['a']}, {'text/b': ['b']});
  32. assert.throws(function() {
  33. mime.define({'text/c': ['b']});
  34. });
  35. assert.doesNotThrow(function() {
  36. mime.define({'text/c': ['b']}, true);
  37. });
  38. assert.deepEqual(mime._types, {
  39. a: 'text/a',
  40. b: 'text/c',
  41. });
  42. assert.deepEqual(mime._extensions, {
  43. 'text/a': 'a',
  44. 'text/b': 'b',
  45. 'text/c': 'b',
  46. });
  47. });
  48. it('define() *\'ed types', function() {
  49. var Mime = require('../Mime');
  50. var mime = new Mime(
  51. {'text/a': ['*b']},
  52. {'text/b': ['b']}
  53. );
  54. assert.deepEqual(mime._types, {
  55. b: 'text/b',
  56. });
  57. assert.deepEqual(mime._extensions, {
  58. 'text/a': 'b',
  59. 'text/b': 'b',
  60. });
  61. });
  62. it('getType()', function() {
  63. // Upper/lower case
  64. assert.equal(mime.getType('text.txt'), 'text/plain');
  65. assert.equal(mime.getType('TEXT.TXT'), 'text/plain');
  66. // Bare extension
  67. assert.equal(mime.getType('txt'), 'text/plain');
  68. assert.equal(mime.getType('.txt'), 'text/plain');
  69. assert.strictEqual(mime.getType('.bogus'), null);
  70. assert.strictEqual(mime.getType('bogus'), null);
  71. // Non-sensical
  72. assert.strictEqual(mime.getType(null), null);
  73. assert.strictEqual(mime.getType(undefined), null);
  74. assert.strictEqual(mime.getType(42), null);
  75. assert.strictEqual(mime.getType({}), null);
  76. // File paths
  77. assert.equal(mime.getType('dir/text.txt'), 'text/plain');
  78. assert.equal(mime.getType('dir\\text.txt'), 'text/plain');
  79. assert.equal(mime.getType('.text.txt'), 'text/plain');
  80. assert.equal(mime.getType('.txt'), 'text/plain');
  81. assert.equal(mime.getType('txt'), 'text/plain');
  82. assert.equal(mime.getType('/path/to/page.html'), 'text/html');
  83. assert.equal(mime.getType('c:\\path\\to\\page.html'), 'text/html');
  84. assert.equal(mime.getType('page.html'), 'text/html');
  85. assert.equal(mime.getType('path/to/page.html'), 'text/html');
  86. assert.equal(mime.getType('path\\to\\page.html'), 'text/html');
  87. assert.strictEqual(mime.getType('/txt'), null);
  88. assert.strictEqual(mime.getType('\\txt'), null);
  89. assert.strictEqual(mime.getType('text.nope'), null);
  90. assert.strictEqual(mime.getType('/path/to/file.bogus'), null);
  91. assert.strictEqual(mime.getType('/path/to/json'), null);
  92. assert.strictEqual(mime.getType('/path/to/.json'), null);
  93. assert.strictEqual(mime.getType('/path/to/.config.json'), 'application/json');
  94. assert.strictEqual(mime.getType('.config.json'), 'application/json');
  95. });
  96. it('getExtension()', function() {
  97. assert.equal(mime.getExtension('text/html'), 'html');
  98. assert.equal(mime.getExtension(' text/html'), 'html');
  99. assert.equal(mime.getExtension('text/html '), 'html');
  100. assert.strictEqual(mime.getExtension('application/x-bogus'), null);
  101. assert.strictEqual(mime.getExtension('bogus'), null);
  102. assert.strictEqual(mime.getExtension(null), null);
  103. assert.strictEqual(mime.getExtension(undefined), null);
  104. assert.strictEqual(mime.getExtension(42), null);
  105. assert.strictEqual(mime.getExtension({}), null);
  106. });
  107. });
  108. describe('DB', function() {
  109. var diffs = [];
  110. after(function() {
  111. if (diffs.length) {
  112. console.log('\n[INFO] The following inconsistencies with MDN (https://goo.gl/lHrFU6) and/or mime-types (https://github.com/jshttp/mime-types) are expected:');
  113. diffs.forEach(function(d) {
  114. console.warn(
  115. ' ' + d[0]+ '[' + chalk.blue(d[1]) + '] = ' + chalk.red(d[2]) +
  116. ', mime[' + d[1] + '] = ' + chalk.green(d[3])
  117. );
  118. });
  119. }
  120. });
  121. it('Consistency', function() {
  122. for (var ext in this.types) {
  123. assert.equal(ext, this.extensions[this.types[ext]], '${ext} does not have consistent ext->type->ext mapping');
  124. }
  125. });
  126. it('MDN types', function() {
  127. // MDN types listed at https://goo.gl/lHrFU6
  128. var MDN = {
  129. 'aac': 'audio/aac',
  130. 'abw': 'application/x-abiword',
  131. 'arc': 'application/octet-stream',
  132. 'avi': 'video/x-msvideo',
  133. 'azw': 'application/vnd.amazon.ebook',
  134. 'bin': 'application/octet-stream',
  135. 'bz': 'application/x-bzip',
  136. 'bz2': 'application/x-bzip2',
  137. 'csh': 'application/x-csh',
  138. 'css': 'text/css',
  139. 'csv': 'text/csv',
  140. 'doc': 'application/msword',
  141. 'epub': 'application/epub+zip',
  142. 'gif': 'image/gif',
  143. 'html': 'text/html',
  144. 'ico': 'image/x-icon',
  145. 'ics': 'text/calendar',
  146. 'jar': 'application/java-archive',
  147. 'jpg': 'image/jpeg',
  148. 'js': 'application/javascript',
  149. 'json': 'application/json',
  150. 'midi': 'audio/midi',
  151. 'mpeg': 'video/mpeg',
  152. 'mpkg': 'application/vnd.apple.installer+xml',
  153. 'odp': 'application/vnd.oasis.opendocument.presentation',
  154. 'ods': 'application/vnd.oasis.opendocument.spreadsheet',
  155. 'odt': 'application/vnd.oasis.opendocument.text',
  156. 'oga': 'audio/ogg',
  157. 'ogv': 'video/ogg',
  158. 'ogx': 'application/ogg',
  159. 'png': 'image/png',
  160. 'pdf': 'application/pdf',
  161. 'ppt': 'application/vnd.ms-powerpoint',
  162. 'rar': 'application/x-rar-compressed',
  163. 'rtf': 'application/rtf',
  164. 'sh': 'application/x-sh',
  165. 'svg': 'image/svg+xml',
  166. 'swf': 'application/x-shockwave-flash',
  167. 'tar': 'application/x-tar',
  168. 'tiff': 'image/tiff',
  169. 'ttf': 'font/ttf',
  170. 'vsd': 'application/vnd.visio',
  171. 'wav': 'audio/x-wav',
  172. 'weba': 'audio/webm',
  173. 'webm': 'video/webm',
  174. 'webp': 'image/webp',
  175. 'woff': 'font/woff',
  176. 'woff2': 'font/woff2',
  177. 'xhtml': 'application/xhtml+xml',
  178. 'xls': 'application/vnd.ms-excel',
  179. 'xml': 'application/xml',
  180. 'xul': 'application/vnd.mozilla.xul+xml',
  181. 'zip': 'application/zip',
  182. '3gp': 'video/3gpp',
  183. '3g2': 'video/3gpp2',
  184. '7z': 'application/x-7z-compressed',
  185. };
  186. for (var ext in MDN) {
  187. var expected = MDN[ext];
  188. var actual = mime.getType(ext);
  189. if (actual !== expected) diffs.push(['MDN', ext, expected, actual]);
  190. }
  191. for (var ext in mimeTypes.types) {
  192. var expected = mimeTypes.types[ext];
  193. var actual = mime.getType(ext);
  194. if (actual !== expected) diffs.push(['mime-types', ext, expected, actual]);
  195. }
  196. });
  197. it('Specific types', function() {
  198. // Assortment of types we sanity check for good measure
  199. assert.equal(mime.getType('html'), 'text/html');
  200. assert.equal(mime.getType('js'), 'application/javascript');
  201. assert.equal(mime.getType('json'), 'application/json');
  202. assert.equal(mime.getType('rtf'), 'application/rtf');
  203. assert.equal(mime.getType('txt'), 'text/plain');
  204. assert.equal(mime.getType('xml'), 'application/xml');
  205. assert.equal(mime.getType('wasm'), 'application/wasm');
  206. });
  207. it('Specific extensions', function() {
  208. assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html');
  209. assert.equal(mime.getExtension('text/HTML; charset=UTF-8'), 'html');
  210. assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html');
  211. assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html');
  212. assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html');
  213. assert.equal(mime.getExtension(mime._types.text), 'txt');
  214. assert.equal(mime.getExtension(mime._types.htm), 'html');
  215. assert.equal(mime.getExtension('application/octet-stream'), 'bin');
  216. assert.equal(mime.getExtension('application/octet-stream '), 'bin');
  217. assert.equal(mime.getExtension(' text/html; charset=UTF-8'), 'html');
  218. assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html');
  219. assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html');
  220. assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html');
  221. assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html');
  222. assert.equal(mime.getExtension('text/Html;charset=UTF-8'), 'html');
  223. assert.equal(mime.getExtension('unrecognized'), null);
  224. assert.equal(mime.getExtension('text/xml'), 'xml'); // See #180
  225. });
  226. });