Commits

Jesper Nøhr  committed 5e70069

First version w/ simple tests and a stupid middleware that does SSI

  • Participants

Comments (0)

Files changed (12)

File fakesi/__init__.py

Empty file added.

File fakesi/middleware.py

+import re
+
+from django.core.urlresolvers import get_urlconf, get_resolver, Resolver404
+from django.core.urlresolvers import resolve
+
+INCLUDE_TAG = re.compile(r'<!--#[\s.]+include[\s.]+virtual=["\'](?P<path>.+)["\'][\s.]+-->')
+
+class FakesiMiddleware(object):
+    def process_response(self, request, response):
+        def ssi_include(match):
+            func, args, kwargs = resolve(match.group('path'))
+
+            # TODO: Insert fancy response when path doesn't resolve.
+
+            return func(request).content
+
+        response.content = re.sub(INCLUDE_TAG, ssi_include, response.content)
+        response['Content-Length'] = len(response.content)
+
+        return response

File fakesi/models.py

Empty file added.

File fakesi/tests/__init__.py

Empty file added.

File fakesi/tests/models.py

Empty file added.

File fakesi/tests/runtests.py

+#!/usr/bin/env python
+import os
+import sys
+
+from django.conf import settings
+
+if not settings.configured:
+    settings.configure(
+        DATABASE_ENGINE='sqlite3',
+        INSTALLED_APPS=[
+            'fakesi',
+            'fakesi.tests',
+        ],
+        TEMPLATE_DIRS=[
+            'fakesi/tests/',
+        ],
+        ROOT_URLCONF='fakesi.tests.urls',
+        MIDDLEWARE_CLASSES=[
+            'fakesi.middleware.FakesiMiddleware',
+        ]
+    )
+
+from django.test.simple import run_tests
+
+
+def runtests(*test_args):
+    if not test_args:
+        test_args = ['tests']
+    parent = os.path.join(
+        os.path.dirname(os.path.abspath(__file__)),
+        "..",
+        "..",
+    )
+    sys.path.insert(0, parent)
+    failures = run_tests(test_args, verbosity=1, interactive=True)
+    sys.exit(failures)
+
+
+if __name__ == '__main__':
+    runtests(*sys.argv[1:])

File fakesi/tests/templates/root.html

+<!doctype html>
+<html lang="en">
+<head>
+  <meta charset="utf-8">
+  <title>Basic HTML5 template</title>
+</head>
+<body>
+	<h1>This is the body of the template</h1>
+	<p>I'm about to do an SSI include!</p>
+	<!--# include virtual="/ssi?with=getvars" -->
+</body>
+</html>

File fakesi/tests/tests.py

+from django.test import TestCase
+
+from django.test.client import Client
+
+class LolTest(TestCase):
+	def test_me(self):
+		c = Client()
+
+		print c.get('/')

File fakesi/tests/urls.py

+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('fakesi.tests.views',
+	(r'^$', 'root'),
+	(r'^ssi', 'ssi'),
+)

File fakesi/tests/views.py

+from django import template, templatetags
+from django.template import RequestContext
+from django.conf import settings
+
+from django.shortcuts import render_to_response
+from django.http import HttpResponse
+
+def root(request):
+    return render_to_response('templates/root.html', 
+        {}, RequestContext(request))
+
+def ssi(request):
+    print "MOTHERFUCKING SSI CALLED"
+    return HttpResponse("hey "+repr(request))
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from setuptools import setup, find_packages
+
+setup(
+    name='django-fakesi',
+    version='1.0.0-alpha',
+    description='Fake ESI for Django.',
+    author='Jesper Noehr',
+    author_email='jesper@noehr.org',
+#    long_description=open('README.rst', 'r').read(),
+    url='https://bitbucket.org/jespern/django-fakesi',
+    packages=find_packages(),
+    classifiers=[
+        'Development Status :: 4 - Beta',
+        'Environment :: Web Environment',
+        'Framework :: Django',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved :: BSD License',
+        'Operating System :: OS Independent',
+        'Programming Language :: Python',
+        'Topic :: Utilities'
+    ],
+    test_suite='fakesi.tests.runtests.runtests',
+)
+[tox]
+envlist = py27
+downloadcache = .tox/_download/
+
+[testenv]
+commands =
+    python setup.py test
+deps =
+    django==1.3