test.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Usage: node test.js
  3. */
  4. var mime = require('./mime');
  5. var assert = require('assert');
  6. var path = require('path');
  7. function eq(a, b) {
  8. console.log('Test: ' + a + ' === ' + b);
  9. assert.strictEqual.apply(null, arguments);
  10. }
  11. console.log(Object.keys(mime.extensions).length + ' types');
  12. console.log(Object.keys(mime.types).length + ' extensions\n');
  13. //
  14. // Test mime lookups
  15. //
  16. eq('text/plain', mime.lookup('text.txt')); // normal file
  17. eq('text/plain', mime.lookup('TEXT.TXT')); // uppercase
  18. eq('text/plain', mime.lookup('dir/text.txt')); // dir + file
  19. eq('text/plain', mime.lookup('.text.txt')); // hidden file
  20. eq('text/plain', mime.lookup('.txt')); // nameless
  21. eq('text/plain', mime.lookup('txt')); // extension-only
  22. eq('text/plain', mime.lookup('/txt')); // extension-less ()
  23. eq('text/plain', mime.lookup('\\txt')); // Windows, extension-less
  24. eq('application/octet-stream', mime.lookup('text.nope')); // unrecognized
  25. eq('fallback', mime.lookup('text.fallback', 'fallback')); // alternate default
  26. //
  27. // Test extensions
  28. //
  29. eq('txt', mime.extension(mime.types.text));
  30. eq('html', mime.extension(mime.types.htm));
  31. eq('bin', mime.extension('application/octet-stream'));
  32. eq('bin', mime.extension('application/octet-stream '));
  33. eq('html', mime.extension(' text/html; charset=UTF-8'));
  34. eq('html', mime.extension('text/html; charset=UTF-8 '));
  35. eq('html', mime.extension('text/html; charset=UTF-8'));
  36. eq('html', mime.extension('text/html ; charset=UTF-8'));
  37. eq('html', mime.extension('text/html;charset=UTF-8'));
  38. eq('html', mime.extension('text/Html;charset=UTF-8'));
  39. eq(undefined, mime.extension('unrecognized'));
  40. //
  41. // Test node.types lookups
  42. //
  43. eq('application/font-woff', mime.lookup('file.woff'));
  44. eq('application/octet-stream', mime.lookup('file.buffer'));
  45. eq('audio/mp4', mime.lookup('file.m4a'));
  46. eq('font/opentype', mime.lookup('file.otf'));
  47. //
  48. // Test charsets
  49. //
  50. eq('UTF-8', mime.charsets.lookup('text/plain'));
  51. eq(undefined, mime.charsets.lookup(mime.types.js));
  52. eq('fallback', mime.charsets.lookup('application/octet-stream', 'fallback'));
  53. //
  54. // Test for overlaps between mime.types and node.types
  55. //
  56. var apacheTypes = new mime.Mime(), nodeTypes = new mime.Mime();
  57. apacheTypes.load(path.join(__dirname, 'types/mime.types'));
  58. nodeTypes.load(path.join(__dirname, 'types/node.types'));
  59. var keys = [].concat(Object.keys(apacheTypes.types))
  60. .concat(Object.keys(nodeTypes.types));
  61. keys.sort();
  62. for (var i = 1; i < keys.length; i++) {
  63. if (keys[i] == keys[i-1]) {
  64. console.warn('Warning: ' +
  65. 'node.types defines ' + keys[i] + '->' + nodeTypes.types[keys[i]] +
  66. ', mime.types defines ' + keys[i] + '->' + apacheTypes.types[keys[i]]);
  67. }
  68. }
  69. console.log('\nOK');