from test
import test_support
# Tell it we don't know about external files:
mimetypes
.knownfiles
= []
class MimeTypesTestCase(unittest
.TestCase
):
self
.db
= mimetypes
.MimeTypes()
def test_default_data(self
):
eq(self
.db
.guess_type("foo.html"), ("text/html", None))
eq(self
.db
.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
eq(self
.db
.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
eq(self
.db
.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
def test_data_urls(self
):
guess_type
= self
.db
.guess_type
eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
def test_file_parsing(self
):
sio
= StringIO
.StringIO("x-application/x-unittest pyunit\n")
eq(self
.db
.guess_type("foo.pyunit"),
("x-application/x-unittest", None))
eq(self
.db
.guess_extension("x-application/x-unittest"), ".pyunit")
def test_non_standard_types(self
):
eq(self
.db
.guess_type('foo.xul', strict
=True), (None, None))
eq(self
.db
.guess_extension('image/jpg', strict
=True), None)
eq(self
.db
.guess_type('foo.xul', strict
=False), ('text/xul', None))
eq(self
.db
.guess_extension('image/jpg', strict
=False), '.jpg')
def test_guess_all_types(self
):
# First try strict. Use a set here for testing the results because if
# test_urllib2 is run before test_mimetypes, global state is modified
# such that the 'all' set will have more items in it.
all
= Set(self
.db
.guess_all_extensions('text/plain', strict
=True))
unless(all
>= Set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
all
= self
.db
.guess_all_extensions('image/jpg', strict
=False)
all
= self
.db
.guess_all_extensions('image/jpg', strict
=True)
test_support
.run_unittest(MimeTypesTestCase
)
if __name__
== "__main__":