summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Zwerschke <cito@online.de>2010-08-19 18:22:46 +0200
committerChristoph Zwerschke <cito@online.de>2010-08-19 18:22:46 +0200
commita5a05c6ed6710823879e3443e5c74886ddbc8a4f (patch)
treefc796a1f419b8d3386addda71971949e41973f8d
parent7a2cde4d6e1f19088652f5048662ce3e84e7a454 (diff)
downloadpaste-a5a05c6ed6710823879e3443e5c74886ddbc8a4f.tar.gz
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.
-rw-r--r--paste/util/mimeparse.py10
-rw-r--r--tests/test_util/test_mimeparse.py64
2 files changed, 66 insertions, 8 deletions
diff --git a/paste/util/mimeparse.py b/paste/util/mimeparse.py
index 238fcdc..01dbe80 100644
--- a/paste/util/mimeparse.py
+++ b/paste/util/mimeparse.py
@@ -6,18 +6,20 @@ the HTTP specification [RFC 2616] for a complete explanation.
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
diff --git a/tests/test_util/test_mimeparse.py b/tests/test_util/test_mimeparse.py
index c24b5e5..c2d6cdf 100644
--- a/tests/test_util/test_mimeparse.py
+++ b/tests/test_util/test_mimeparse.py
@@ -16,6 +16,16 @@ def test_parse_mime_type():
'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
@@ -35,6 +45,10 @@ def test_parse_illformed_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
@@ -60,6 +74,16 @@ def test_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
@@ -132,17 +156,29 @@ def test_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
@@ -159,6 +195,26 @@ def test_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