Commits

Christoph Zwerschke  committed df4b961

Added some more test cases to test_mimeparse that I have found here:
http://code.google.com/p/mimeparse/source/browse/trunk/testdata.json
Also added a link to the project upstream.

  • Participants
  • Parent commits 52ab408

Comments (0)

Files changed (2)

File paste/util/mimeparse.py

 
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
 
+Based on mime_parse 0.1.2 by Joe Gregorio
+
+    http://code.google.com/p/mimeparse/
+
 Contents:
     - parse_mime_type():   Parses a mime-type into its component parts.
     - parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q' quality parameter.
     - quality():           Determines the quality ('q') of a mime-type when compared against a list of media-ranges.
     - quality_parsed():    Just like quality() except the second parameter must be pre-parsed.
     - best_match():        Choose the mime-type with the highest quality ('q') from a list of candidates.
+    - desired_matches():   Filter against a list of desired mime-types in the order the server prefers.
+
 """
 
-__version__ = "0.1.2"
-__author__ = 'Joe Gregorio'
-__email__ = "joe@bitworking.org"
-__credits__ = ""
 
 def parse_mime_type(mime_type):
     """Carves up a mime-type and returns a tuple of the

File tests/test_util/test_mimeparse.py

         'image', 'gif', {'level': '2', 'q': '0.4'})
     assert parse('application/xhtml;level=3;q=0.5') == (
         'application', 'xhtml', {'level': '3', 'q': '0.5'})
+    assert parse('application/xml') == ('application', 'xml', {})
+    assert parse('application/xml;q=1') == ('application', 'xml', {'q': '1'})
+    assert parse('application/xml ; q=1;b=other') == (
+        'application', 'xml', {'q': '1', 'b': 'other'})
+    assert parse('application/xml ; q=2;b=other') == (
+        'application', 'xml', {'q': '2', 'b': 'other'})
+    assert parse('application/xhtml;q=0.5') == (
+        'application', 'xhtml', {'q': '0.5'})
+    assert parse('application/xhtml;q=0.5;ver=1.2') == (
+        'application', 'xhtml', {'q': '0.5', 'ver': '1.2'})
 
 def test_parse_illformed_mime_type():
     parse = parse_mime_type
     assert parse('*;q=foobar') == ('*', '*', {'q': 'foobar'})
     assert parse('image/gif; level=2; q=2') == (
         'image', 'gif', {'level': '2', 'q': '2'})
+    assert parse('application/xml;q=') == ('application', 'xml', {})
+    assert parse('application/xml ;q=') == ('application', 'xml', {})
+    assert parse(' *; q =;') == ('*', '*', {})
+    assert parse(' *; q=.2') == ('*', '*', {'q': '.2'})
 
 def test_parse_media_range():
     parse = parse_media_range
         'image', 'gif', {'level': '2', 'q': '0.5'})
     assert parse('image/gif; level=2; q=2') == (
         'image', 'gif', {'level': '2', 'q': '1'})
+    assert parse('application/xml') == ('application', 'xml', {'q': '1'})
+    assert parse('application/xml;q=1') == ('application', 'xml', {'q': '1'})
+    assert parse('application/xml;q=') == ('application', 'xml', {'q': '1'})
+    assert parse('application/xml ;q=') == ('application', 'xml', {'q': '1'})
+    assert parse('application/xml ; q=1;b=other') == (
+        'application', 'xml', {'q': '1', 'b': 'other'})
+    assert parse('application/xml ; q=2;b=other') == (
+        'application', 'xml', {'q': '1', 'b': 'other'})
+    assert parse(' *; q =;') == ('*', '*', {'q': '1'})
+    assert parse(' *; q=.2') == ('*', '*', {'q': '.2'})
 
 def test_fitness_and_quality_parsed():
     faq = fitness_and_quality_parsed
 
 def test_quality():
     assert quality('text/html',
-        'text/*;q=0.3, text/html;q=0.7, text/html;level=1,'
-        ' text/html;level=2;q=0.4, */*;q=0.5') == 0.7
+        'text/*;q=0.3, text/html;q=0.75, text/html;level=1,'
+        ' text/html;level=2;q=0.4, */*;q=0.5') == 0.75
     assert quality('text/html;level=2',
         'text/*;q=0.3, text/html;q=0.7, text/html;level=1,'
         ' text/html;level=2;q=0.4, */*;q=0.5') == 0.4
     assert quality('text/plain',
-        'text/*;q=0.3, text/html;q=0.7, text/html;level=1,'
-        ' text/html;level=2;q=0.4, */*;q=0.5') == 0.3
+        'text/*;q=0.25, text/html;q=0.7, text/html;level=1,'
+        ' text/html;level=2;q=0.4, */*;q=0.5') == 0.25
     assert quality('plain/text',
         'text/*;q=0.3, text/html;q=0.7, text/html;level=1,'
         ' text/html;level=2;q=0.4, */*;q=0.5') == 0.5
+    assert quality('text/html;level=1',
+        'text/*;q=0.3, text/html;q=0.7, text/html;level=1,'
+        ' text/html;level=2;q=0.4, */*;q=0.5') == 1
+    assert quality('image/jpeg',
+        'text/*;q=0.3, text/html;q=0.7, text/html;level=1,'
+        ' text/html;level=2;q=0.4, */*;q=0.5') == 0.5
+    assert quality('text/html;level=2',
+        'text/*;q=0.3, text/html;q=0.7, text/html;level=1,'
+        ' text/html;level=2;q=0.375, */*;q=0.5') == 0.375
+    assert quality('text/html;level=3',
+        'text/*;q=0.3, text/html;q=0.75, text/html;level=1,'
+        ' text/html;level=2;q=0.4, */*;q=0.5') == 0.75
 
 def test_best_match():
     bm = best_match
         'text/*;q=0.1,*/xhtml; q=0.5') == 'text/html'
     assert bm(['application/xbel+xml', 'text/html', 'text/xhtml'],
         '*/html;q=0.1,*/xhtml; q=0.5') == 'text/xhtml'
+    assert bm(['application/xbel+xml', 'application/xml'],
+        'application/xbel+xml') == 'application/xbel+xml'
+    assert bm(['application/xbel+xml', 'application/xml'],
+        'application/xbel+xml; q=1') == 'application/xbel+xml'
+    assert bm(['application/xbel+xml', 'application/xml'],
+        'application/xml; q=1') == 'application/xml'
+    assert bm(['application/xbel+xml', 'application/xml'],
+        'application/*; q=1') == 'application/xbel+xml'
+    assert bm(['application/xbel+xml', 'application/xml'],
+        '*/*, application/xml') == 'application/xml'
+    assert bm(['application/xbel+xml', 'text/xml'],
+        'text/*;q=0.5,*/*; q=0.1') == 'text/xml'
+    assert bm(['application/xbel+xml', 'text/xml'],
+        'text/html,application/atom+xml; q=0.9') == ''
+    assert bm(['application/json', 'text/html'],
+        'application/json, text/javascript, */*') == 'application/json'
+    assert bm(['application/json', 'text/html'],
+        'application/json, text/html;q=0.9') == 'application/json'
+    assert bm(['image/*', 'application/xml'], 'image/png') == 'image/*'
+    assert bm(['image/*', 'application/xml'], 'image/*') == 'image/*'
 
 def test_illformed_best_match():
     bm = best_match