6 from test
import test_support
8 # Tell it we don't know about external files:
9 mimetypes
.knownfiles
= []
10 mimetypes
.inited
= False
11 mimetypes
._default
_mime
_types
()
14 class MimeTypesTestCase(unittest
.TestCase
):
16 self
.db
= mimetypes
.MimeTypes()
18 def test_default_data(self
):
20 eq(self
.db
.guess_type("foo.html"), ("text/html", None))
21 eq(self
.db
.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
22 eq(self
.db
.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
23 eq(self
.db
.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
25 def test_data_urls(self
):
27 guess_type
= self
.db
.guess_type
28 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
29 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
30 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
32 def test_file_parsing(self
):
34 sio
= StringIO
.StringIO("x-application/x-unittest pyunit\n")
36 eq(self
.db
.guess_type("foo.pyunit"),
37 ("x-application/x-unittest", None))
38 eq(self
.db
.guess_extension("x-application/x-unittest"), ".pyunit")
40 def test_non_standard_types(self
):
43 eq(self
.db
.guess_type('foo.xul', strict
=True), (None, None))
44 eq(self
.db
.guess_extension('image/jpg', strict
=True), None)
46 eq(self
.db
.guess_type('foo.xul', strict
=False), ('text/xul', None))
47 eq(self
.db
.guess_extension('image/jpg', strict
=False), '.jpg')
49 def test_guess_all_types(self
):
51 unless
= self
.assertTrue
52 # First try strict. Use a set here for testing the results because if
53 # test_urllib2 is run before test_mimetypes, global state is modified
54 # such that the 'all' set will have more items in it.
55 all
= set(self
.db
.guess_all_extensions('text/plain', strict
=True))
56 unless(all
>= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
58 all
= self
.db
.guess_all_extensions('image/jpg', strict
=False)
62 all
= self
.db
.guess_all_extensions('image/jpg', strict
=True)
66 @unittest.skipUnless(sys
.platform
.startswith("win"), "Windows only")
67 class Win32MimeTypesTestCase(unittest
.TestCase
):
69 # ensure all entries actually come from the Windows registry
70 self
.original_types_map
= mimetypes
.types_map
.copy()
71 mimetypes
.types_map
.clear()
73 self
.db
= mimetypes
.MimeTypes()
76 # restore default settings
77 mimetypes
.types_map
.clear()
78 mimetypes
.types_map
.update(self
.original_types_map
)
80 def test_registry_parsing(self
):
81 # the original, minimum contents of the MIME database in the
82 # Windows registry is undocumented AFAIK.
83 # Use file types that should *always* exist:
85 eq(self
.db
.guess_type("foo.txt"), ("text/plain", None))
88 test_support
.run_unittest(MimeTypesTestCase
,
89 Win32MimeTypesTestCase
93 if __name__
== "__main__":