Anonymous avatar Anonymous committed 3ccf20d

Inital implementation of mimehelper. Does not support */* accepts

Comments (0)

Files changed (3)

tests/test_mimetypes.py

+from webhelpers.mimehelper import MIMETypes
+from util import test_environ
+
+def setup():
+    MIMETypes.init()
+
+def test_register_alias():
+    MIMETypes.add_alias('html', 'text/html')
+    assert MIMETypes.aliases['html'] == 'text/html'
+    
+def test_usage():
+    environ = test_environ()
+    environ['PATH_INFO'] = '/test.html'
+    m = MIMETypes(environ)
+    assert m.mimetype('html') == 'text/html'
+
+def test_root_path():
+    environ = test_environ()
+    environ['PATH_INFO'] = '/'
+    environ['HTTP_ACCEPT'] = 'text/html, application/xml'
+    m = MIMETypes(environ)
+    assert m.mimetype('text/html') == 'text/html'
+
+def test_with_extention():
+    environ = test_environ()
+    environ['PATH_INFO'] = '/test.xml'
+    environ['HTTP_ACCEPT'] = 'text/html, application/xml'
+    m = MIMETypes(environ)
+    assert m.mimetype('text/html') == False
+    assert m.mimetype('application/xml') == 'application/xml'
+
+def test_with_unregistered_extention():
+    environ = test_environ()
+    environ['PATH_INFO'] = '/test.iscool'
+    environ['HTTP_ACCEPT'] = 'application/xml'
+    m = MIMETypes(environ)
+    assert m.mimetype('text/html') == False
+    assert m.mimetype('application/xml') == 'application/xml'
+
+def test_with_no_extention():
+    environ = test_environ()
+    environ['PATH_INFO'] = '/test'
+    environ['HTTP_ACCEPT'] = 'application/xml'
+    m = MIMETypes(environ)
+    assert m.mimetype('text/html') == False
+    assert m.mimetype('application/xml') == 'application/xml'
+    
+def test_with_no_extention_and_no_accept():
+    environ = test_environ()
+    environ['PATH_INFO'] = '/test'
+    m = MIMETypes(environ)
+    assert m.mimetype('html') == 'text/html'
 
 import routes
 
+def test_environ():
+    return {
+        'HTTP_HOST': 'bob.local:5000',
+        'PATH_INFO': '/test',
+        'QUERY_STRING': 'test=webhelpers&framework=pylons',
+        'REQUEST_METHOD': 'GET',
+        'SERVER_NAME': '0.0.0.0',
+        'SCRIPT_NAME': '',
+        'pylons.environ_config': dict(session='test.session'),
+        'test.session': {},
+        'wsgi.multiprocess': False,
+        'wsgi.multithread': True,
+        'wsgi.run_once': False,
+        'wsgi.url_scheme': 'http'
+        }
+
+
 class WebHelpersTestCase(TestCase):
     """Establishes a faux-environment for tests"""
     def test_environ(self):

webhelpers/mimehelper.py

+"""MIMEType helpers"""
+import mimetypes
+import webob
+
+class MIMETypes(object):
+    aliases = {}
+    
+    def init(cls):
+        mimetypes.init()
+    init = classmethod(init)
+    
+    def add_alias(cls, alias, mimetype):
+        cls.aliases[alias] = mimetype
+    add_alias = classmethod(add_alias)
+    
+    def __init__(self, environ):
+        self.env = environ
+    
+    def _set_responce_conetent_type(self, mimetype):
+        if 'pylons.pylons' in self.env:
+            self.env['pylons.pylons'].response.content_type = mimetype
+        return mimetype
+        
+    def mimetype(self, mimetype):
+        if mimetype in MIMETypes.aliases:
+            mimetype = MIMETypes.aliases[mimetype]
+        path = self.env['PATH_INFO']
+        guess_from_url = mimetypes.guess_type(path)[0]
+        possible_from_accept_header = None
+        has_extension = False
+        if len(path.split('/')) > 1:
+            last_part = path.split('/')[-1]
+            if '.' in last_part:
+                has_extension = True
+        if 'HTTP_ACCEPT' in self.env:
+            possible_from_accept_header = [type for type, q in webob.acceptparse.parse_accept(
+                self.env['HTTP_ACCEPT'])]
+        if has_extension == False:
+            if possible_from_accept_header is None:
+                return self._set_responce_conetent_type(mimetype)
+            elif mimetype in possible_from_accept_header:
+                return self._set_responce_conetent_type(mimetype)
+            else:
+                return False
+        if mimetype == guess_from_url:
+            # Guessed same mimetype
+            return self._set_responce_conetent_type(mimetype)
+        elif guess_from_url is None and mimetype in possible_from_accept_header:
+            return self._set_responce_conetent_type(mimetype)
+        else:
+            return False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.