You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.1 KiB

7 years ago
  1. /**
  2. * Usage: node test.js
  3. */
  4. var mime = require('../mime');
  5. var assert = require('assert');
  6. var path = require('path');
  7. //
  8. // Test mime lookups
  9. //
  10. assert.equal('text/plain', mime.lookup('text.txt')); // normal file
  11. assert.equal('text/plain', mime.lookup('TEXT.TXT')); // uppercase
  12. assert.equal('text/plain', mime.lookup('dir/text.txt')); // dir + file
  13. assert.equal('text/plain', mime.lookup('.text.txt')); // hidden file
  14. assert.equal('text/plain', mime.lookup('.txt')); // nameless
  15. assert.equal('text/plain', mime.lookup('txt')); // extension-only
  16. assert.equal('text/plain', mime.lookup('/txt')); // extension-less ()
  17. assert.equal('text/plain', mime.lookup('\\txt')); // Windows, extension-less
  18. assert.equal('application/octet-stream', mime.lookup('text.nope')); // unrecognized
  19. assert.equal('fallback', mime.lookup('text.fallback', 'fallback')); // alternate default
  20. //
  21. // Test extensions
  22. //
  23. assert.equal('txt', mime.extension(mime.types.text));
  24. assert.equal('html', mime.extension(mime.types.htm));
  25. assert.equal('bin', mime.extension('application/octet-stream'));
  26. assert.equal('bin', mime.extension('application/octet-stream '));
  27. assert.equal('html', mime.extension(' text/html; charset=UTF-8'));
  28. assert.equal('html', mime.extension('text/html; charset=UTF-8 '));
  29. assert.equal('html', mime.extension('text/html; charset=UTF-8'));
  30. assert.equal('html', mime.extension('text/html ; charset=UTF-8'));
  31. assert.equal('html', mime.extension('text/html;charset=UTF-8'));
  32. assert.equal('html', mime.extension('text/Html;charset=UTF-8'));
  33. assert.equal(undefined, mime.extension('unrecognized'));
  34. //
  35. // Test node.types lookups
  36. //
  37. assert.equal('application/font-woff', mime.lookup('file.woff'));
  38. assert.equal('application/octet-stream', mime.lookup('file.buffer'));
  39. assert.equal('audio/mp4', mime.lookup('file.m4a'));
  40. assert.equal('font/opentype', mime.lookup('file.otf'));
  41. //
  42. // Test charsets
  43. //
  44. assert.equal('UTF-8', mime.charsets.lookup('text/plain'));
  45. assert.equal(undefined, mime.charsets.lookup(mime.types.js));
  46. assert.equal('fallback', mime.charsets.lookup('application/octet-stream', 'fallback'));
  47. console.log('\nAll tests passed');