/Lib/test/test_mimetypes.py

http://unladen-swallow.googlecode.com/ · Python · 70 lines · 49 code · 13 blank · 8 comment · 1 complexity · 01835e46680fa27401ac368d81fbd717 MD5 · raw file

  1. import mimetypes
  2. import StringIO
  3. import unittest
  4. from test import test_support
  5. # Tell it we don't know about external files:
  6. mimetypes.knownfiles = []
  7. mimetypes.inited = False
  8. mimetypes._default_mime_types()
  9. class MimeTypesTestCase(unittest.TestCase):
  10. def setUp(self):
  11. self.db = mimetypes.MimeTypes()
  12. def test_default_data(self):
  13. eq = self.assertEqual
  14. eq(self.db.guess_type("foo.html"), ("text/html", None))
  15. eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
  16. eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
  17. eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
  18. def test_data_urls(self):
  19. eq = self.assertEqual
  20. guess_type = self.db.guess_type
  21. eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
  22. eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
  23. eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
  24. def test_file_parsing(self):
  25. eq = self.assertEqual
  26. sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
  27. self.db.readfp(sio)
  28. eq(self.db.guess_type("foo.pyunit"),
  29. ("x-application/x-unittest", None))
  30. eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
  31. def test_non_standard_types(self):
  32. eq = self.assertEqual
  33. # First try strict
  34. eq(self.db.guess_type('foo.xul', strict=True), (None, None))
  35. eq(self.db.guess_extension('image/jpg', strict=True), None)
  36. # And then non-strict
  37. eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
  38. eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
  39. def test_guess_all_types(self):
  40. eq = self.assertEqual
  41. unless = self.failUnless
  42. # First try strict. Use a set here for testing the results because if
  43. # test_urllib2 is run before test_mimetypes, global state is modified
  44. # such that the 'all' set will have more items in it.
  45. all = set(self.db.guess_all_extensions('text/plain', strict=True))
  46. unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
  47. # And now non-strict
  48. all = self.db.guess_all_extensions('image/jpg', strict=False)
  49. all.sort()
  50. eq(all, ['.jpg'])
  51. # And now for no hits
  52. all = self.db.guess_all_extensions('image/jpg', strict=True)
  53. eq(all, [])
  54. def test_main():
  55. test_support.run_unittest(MimeTypesTestCase)
  56. if __name__ == "__main__":
  57. test_main()