summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephan Richter <stephan.richter@gmail.com>2013-02-11 19:20:58 +0000
committerStephan Richter <stephan.richter@gmail.com>2013-02-11 19:20:58 +0000
commitac1776c089fd80a6bef7c3808ff5f2d45a2c91ac (patch)
tree99e1aed0f981f7a9ffc3ebe6c8268e30958752da /src
parentb79e6f2375d26c73bd59c63992c193009fea66e8 (diff)
downloadzope-contenttype-ac1776c089fd80a6bef7c3808ff5f2d45a2c91ac.tar.gz
- Added some tests for better coverage.
- Added `tox.ini` and manifest. - Added support for Python 3.3 and PyPy 1.9.
Diffstat (limited to 'src')
-rw-r--r--src/zope/contenttype/__init__.py21
-rw-r--r--src/zope/contenttype/parse.py5
-rw-r--r--src/zope/contenttype/tests/testContentTypes.py20
3 files changed, 37 insertions, 9 deletions
diff --git a/src/zope/contenttype/__init__.py b/src/zope/contenttype/__init__.py
index c0a26ec..9a6c76b 100644
--- a/src/zope/contenttype/__init__.py
+++ b/src/zope/contenttype/__init__.py
@@ -16,10 +16,9 @@ import re
import os.path
import mimetypes
-
find_binary = re.compile('[\0-\7]').search
-
+
def text_type(s):
"""Given an unnamed piece of text, try to guess its content type.
@@ -42,12 +41,12 @@ def text_type(s):
return 'text/xml'
# we also recognize small snippets of HTML - the closing tag might be
- # anywhere, even at the end of
+ # anywhere, even at the end of
if '</' in s:
return 'text/html'
return 'text/plain'
-
+
def guess_content_type(name='', body='', default=None):
"""Given a named piece of content, try to guess its content type.
@@ -105,7 +104,15 @@ def add_files(filenames):
here = os.path.dirname(os.path.abspath(__file__))
add_files([os.path.join(here, "mime.types")])
-if __name__ == '__main__':
+# Python 2/3 compatibility for testing.
+def _print(s):
+ print(s)
+
+def main():
items = mimetypes.types_map.items()
- items.sort()
- for item in items: print "%s:\t%s" % item
+ items = sorted(items)
+ for item in items:
+ _print("%s:\t%s" % item)
+
+if __name__ == '__main__': #pragma: nocover
+ main()
diff --git a/src/zope/contenttype/parse.py b/src/zope/contenttype/parse.py
index 4282c14..5978da3 100644
--- a/src/zope/contenttype/parse.py
+++ b/src/zope/contenttype/parse.py
@@ -112,7 +112,8 @@ def _unescape(string):
return string
-def join((major, minor, params)):
+def join(spec):
+ (major, minor, params) = spec
pstr = ""
try:
params.items
@@ -121,7 +122,7 @@ def join((major, minor, params)):
else:
params = params.items()
# ensure a predictable order:
- params.sort()
+ params = sorted(params)
for name, value in params:
pstr += ";%s=%s" % (name, _escape(value))
return "%s/%s%s" % (major, minor, pstr)
diff --git a/src/zope/contenttype/tests/testContentTypes.py b/src/zope/contenttype/tests/testContentTypes.py
index a09f278..ee51fd7 100644
--- a/src/zope/contenttype/tests/testContentTypes.py
+++ b/src/zope/contenttype/tests/testContentTypes.py
@@ -37,6 +37,26 @@ class ContentTypesTestCase(unittest.TestCase):
here = os.path.dirname(os.path.abspath(__file__))
return os.path.join(here, name)
+ def test_main(self):
+ import zope.contenttype
+ _print = zope.contenttype._print
+ zope.contenttype._print = lambda s: None
+ zope.contenttype.main()
+ zope.contenttype._print = _print
+
+ def test_guess_content_type(self):
+ from zope.contenttype import add_files
+ from zope.contenttype import guess_content_type
+ filename = self._getFilename('mime.types-1')
+ add_files([filename])
+ ctype, encoding = guess_content_type(body='text file')
+ self.assertEqual(ctype, "text/plain")
+ ctype, encoding = guess_content_type(body='\001binary')
+ self.assertEqual(ctype, "application/octet-stream")
+ ctype, encoding = guess_content_type()
+ self.assertEqual(ctype, "text/x-unknown-content-type")
+
+
def test_add_one_file(self):
from zope.contenttype import add_files
from zope.contenttype import guess_content_type