6 from test
import test_support
8 # Tell it we don't know about external files:
9 mimetypes
.knownfiles
= []
10 mimetypes
.inited
= False
13 class MimeTypesTestCase(unittest
.TestCase
):
15 self
.db
= mimetypes
.MimeTypes()
17 def test_default_data(self
):
19 eq(self
.db
.guess_type("foo.html"), ("text/html", None))
20 eq(self
.db
.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
21 eq(self
.db
.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
22 eq(self
.db
.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
24 def test_data_urls(self
):
26 guess_type
= self
.db
.guess_type
27 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
28 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
29 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
31 def test_file_parsing(self
):
33 sio
= StringIO
.StringIO("x-application/x-unittest pyunit\n")
35 eq(self
.db
.guess_type("foo.pyunit"),
36 ("x-application/x-unittest", None))
37 eq(self
.db
.guess_extension("x-application/x-unittest"), ".pyunit")
39 def test_non_standard_types(self
):
42 eq(self
.db
.guess_type('foo.xul', strict
=True), (None, None))
43 eq(self
.db
.guess_extension('image/jpg', strict
=True), None)
45 eq(self
.db
.guess_type('foo.xul', strict
=False), ('text/xul', None))
46 eq(self
.db
.guess_extension('image/jpg', strict
=False), '.jpg')
48 def test_guess_all_types(self
):
50 unless
= self
.failUnless
51 # First try strict. Use a set here for testing the results because if
52 # test_urllib2 is run before test_mimetypes, global state is modified
53 # such that the 'all' set will have more items in it.
54 all
= Set(self
.db
.guess_all_extensions('text/plain', strict
=True))
55 unless(all
>= Set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
57 all
= self
.db
.guess_all_extensions('image/jpg', strict
=False)
61 all
= self
.db
.guess_all_extensions('image/jpg', strict
=True)
66 test_support
.run_unittest(MimeTypesTestCase
)
69 if __name__
== "__main__":