Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | import mimetypes |
2 | import StringIO | |
3 | import unittest | |
4 | from sets import Set | |
5 | ||
6 | from test import test_support | |
7 | ||
8 | # Tell it we don't know about external files: | |
9 | mimetypes.knownfiles = [] | |
10 | mimetypes.inited = False | |
11 | ||
12 | ||
13 | class MimeTypesTestCase(unittest.TestCase): | |
14 | def setUp(self): | |
15 | self.db = mimetypes.MimeTypes() | |
16 | ||
17 | def test_default_data(self): | |
18 | eq = self.assertEqual | |
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")) | |
23 | ||
24 | def test_data_urls(self): | |
25 | eq = self.assertEqual | |
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)) | |
30 | ||
31 | def test_file_parsing(self): | |
32 | eq = self.assertEqual | |
33 | sio = StringIO.StringIO("x-application/x-unittest pyunit\n") | |
34 | self.db.readfp(sio) | |
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") | |
38 | ||
39 | def test_non_standard_types(self): | |
40 | eq = self.assertEqual | |
41 | # First try strict | |
42 | eq(self.db.guess_type('foo.xul', strict=True), (None, None)) | |
43 | eq(self.db.guess_extension('image/jpg', strict=True), None) | |
44 | # And then non-strict | |
45 | eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None)) | |
46 | eq(self.db.guess_extension('image/jpg', strict=False), '.jpg') | |
47 | ||
48 | def test_guess_all_types(self): | |
49 | eq = self.assertEqual | |
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'])) | |
56 | # And now non-strict | |
57 | all = self.db.guess_all_extensions('image/jpg', strict=False) | |
58 | all.sort() | |
59 | eq(all, ['.jpg']) | |
60 | # And now for no hits | |
61 | all = self.db.guess_all_extensions('image/jpg', strict=True) | |
62 | eq(all, []) | |
63 | ||
64 | ||
65 | def test_main(): | |
66 | test_support.run_unittest(MimeTypesTestCase) | |
67 | ||
68 | ||
69 | if __name__ == "__main__": | |
70 | test_main() |