@@ -36,20 +36,28 @@ def setUp(self):
3636
3737 def test_case_sensitivity (self ):
3838 eq = self .assertEqual
39- eq (self .db .guess_type ("foobar.HTML" ), self .db .guess_type ("foobar.html" ))
40- eq (self .db .guess_type ("foobar.TGZ" ), self .db .guess_type ("foobar.tgz" ))
41- eq (self .db .guess_type ("foobar.tar.Z" ), ("application/x-tar" , "compress" ))
42- eq (self .db .guess_type ("foobar.tar.z" ), (None , None ))
39+ eq (self .db .guess_file_type ("foobar.html" ), ("text/html" , None ))
40+ eq (self .db .guess_type ("scheme:foobar.html" ), ("text/html" , None ))
41+ eq (self .db .guess_file_type ("foobar.HTML" ), ("text/html" , None ))
42+ eq (self .db .guess_type ("scheme:foobar.HTML" ), ("text/html" , None ))
43+ eq (self .db .guess_file_type ("foobar.tgz" ), ("application/x-tar" , "gzip" ))
44+ eq (self .db .guess_type ("scheme:foobar.tgz" ), ("application/x-tar" , "gzip" ))
45+ eq (self .db .guess_file_type ("foobar.TGZ" ), ("application/x-tar" , "gzip" ))
46+ eq (self .db .guess_type ("scheme:foobar.TGZ" ), ("application/x-tar" , "gzip" ))
47+ eq (self .db .guess_file_type ("foobar.tar.Z" ), ("application/x-tar" , "compress" ))
48+ eq (self .db .guess_type ("scheme:foobar.tar.Z" ), ("application/x-tar" , "compress" ))
49+ eq (self .db .guess_file_type ("foobar.tar.z" ), (None , None ))
50+ eq (self .db .guess_type ("scheme:foobar.tar.z" ), (None , None ))
4351
4452 def test_default_data (self ):
4553 eq = self .assertEqual
46- eq (self .db .guess_type ("foo.html" ), ("text/html" , None ))
47- eq (self .db .guess_type ("foo.HTML" ), ("text/html" , None ))
48- eq (self .db .guess_type ("foo.tgz" ), ("application/x-tar" , "gzip" ))
49- eq (self .db .guess_type ("foo.tar.gz" ), ("application/x-tar" , "gzip" ))
50- eq (self .db .guess_type ("foo.tar.Z" ), ("application/x-tar" , "compress" ))
51- eq (self .db .guess_type ("foo.tar.bz2" ), ("application/x-tar" , "bzip2" ))
52- eq (self .db .guess_type ("foo.tar.xz" ), ("application/x-tar" , "xz" ))
54+ eq (self .db .guess_file_type ("foo.html" ), ("text/html" , None ))
55+ eq (self .db .guess_file_type ("foo.HTML" ), ("text/html" , None ))
56+ eq (self .db .guess_file_type ("foo.tgz" ), ("application/x-tar" , "gzip" ))
57+ eq (self .db .guess_file_type ("foo.tar.gz" ), ("application/x-tar" , "gzip" ))
58+ eq (self .db .guess_file_type ("foo.tar.Z" ), ("application/x-tar" , "compress" ))
59+ eq (self .db .guess_file_type ("foo.tar.bz2" ), ("application/x-tar" , "bzip2" ))
60+ eq (self .db .guess_file_type ("foo.tar.xz" ), ("application/x-tar" , "xz" ))
5361
5462 def test_data_urls (self ):
5563 eq = self .assertEqual
@@ -63,7 +71,7 @@ def test_file_parsing(self):
6371 eq = self .assertEqual
6472 sio = io .StringIO ("x-application/x-unittest pyunit\n " )
6573 self .db .readfp (sio )
66- eq (self .db .guess_type ("foo.pyunit" ),
74+ eq (self .db .guess_file_type ("foo.pyunit" ),
6775 ("x-application/x-unittest" , None ))
6876 eq (self .db .guess_extension ("x-application/x-unittest" ), ".pyunit" )
6977
@@ -95,12 +103,12 @@ def test_read_mime_types(self):
95103 def test_non_standard_types (self ):
96104 eq = self .assertEqual
97105 # First try strict
98- eq (self .db .guess_type ('foo.xul' , strict = True ), (None , None ))
106+ eq (self .db .guess_file_type ('foo.xul' , strict = True ), (None , None ))
99107 eq (self .db .guess_extension ('image/jpg' , strict = True ), None )
100108 # And then non-strict
101- eq (self .db .guess_type ('foo.xul' , strict = False ), ('text/xul' , None ))
102- eq (self .db .guess_type ('foo.XUL' , strict = False ), ('text/xul' , None ))
103- eq (self .db .guess_type ('foo.invalid' , strict = False ), (None , None ))
109+ eq (self .db .guess_file_type ('foo.xul' , strict = False ), ('text/xul' , None ))
110+ eq (self .db .guess_file_type ('foo.XUL' , strict = False ), ('text/xul' , None ))
111+ eq (self .db .guess_file_type ('foo.invalid' , strict = False ), (None , None ))
104112 eq (self .db .guess_extension ('image/jpg' , strict = False ), '.jpg' )
105113 eq (self .db .guess_extension ('image/JPG' , strict = False ), '.jpg' )
106114
@@ -124,15 +132,26 @@ def test_filename_with_url_delimiters(self):
124132 '//share/server/' , '\\ \\ share\\ server\\ ' ):
125133 path = prefix + name
126134 with self .subTest (path = path ):
135+ eq (self .db .guess_file_type (path ), gzip_expected )
127136 eq (self .db .guess_type (path ), gzip_expected )
128137 expected = (None , None ) if os .name == 'nt' else gzip_expected
129138 for prefix in ('//' , '\\ \\ ' , '//share/' , '\\ \\ share\\ ' ):
130139 path = prefix + name
131140 with self .subTest (path = path ):
141+ eq (self .db .guess_file_type (path ), expected )
132142 eq (self .db .guess_type (path ), expected )
143+ eq (self .db .guess_file_type (r" \"\`;b&b&c |.tar.gz" ), gzip_expected )
133144 eq (self .db .guess_type (r" \"\`;b&b&c |.tar.gz" ), gzip_expected )
134145
146+ eq (self .db .guess_file_type (r'foo/.tar.gz' ), (None , 'gzip' ))
147+ eq (self .db .guess_type (r'foo/.tar.gz' ), (None , 'gzip' ))
148+ expected = (None , 'gzip' ) if os .name == 'nt' else gzip_expected
149+ eq (self .db .guess_file_type (r'foo\.tar.gz' ), expected )
150+ eq (self .db .guess_type (r'foo\.tar.gz' ), expected )
151+ eq (self .db .guess_type (r'scheme:foo\.tar.gz' ), gzip_expected )
152+
135153 def test_url (self ):
154+ result = self .db .guess_type ('http://example.com/host.html' )
136155 result = self .db .guess_type ('http://host.html' )
137156 msg = 'URL only has a host name, not a file'
138157 self .assertSequenceEqual (result , (None , None ), msg )
@@ -240,22 +259,38 @@ def test_init_stability(self):
240259
241260 def test_path_like_ob (self ):
242261 filename = "LICENSE.txt"
243- filepath = pathlib . Path (filename )
244- filepath_with_abs_dir = pathlib . Path ('/dir/' + filename )
245- filepath_relative = pathlib . Path ('../dir/' + filename )
246- path_dir = pathlib . Path ('./' )
262+ filepath = os_helper . FakePath (filename )
263+ filepath_with_abs_dir = os_helper . FakePath ('/dir/' + filename )
264+ filepath_relative = os_helper . FakePath ('../dir/' + filename )
265+ path_dir = os_helper . FakePath ('./' )
247266
248- expected = self .db .guess_type (filename )
267+ expected = self .db .guess_file_type (filename )
249268
269+ self .assertEqual (self .db .guess_file_type (filepath ), expected )
250270 self .assertEqual (self .db .guess_type (filepath ), expected )
271+ self .assertEqual (self .db .guess_file_type (
272+ filepath_with_abs_dir ), expected )
251273 self .assertEqual (self .db .guess_type (
252274 filepath_with_abs_dir ), expected )
275+ self .assertEqual (self .db .guess_file_type (filepath_relative ), expected )
253276 self .assertEqual (self .db .guess_type (filepath_relative ), expected )
277+
278+ self .assertEqual (self .db .guess_file_type (path_dir ), (None , None ))
254279 self .assertEqual (self .db .guess_type (path_dir ), (None , None ))
255280
281+ def test_bytes_path (self ):
282+ self .assertEqual (self .db .guess_file_type (b'foo.html' ),
283+ self .db .guess_file_type ('foo.html' ))
284+ self .assertEqual (self .db .guess_file_type (b'foo.tar.gz' ),
285+ self .db .guess_file_type ('foo.tar.gz' ))
286+ self .assertEqual (self .db .guess_file_type (b'foo.tgz' ),
287+ self .db .guess_file_type ('foo.tgz' ))
288+
256289 def test_keywords_args_api (self ):
290+ self .assertEqual (self .db .guess_file_type (
291+ path = "foo.html" , strict = True ), ("text/html" , None ))
257292 self .assertEqual (self .db .guess_type (
258- url = "foo.html" , strict = True ), ("text/html" , None ))
293+ url = "scheme: foo.html" , strict = True ), ("text/html" , None ))
259294 self .assertEqual (self .db .guess_all_extensions (
260295 type = 'image/jpg' , strict = True ), [])
261296 self .assertEqual (self .db .guess_extension (
0 commit comments