summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-04-21 15:47:06 +0200
committerVictor Stinner <victor.stinner@gmail.com>2015-04-21 15:47:06 +0200
commit87f53c983e7b94ed1d8389f97c605c6afe5b1c57 (patch)
treec8daef3d8a9adadecc3825ed228b1f1dbba4f596 /tests
parent04ecd5542849191457ccfc3e42a39452db47825a (diff)
downloadpaste-87f53c983e7b94ed1d8389f97c605c6afe5b1c57.tar.gz
Convert files from file format DOS to UNIX
Diffstat (limited to 'tests')
-rw-r--r--tests/test_util/test_datetimeutil.py270
-rw-r--r--tests/test_util/test_mimeparse.py474
2 files changed, 372 insertions, 372 deletions
diff --git a/tests/test_util/test_datetimeutil.py b/tests/test_util/test_datetimeutil.py
index 17808f3..45d96c7 100644
--- a/tests/test_util/test_datetimeutil.py
+++ b/tests/test_util/test_datetimeutil.py
@@ -1,135 +1,135 @@
-# (c) 2005 Clark C. Evans and contributors
-# This module is part of the Python Paste Project and is released under
-# the MIT License: http://www.opensource.org/licenses/mit-license.php
-# Some of this code was funded by: http://prometheusresearch.com
-from time import localtime
-from datetime import date
-from paste.util.datetimeutil import *
-
-def test_timedelta():
- assert('' == normalize_timedelta(""))
- assert('0.10' == normalize_timedelta("6m"))
- assert('0.50' == normalize_timedelta("30m"))
- assert('0.75' == normalize_timedelta("45m"))
- assert('1.00' == normalize_timedelta("60 min"))
- assert('1.50' == normalize_timedelta("90min"))
- assert('1.50' == normalize_timedelta("1.50"))
- assert('4.50' == normalize_timedelta("4 : 30"))
- assert('1.50' == normalize_timedelta("1h 30m"))
- assert('1.00' == normalize_timedelta("1"))
- assert('1.00' == normalize_timedelta("1 hour"))
- assert('8.00' == normalize_timedelta("480 mins"))
- assert('8.00' == normalize_timedelta("8h"))
- assert('0.50' == normalize_timedelta("0.5"))
- assert('0.10' == normalize_timedelta(".1"))
- assert('0.50' == normalize_timedelta(".50"))
- assert('0.75' == normalize_timedelta("0.75"))
-
-def test_time():
- assert('03:00 PM' == normalize_time("3p", ampm=True))
- assert('03:00 AM' == normalize_time("300", ampm=True))
- assert('03:22 AM' == normalize_time("322", ampm=True))
- assert('01:22 PM' == normalize_time("1322", ampm=True))
- assert('01:00 PM' == normalize_time("13", ampm=True))
- assert('12:00 PM' == normalize_time("noon", ampm=True))
- assert("06:00 PM" == normalize_time("6", ampm=True))
- assert("01:00 PM" == normalize_time("1", ampm=True))
- assert("07:00 AM" == normalize_time("7", ampm=True))
- assert("01:00 PM" == normalize_time("1 pm", ampm=True))
- assert("03:30 PM" == normalize_time("3:30 pm", ampm=True))
- assert("03:30 PM" == normalize_time("3 30 pm", ampm=True))
- assert("03:30 PM" == normalize_time("3 30 P.M.", ampm=True))
- assert("12:00 PM" == normalize_time("0", ampm=True))
- assert("12:00 AM" == normalize_time("1200 AM", ampm=True))
-
-def test_date():
- tm = localtime()
- yr = tm[0]
- mo = tm[1]
- assert(date(yr,4,11) == parse_date("411"))
- assert(date(yr,4,11) == parse_date("APR11"))
- assert(date(yr,4,11) == parse_date("11APR"))
- assert(date(yr,4,11) == parse_date("4 11"))
- assert(date(yr,4,11) == parse_date("11 APR"))
- assert(date(yr,4,11) == parse_date("APR 11"))
- assert(date(yr,mo,11) == parse_date("11"))
- assert(date(yr,4,1) == parse_date("APR"))
- assert(date(yr,4,11) == parse_date("4/11"))
- assert(date.today() == parse_date("today"))
- assert(date.today() == parse_date("now"))
- assert(None == parse_date(""))
- assert('' == normalize_date(None))
-
- assert('2001-02-03' == normalize_date("20010203"))
- assert('1999-04-11' == normalize_date("1999 4 11"))
- assert('1999-04-11' == normalize_date("1999 APR 11"))
- assert('1999-04-11' == normalize_date("APR 11 1999"))
- assert('1999-04-11' == normalize_date("11 APR 1999"))
- assert('1999-04-11' == normalize_date("4 11 1999"))
- assert('1999-04-01' == normalize_date("1999 APR"))
- assert('1999-04-01' == normalize_date("1999 4"))
- assert('1999-04-01' == normalize_date("4 1999"))
- assert('1999-04-01' == normalize_date("APR 1999"))
- assert('1999-01-01' == normalize_date("1999"))
-
- assert('1999-04-01' == normalize_date("1APR1999"))
- assert('2001-04-01' == normalize_date("1APR2001"))
-
- assert('1999-04-18' == normalize_date("1999-04-11+7"))
- assert('1999-04-18' == normalize_date("1999-04-11 7"))
- assert('1999-04-01' == normalize_date("1 apr 1999"))
- assert('1999-04-11' == normalize_date("11 apr 1999"))
- assert('1999-04-11' == normalize_date("11 Apr 1999"))
- assert('1999-04-11' == normalize_date("11-apr-1999"))
- assert('1999-04-11' == normalize_date("11 April 1999"))
- assert('1999-04-11' == normalize_date("11 APRIL 1999"))
- assert('1999-04-11' == normalize_date("11 april 1999"))
- assert('1999-04-11' == normalize_date("11 aprick 1999"))
- assert('1999-04-11' == normalize_date("APR 11, 1999"))
- assert('1999-04-11' == normalize_date("4/11/1999"))
- assert('1999-04-11' == normalize_date("4-11-1999"))
- assert('1999-04-11' == normalize_date("1999-4-11"))
- assert('1999-04-11' == normalize_date("19990411"))
-
- assert('1999-01-01' == normalize_date("1 Jan 1999"))
- assert('1999-02-01' == normalize_date("1 Feb 1999"))
- assert('1999-03-01' == normalize_date("1 Mar 1999"))
- assert('1999-04-01' == normalize_date("1 Apr 1999"))
- assert('1999-05-01' == normalize_date("1 May 1999"))
- assert('1999-06-01' == normalize_date("1 Jun 1999"))
- assert('1999-07-01' == normalize_date("1 Jul 1999"))
- assert('1999-08-01' == normalize_date("1 Aug 1999"))
- assert('1999-09-01' == normalize_date("1 Sep 1999"))
- assert('1999-10-01' == normalize_date("1 Oct 1999"))
- assert('1999-11-01' == normalize_date("1 Nov 1999"))
- assert('1999-12-01' == normalize_date("1 Dec 1999"))
-
- assert('1999-04-30' == normalize_date("1999-4-30"))
- assert('2000-02-29' == normalize_date("29 FEB 2000"))
- assert('2001-02-28' == normalize_date("28 FEB 2001"))
- assert('2004-02-29' == normalize_date("29 FEB 2004"))
- assert('2100-02-28' == normalize_date("28 FEB 2100"))
- assert('1900-02-28' == normalize_date("28 FEB 1900"))
-
- def assertError(val):
- try:
- normalize_date(val)
- except (TypeError,ValueError):
- return
- raise ValueError("type error expected", val)
-
- assertError("2000-13-11")
- assertError("APR 99")
- assertError("29 FEB 1900")
- assertError("29 FEB 2100")
- assertError("29 FEB 2001")
- assertError("1999-4-31")
- assertError("APR 99")
- assertError("20301")
- assertError("020301")
- assertError("1APR99")
- assertError("1APR01")
- assertError("1 APR 99")
- assertError("1 APR 01")
- assertError("11/5/01")
-
+# (c) 2005 Clark C. Evans and contributors
+# This module is part of the Python Paste Project and is released under
+# the MIT License: http://www.opensource.org/licenses/mit-license.php
+# Some of this code was funded by: http://prometheusresearch.com
+from time import localtime
+from datetime import date
+from paste.util.datetimeutil import *
+
+def test_timedelta():
+ assert('' == normalize_timedelta(""))
+ assert('0.10' == normalize_timedelta("6m"))
+ assert('0.50' == normalize_timedelta("30m"))
+ assert('0.75' == normalize_timedelta("45m"))
+ assert('1.00' == normalize_timedelta("60 min"))
+ assert('1.50' == normalize_timedelta("90min"))
+ assert('1.50' == normalize_timedelta("1.50"))
+ assert('4.50' == normalize_timedelta("4 : 30"))
+ assert('1.50' == normalize_timedelta("1h 30m"))
+ assert('1.00' == normalize_timedelta("1"))
+ assert('1.00' == normalize_timedelta("1 hour"))
+ assert('8.00' == normalize_timedelta("480 mins"))
+ assert('8.00' == normalize_timedelta("8h"))
+ assert('0.50' == normalize_timedelta("0.5"))
+ assert('0.10' == normalize_timedelta(".1"))
+ assert('0.50' == normalize_timedelta(".50"))
+ assert('0.75' == normalize_timedelta("0.75"))
+
+def test_time():
+ assert('03:00 PM' == normalize_time("3p", ampm=True))
+ assert('03:00 AM' == normalize_time("300", ampm=True))
+ assert('03:22 AM' == normalize_time("322", ampm=True))
+ assert('01:22 PM' == normalize_time("1322", ampm=True))
+ assert('01:00 PM' == normalize_time("13", ampm=True))
+ assert('12:00 PM' == normalize_time("noon", ampm=True))
+ assert("06:00 PM" == normalize_time("6", ampm=True))
+ assert("01:00 PM" == normalize_time("1", ampm=True))
+ assert("07:00 AM" == normalize_time("7", ampm=True))
+ assert("01:00 PM" == normalize_time("1 pm", ampm=True))
+ assert("03:30 PM" == normalize_time("3:30 pm", ampm=True))
+ assert("03:30 PM" == normalize_time("3 30 pm", ampm=True))
+ assert("03:30 PM" == normalize_time("3 30 P.M.", ampm=True))
+ assert("12:00 PM" == normalize_time("0", ampm=True))
+ assert("12:00 AM" == normalize_time("1200 AM", ampm=True))
+
+def test_date():
+ tm = localtime()
+ yr = tm[0]
+ mo = tm[1]
+ assert(date(yr,4,11) == parse_date("411"))
+ assert(date(yr,4,11) == parse_date("APR11"))
+ assert(date(yr,4,11) == parse_date("11APR"))
+ assert(date(yr,4,11) == parse_date("4 11"))
+ assert(date(yr,4,11) == parse_date("11 APR"))
+ assert(date(yr,4,11) == parse_date("APR 11"))
+ assert(date(yr,mo,11) == parse_date("11"))
+ assert(date(yr,4,1) == parse_date("APR"))
+ assert(date(yr,4,11) == parse_date("4/11"))
+ assert(date.today() == parse_date("today"))
+ assert(date.today() == parse_date("now"))
+ assert(None == parse_date(""))
+ assert('' == normalize_date(None))
+
+ assert('2001-02-03' == normalize_date("20010203"))
+ assert('1999-04-11' == normalize_date("1999 4 11"))
+ assert('1999-04-11' == normalize_date("1999 APR 11"))
+ assert('1999-04-11' == normalize_date("APR 11 1999"))
+ assert('1999-04-11' == normalize_date("11 APR 1999"))
+ assert('1999-04-11' == normalize_date("4 11 1999"))
+ assert('1999-04-01' == normalize_date("1999 APR"))
+ assert('1999-04-01' == normalize_date("1999 4"))
+ assert('1999-04-01' == normalize_date("4 1999"))
+ assert('1999-04-01' == normalize_date("APR 1999"))
+ assert('1999-01-01' == normalize_date("1999"))
+
+ assert('1999-04-01' == normalize_date("1APR1999"))
+ assert('2001-04-01' == normalize_date("1APR2001"))
+
+ assert('1999-04-18' == normalize_date("1999-04-11+7"))
+ assert('1999-04-18' == normalize_date("1999-04-11 7"))
+ assert('1999-04-01' == normalize_date("1 apr 1999"))
+ assert('1999-04-11' == normalize_date("11 apr 1999"))
+ assert('1999-04-11' == normalize_date("11 Apr 1999"))
+ assert('1999-04-11' == normalize_date("11-apr-1999"))
+ assert('1999-04-11' == normalize_date("11 April 1999"))
+ assert('1999-04-11' == normalize_date("11 APRIL 1999"))
+ assert('1999-04-11' == normalize_date("11 april 1999"))
+ assert('1999-04-11' == normalize_date("11 aprick 1999"))
+ assert('1999-04-11' == normalize_date("APR 11, 1999"))
+ assert('1999-04-11' == normalize_date("4/11/1999"))
+ assert('1999-04-11' == normalize_date("4-11-1999"))
+ assert('1999-04-11' == normalize_date("1999-4-11"))
+ assert('1999-04-11' == normalize_date("19990411"))
+
+ assert('1999-01-01' == normalize_date("1 Jan 1999"))
+ assert('1999-02-01' == normalize_date("1 Feb 1999"))
+ assert('1999-03-01' == normalize_date("1 Mar 1999"))
+ assert('1999-04-01' == normalize_date("1 Apr 1999"))
+ assert('1999-05-01' == normalize_date("1 May 1999"))
+ assert('1999-06-01' == normalize_date("1 Jun 1999"))
+ assert('1999-07-01' == normalize_date("1 Jul 1999"))
+ assert('1999-08-01' == normalize_date("1 Aug 1999"))
+ assert('1999-09-01' == normalize_date("1 Sep 1999"))
+ assert('1999-10-01' == normalize_date("1 Oct 1999"))
+ assert('1999-11-01' == normalize_date("1 Nov 1999"))
+ assert('1999-12-01' == normalize_date("1 Dec 1999"))
+
+ assert('1999-04-30' == normalize_date("1999-4-30"))
+ assert('2000-02-29' == normalize_date("29 FEB 2000"))
+ assert('2001-02-28' == normalize_date("28 FEB 2001"))
+ assert('2004-02-29' == normalize_date("29 FEB 2004"))
+ assert('2100-02-28' == normalize_date("28 FEB 2100"))
+ assert('1900-02-28' == normalize_date("28 FEB 1900"))
+
+ def assertError(val):
+ try:
+ normalize_date(val)
+ except (TypeError,ValueError):
+ return
+ raise ValueError("type error expected", val)
+
+ assertError("2000-13-11")
+ assertError("APR 99")
+ assertError("29 FEB 1900")
+ assertError("29 FEB 2100")
+ assertError("29 FEB 2001")
+ assertError("1999-4-31")
+ assertError("APR 99")
+ assertError("20301")
+ assertError("020301")
+ assertError("1APR99")
+ assertError("1APR01")
+ assertError("1 APR 99")
+ assertError("1 APR 01")
+ assertError("11/5/01")
+
diff --git a/tests/test_util/test_mimeparse.py b/tests/test_util/test_mimeparse.py
index c2d6cdf..109245a 100644
--- a/tests/test_util/test_mimeparse.py
+++ b/tests/test_util/test_mimeparse.py
@@ -1,237 +1,237 @@
-# (c) 2010 Ch. Zwerschke and contributors
-# This module is part of the Python Paste Project and is released under
-# the MIT License: http://www.opensource.org/licenses/mit-license.php
-
-from time import localtime
-from datetime import date
-from paste.util.mimeparse import *
-
-def test_parse_mime_type():
- parse = parse_mime_type
- assert parse('*/*') == ('*', '*', {})
- assert parse('text/html') == ('text', 'html', {})
- assert parse('audio/*; q=0.2') == ('audio', '*', {'q': '0.2'})
- assert parse('text/x-dvi;level=1') == ('text', 'x-dvi', {'level': '1'})
- assert parse('image/gif; level=2; q=0.4') == (
- '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('*') == ('*', '*', {})
- assert parse('text') == ('text', '*', {})
- assert parse('text/') == ('text', '*', {})
- assert parse('/plain') == ('*', 'plain', {})
- assert parse('/') == ('*', '*', {})
- assert parse('text/plain;') == ('text', 'plain', {})
- assert parse(';q=0.5') == ('*', '*', {'q': '0.5'})
- assert parse('*; q=.2') == ('*', '*', {'q': '.2'})
- assert parse('image; q=.7; level=3') == (
- 'image', '*', {'q': '.7', 'level': '3'})
- assert parse('*;q=1') == ('*', '*', {'q': '1'})
- assert parse('*;q=') == ('*', '*', {})
- assert parse('*;=0.5') == ('*', '*', {})
- 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
- assert parse('application/*;q=0.5') == ('application', '*', {'q': '0.5'})
- assert parse('text/plain') == ('text', 'plain', {'q': '1'})
- assert parse('*') == ('*', '*', {'q': '1'})
- assert parse(';q=0.5') == ('*', '*', {'q': '0.5'})
- assert parse('*;q=0.5') == ('*', '*', {'q': '0.5'})
- assert parse('*;q=1') == ('*', '*', {'q': '1'})
- assert parse('*;q=') == ('*', '*', {'q': '1'})
- assert parse('*;q=-1') == ('*', '*', {'q': '1'})
- assert parse('*;q=foobar') == ('*', '*', {'q': '1'})
- assert parse('*;q=0.0001') == ('*', '*', {'q': '0.0001'})
- assert parse('*;q=1000.0') == ('*', '*', {'q': '1'})
- assert parse('*;q=0') == ('*', '*', {'q': '0'})
- assert parse('*;q=0.0000') == ('*', '*', {'q': '0.0000'})
- assert parse('*;q=1.0001') == ('*', '*', {'q': '1'})
- assert parse('*;q=2') == ('*', '*', {'q': '1'})
- assert parse('*;q=1e3') == ('*', '*', {'q': '1'})
- assert parse('image/gif; level=2') == (
- 'image', 'gif', {'level': '2', 'q': '1'})
- assert parse('image/gif; level=2; q=0.5') == (
- '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
- assert faq('*/*;q=0.7', [
- ('foo', 'bar', {'q': '0.5'})]) == (0, 0.5)
- assert faq('foo/*;q=0.7', [
- ('foo', 'bar', {'q': '0.5'})]) == (100, 0.5)
- assert faq('*/bar;q=0.7', [
- ('foo', 'bar', {'q': '0.5'})]) == (10, 0.5)
- assert faq('foo/bar;q=0.7', [
- ('foo', 'bar', {'q': '0.5'})]) == (110, 0.5)
- assert faq('text/html;q=0.7', [
- ('foo', 'bar', {'q': '0.5'})]) == (-1, 0)
- assert faq('text/html;q=0.7', [
- ('text', 'bar', {'q': '0.5'})]) == (-1, 0)
- assert faq('text/html;q=0.7', [
- ('foo', 'html', {'q': '0.5'})]) == (-1, 0)
- assert faq('text/html;q=0.7', [
- ('text', '*', {'q': '0.5'})]) == (100, 0.5)
- assert faq('text/html;q=0.7', [
- ('*', 'html', {'q': '0.5'})]) == (10, 0.5)
- assert faq('text/html;q=0.7', [
- ('*', '*', {'q': '0'}), ('text', 'html', {'q': '0.5'})]) == (110, 0.5)
- assert faq('text/html;q=0.7', [
- ('*', '*', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (0, 0.5)
- assert faq('audio/mp3;q=0.7', [
- ('*', '*', {'q': '0'}), ('audio', '*', {'q': '0.5'})]) == (100, 0.5)
- assert faq('*/mp3;q=0.7', [
- ('foo', 'mp3', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (10, 0.5)
- assert faq('audio/mp3;q=0.7', [
- ('audio', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5)
- assert faq('audio/mp3;q=0.7', [
- ('*', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5)
- assert faq('text/html;q=0.7', [
- ('text', 'plain', {'q': '0'}),
- ('plain', 'html', {'q': '0'}),
- ('text', 'html', {'q': '0.5'}),
- ('html', 'text', {'q': '0'})]) == (110, 0.5)
- assert faq('text/html;q=0.7;level=2', [
- ('plain', 'html', {'q': '0', 'level': '2'}),
- ('text', '*', {'q': '0.5', 'level': '3'}),
- ('*', 'html', {'q': '0.5', 'level': '2'}),
- ('image', 'gif', {'q': '0.5', 'level': '2'})]) == (100, 0.5)
- assert faq('text/html;q=0.7;level=2', [
- ('text', 'plain', {'q': '0'}), ('text', 'html', {'q': '0'}),
- ('text', 'plain', {'q': '0', 'level': '2'}),
- ('text', 'html', {'q': '0.5', 'level': '2'}),
- ('*', '*', {'q': '0', 'level': '2'}),
- ('text', 'html', {'q': '0', 'level': '3'})]) == (111, 0.5)
- assert faq('text/html;q=0.7;level=2;opt=3', [
- ('text', 'html', {'q': '0'}),
- ('text', 'html', {'q': '0', 'level': '2'}),
- ('text', 'html', {'q': '0', 'opt': '3'}),
- ('*', '*', {'q': '0', 'level': '2', 'opt': '3'}),
- ('text', 'html', {'q': '0', 'level': '3', 'opt': '3'}),
- ('text', 'html', {'q': '0.5', 'level': '2', 'opt': '3'}),
- ('*', '*', {'q': '0', 'level': '3', 'opt': '3'})]) == (112, 0.5)
-
-def test_quality_parsed():
- qp = quality_parsed
- assert qp('image/gif;q=0.7', [('image', 'jpg', {'q': '0.5'})]) == 0
- assert qp('image/gif;q=0.7', [('image', '*', {'q': '0.5'})]) == 0.5
- assert qp('audio/mp3;q=0.7;quality=100', [
- ('*', '*', {'q': '0', 'quality': '100'}),
- ('audio', '*', {'q': '0', 'quality': '100'}),
- ('*', 'mp3', {'q': '0', 'quality': '100'}),
- ('audio', 'mp3', {'q': '0', 'quality': '50'}),
- ('audio', 'mp3', {'q': '0.5', 'quality': '100'}),
- ('audio', 'mp3', {'q': '0.5'})]) == 0.5
-
-def test_quality():
- assert quality('text/html',
- '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.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
- assert bm([], '*/*') == ''
- assert bm(['application/xbel+xml', 'text/xml'],
- 'text/*;q=0.5,*/*; q=0.1') == 'text/xml'
- assert bm(['application/xbel+xml', 'audio/mp3'],
- 'text/*;q=0.5,*/*; q=0.1') == 'application/xbel+xml'
- assert bm(['application/xbel+xml', 'audio/mp3'],
- 'text/*;q=0.5,*/mp3; q=0.1') == 'audio/mp3'
- assert bm(['application/xbel+xml', 'text/plain', 'text/html'],
- 'text/*;q=0.5,*/plain; q=0.1') == 'text/plain'
- assert bm(['application/xbel+xml', 'text/html', 'text/xhtml'],
- '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
- assert bm(['image/png', 'image/jpeg', 'image/gif', 'text/html'],
- 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/jpeg'
- assert bm(['image/png', 'image/jpg', 'image/tif', 'text/html'],
- 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'text/html'
- assert bm(['image/png', 'image/jpg', 'image/tif', 'audio/mp3'],
- 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/png'
-
-def test_sorted_match():
- dm = desired_matches
- assert dm(['text/html', 'application/xml'],
- 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,'
- 'text/plain;q=0.8,image/png') == ['text/html', 'application/xml']
- assert dm(['text/html', 'application/xml'],
- 'application/xml,application/json') == ['application/xml']
- assert dm(['text/xhtml', 'text/plain', 'application/xhtml'],
- 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,'
- 'text/plain;q=0.8,image/png') == ['text/plain']
+# (c) 2010 Ch. Zwerschke and contributors
+# This module is part of the Python Paste Project and is released under
+# the MIT License: http://www.opensource.org/licenses/mit-license.php
+
+from time import localtime
+from datetime import date
+from paste.util.mimeparse import *
+
+def test_parse_mime_type():
+ parse = parse_mime_type
+ assert parse('*/*') == ('*', '*', {})
+ assert parse('text/html') == ('text', 'html', {})
+ assert parse('audio/*; q=0.2') == ('audio', '*', {'q': '0.2'})
+ assert parse('text/x-dvi;level=1') == ('text', 'x-dvi', {'level': '1'})
+ assert parse('image/gif; level=2; q=0.4') == (
+ '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('*') == ('*', '*', {})
+ assert parse('text') == ('text', '*', {})
+ assert parse('text/') == ('text', '*', {})
+ assert parse('/plain') == ('*', 'plain', {})
+ assert parse('/') == ('*', '*', {})
+ assert parse('text/plain;') == ('text', 'plain', {})
+ assert parse(';q=0.5') == ('*', '*', {'q': '0.5'})
+ assert parse('*; q=.2') == ('*', '*', {'q': '.2'})
+ assert parse('image; q=.7; level=3') == (
+ 'image', '*', {'q': '.7', 'level': '3'})
+ assert parse('*;q=1') == ('*', '*', {'q': '1'})
+ assert parse('*;q=') == ('*', '*', {})
+ assert parse('*;=0.5') == ('*', '*', {})
+ 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
+ assert parse('application/*;q=0.5') == ('application', '*', {'q': '0.5'})
+ assert parse('text/plain') == ('text', 'plain', {'q': '1'})
+ assert parse('*') == ('*', '*', {'q': '1'})
+ assert parse(';q=0.5') == ('*', '*', {'q': '0.5'})
+ assert parse('*;q=0.5') == ('*', '*', {'q': '0.5'})
+ assert parse('*;q=1') == ('*', '*', {'q': '1'})
+ assert parse('*;q=') == ('*', '*', {'q': '1'})
+ assert parse('*;q=-1') == ('*', '*', {'q': '1'})
+ assert parse('*;q=foobar') == ('*', '*', {'q': '1'})
+ assert parse('*;q=0.0001') == ('*', '*', {'q': '0.0001'})
+ assert parse('*;q=1000.0') == ('*', '*', {'q': '1'})
+ assert parse('*;q=0') == ('*', '*', {'q': '0'})
+ assert parse('*;q=0.0000') == ('*', '*', {'q': '0.0000'})
+ assert parse('*;q=1.0001') == ('*', '*', {'q': '1'})
+ assert parse('*;q=2') == ('*', '*', {'q': '1'})
+ assert parse('*;q=1e3') == ('*', '*', {'q': '1'})
+ assert parse('image/gif; level=2') == (
+ 'image', 'gif', {'level': '2', 'q': '1'})
+ assert parse('image/gif; level=2; q=0.5') == (
+ '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
+ assert faq('*/*;q=0.7', [
+ ('foo', 'bar', {'q': '0.5'})]) == (0, 0.5)
+ assert faq('foo/*;q=0.7', [
+ ('foo', 'bar', {'q': '0.5'})]) == (100, 0.5)
+ assert faq('*/bar;q=0.7', [
+ ('foo', 'bar', {'q': '0.5'})]) == (10, 0.5)
+ assert faq('foo/bar;q=0.7', [
+ ('foo', 'bar', {'q': '0.5'})]) == (110, 0.5)
+ assert faq('text/html;q=0.7', [
+ ('foo', 'bar', {'q': '0.5'})]) == (-1, 0)
+ assert faq('text/html;q=0.7', [
+ ('text', 'bar', {'q': '0.5'})]) == (-1, 0)
+ assert faq('text/html;q=0.7', [
+ ('foo', 'html', {'q': '0.5'})]) == (-1, 0)
+ assert faq('text/html;q=0.7', [
+ ('text', '*', {'q': '0.5'})]) == (100, 0.5)
+ assert faq('text/html;q=0.7', [
+ ('*', 'html', {'q': '0.5'})]) == (10, 0.5)
+ assert faq('text/html;q=0.7', [
+ ('*', '*', {'q': '0'}), ('text', 'html', {'q': '0.5'})]) == (110, 0.5)
+ assert faq('text/html;q=0.7', [
+ ('*', '*', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (0, 0.5)
+ assert faq('audio/mp3;q=0.7', [
+ ('*', '*', {'q': '0'}), ('audio', '*', {'q': '0.5'})]) == (100, 0.5)
+ assert faq('*/mp3;q=0.7', [
+ ('foo', 'mp3', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (10, 0.5)
+ assert faq('audio/mp3;q=0.7', [
+ ('audio', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5)
+ assert faq('audio/mp3;q=0.7', [
+ ('*', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5)
+ assert faq('text/html;q=0.7', [
+ ('text', 'plain', {'q': '0'}),
+ ('plain', 'html', {'q': '0'}),
+ ('text', 'html', {'q': '0.5'}),
+ ('html', 'text', {'q': '0'})]) == (110, 0.5)
+ assert faq('text/html;q=0.7;level=2', [
+ ('plain', 'html', {'q': '0', 'level': '2'}),
+ ('text', '*', {'q': '0.5', 'level': '3'}),
+ ('*', 'html', {'q': '0.5', 'level': '2'}),
+ ('image', 'gif', {'q': '0.5', 'level': '2'})]) == (100, 0.5)
+ assert faq('text/html;q=0.7;level=2', [
+ ('text', 'plain', {'q': '0'}), ('text', 'html', {'q': '0'}),
+ ('text', 'plain', {'q': '0', 'level': '2'}),
+ ('text', 'html', {'q': '0.5', 'level': '2'}),
+ ('*', '*', {'q': '0', 'level': '2'}),
+ ('text', 'html', {'q': '0', 'level': '3'})]) == (111, 0.5)
+ assert faq('text/html;q=0.7;level=2;opt=3', [
+ ('text', 'html', {'q': '0'}),
+ ('text', 'html', {'q': '0', 'level': '2'}),
+ ('text', 'html', {'q': '0', 'opt': '3'}),
+ ('*', '*', {'q': '0', 'level': '2', 'opt': '3'}),
+ ('text', 'html', {'q': '0', 'level': '3', 'opt': '3'}),
+ ('text', 'html', {'q': '0.5', 'level': '2', 'opt': '3'}),
+ ('*', '*', {'q': '0', 'level': '3', 'opt': '3'})]) == (112, 0.5)
+
+def test_quality_parsed():
+ qp = quality_parsed
+ assert qp('image/gif;q=0.7', [('image', 'jpg', {'q': '0.5'})]) == 0
+ assert qp('image/gif;q=0.7', [('image', '*', {'q': '0.5'})]) == 0.5
+ assert qp('audio/mp3;q=0.7;quality=100', [
+ ('*', '*', {'q': '0', 'quality': '100'}),
+ ('audio', '*', {'q': '0', 'quality': '100'}),
+ ('*', 'mp3', {'q': '0', 'quality': '100'}),
+ ('audio', 'mp3', {'q': '0', 'quality': '50'}),
+ ('audio', 'mp3', {'q': '0.5', 'quality': '100'}),
+ ('audio', 'mp3', {'q': '0.5'})]) == 0.5
+
+def test_quality():
+ assert quality('text/html',
+ '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.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
+ assert bm([], '*/*') == ''
+ assert bm(['application/xbel+xml', 'text/xml'],
+ 'text/*;q=0.5,*/*; q=0.1') == 'text/xml'
+ assert bm(['application/xbel+xml', 'audio/mp3'],
+ 'text/*;q=0.5,*/*; q=0.1') == 'application/xbel+xml'
+ assert bm(['application/xbel+xml', 'audio/mp3'],
+ 'text/*;q=0.5,*/mp3; q=0.1') == 'audio/mp3'
+ assert bm(['application/xbel+xml', 'text/plain', 'text/html'],
+ 'text/*;q=0.5,*/plain; q=0.1') == 'text/plain'
+ assert bm(['application/xbel+xml', 'text/html', 'text/xhtml'],
+ '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
+ assert bm(['image/png', 'image/jpeg', 'image/gif', 'text/html'],
+ 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/jpeg'
+ assert bm(['image/png', 'image/jpg', 'image/tif', 'text/html'],
+ 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'text/html'
+ assert bm(['image/png', 'image/jpg', 'image/tif', 'audio/mp3'],
+ 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/png'
+
+def test_sorted_match():
+ dm = desired_matches
+ assert dm(['text/html', 'application/xml'],
+ 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,'
+ 'text/plain;q=0.8,image/png') == ['text/html', 'application/xml']
+ assert dm(['text/html', 'application/xml'],
+ 'application/xml,application/json') == ['application/xml']
+ assert dm(['text/xhtml', 'text/plain', 'application/xhtml'],
+ 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,'
+ 'text/plain;q=0.8,image/png') == ['text/plain']