summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAde Oshineye <adewale@gmail.com>2010-02-14 17:41:09 -0800
committerDavid Tsai <dbtsai@dbtsai.com>2012-08-22 14:32:30 -0700
commit032c52c6172856af88bbd5b7c59b3c1fb744421e (patch)
tree8fea3c2666a97a309759bdd6877cfa607cb339a0
parentc3320d5a262c7698d00e6fcf3b89ab384287d626 (diff)
downloadpython-mimeparse-032c52c6172856af88bbd5b7c59b3c1fb744421e.tar.gz
Added the first set of language independent tests and a Python implementation.
The current tests are just a port of the existing Python tests but I plan to add more.
-rw-r--r--README13
-rw-r--r--mimeparse_test.py60
-rw-r--r--testdata.json34
3 files changed, 107 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..82e593b
--- /dev/null
+++ b/README
@@ -0,0 +1,13 @@
+This module provides basic functions for parsing mime-type names and matching them against a list of media-ranges.
+
+See section 14.1 of RFC 2616 (the HTTP specification) for a complete explanation.
+
+Testing
+=======
+The Python tests require either Python 2.6 or the installation of the SimpleJSON library.
+
+Installing SimpleJson can be done by:
+sudo easy_install simplejson
+
+Run the tests by typing:
+python mimeparse_test.py
diff --git a/mimeparse_test.py b/mimeparse_test.py
new file mode 100644
index 0000000..8de2d8c
--- /dev/null
+++ b/mimeparse_test.py
@@ -0,0 +1,60 @@
+"""
+Python tests for Mime-Type Parser.
+
+This module loads a json file and converts the tests specified therein to a set
+of PyUnitTestCases. Then it uses PyUnit to run them and report their status.
+"""
+__version__ = "0.1"
+__author__ = 'Ade Oshineye'
+__email__ = "ade@oshineye.com"
+__credits__ = ""
+
+import mimeparse
+import unittest
+from functools import partial
+# Conditional import to support Python 2.5
+try:
+ import json
+except ImportError:
+ import simplejson as json
+
+def test_parse_media_range(args, expected):
+ expected = tuple(expected)
+ result = mimeparse.parse_media_range(args)
+ message = "Expected: '%s' but got %s" % (expected, result)
+ assert expected == result, message
+
+def test_quality(args, expected):
+ result = mimeparse.quality(args[0], args[1])
+ message = "Expected: '%s' but got %s" % (expected, result)
+ assert expected == result, message
+
+def test_best_match(args, expected):
+ result = mimeparse.best_match(args[0], args[1])
+ message = "Expected: '%s' but got %s" % (expected, result)
+ assert expected == result, message
+
+def add_tests(suite, json_object, func_name, test_func):
+ test_data = json_object[func_name]
+ for test_datum in test_data:
+ args, expected = test_datum[0], test_datum[1]
+ desc = "%s(%s) with expected result: %s" % (func_name, str(args), str(expected))
+ if len(test_datum) == 3:
+ desc = test_datum[2] + " : " + desc
+ func = partial(test_func, *(args, expected))
+ testcase = unittest.FunctionTestCase(func, description=desc)
+ suite.addTest(testcase)
+
+def run_tests():
+ json_object = json.load(open("testdata.json"))
+
+ suite = unittest.TestSuite()
+ add_tests(suite, json_object, "parse_media_range", test_parse_media_range)
+ add_tests(suite, json_object, "quality", test_quality)
+ add_tests(suite, json_object, "best_match", test_best_match)
+
+ test_runner = unittest.TextTestRunner(verbosity=1)
+ test_runner.run(suite)
+
+if __name__ == "__main__":
+ run_tests()
diff --git a/testdata.json b/testdata.json
new file mode 100644
index 0000000..aea0c55
--- /dev/null
+++ b/testdata.json
@@ -0,0 +1,34 @@
+{
+"parse_media_range": [
+ ["application/xml;q=1", ["application", "xml", {"q": "1"}]],
+ ["application/xml", ["application", "xml", {"q": "1"}]],
+ ["application/xml;q=",["application", "xml", {"q": "1"}]],
+ ["application/xml ;q=",["application", "xml", {"q": "1"}]],
+ ["application/xml ; q=1;b=other",["application", "xml", {"q": "1", "b":"other"}]],
+ ["application/xml ; q=2;b=other",["application", "xml", {"q": "1", "b":"other"}]],
+ [" *; q=.2",["*", "*", {"q": ".2"}]]
+],
+
+"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],
+ [["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/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],
+ [["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],
+ [["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],
+ [["text/html;level=3", "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]
+],
+
+"best_match": [
+ [[["application/xbel+xml", "application/xml"], "application/xbel+xml"], "application/xbel+xml", "direct match"],
+ [[["application/xbel+xml", "application/xml"], "application/xbel+xml; q=1"], "application/xbel+xml", "direct match with a q parameter"],
+ [[["application/xbel+xml", "application/xml"], "application/xml; q=1"], "application/xml", "direct match of our second choice with a q parameter"],
+ [[["application/xbel+xml", "application/xml"], "application/*; q=1"], "application/xml", "match using a subtype wildcard"],
+ [[["application/xbel+xml", "application/xml"], "*/*", "application/xml"], "application/xml", "match using a type wildcard"],
+ [[["application/xbel+xml", "text/xml"], "text/*;q=0.5,*/*; q=0.1"], "text/xml", "match using a type versus a lower weighted subtype"],
+ [[["application/xbel+xml", "text/xml"], "text/html,application/atom+xml; q=0.9"], "", "fail to match anything"],
+ [[["application/json", "text/html"], "application/json, text/javascript, */*"], "application/json", "common AJAX scenario"],
+ [[["application/json", "text/html"], "application/json, text/html;q=0.9"], "application/json", "verify fitness ordering"],
+ [[["image/*", "application/xml"], "image/png"], "image/*", "match using a type wildcard"],
+ [[["image/*", "application/xml"], "image/*"], "image/*", "match using a wildcard for both requested and supported"]
+]
+} \ No newline at end of file