Exemple #1
0
    def test_204(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application1(environ, start_response):
            status = '204 NO CONTENT'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(application1, {}, tree=etree.fromstring(XSLT))

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertEqual(response.body, HTML)

        def application2(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(application2, {}, tree=etree.fromstring(XSLT))

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertTrue(
            b'<div id="content">Content content</div>' in response.body, )
        self.assertTrue(b'<title>Transformed</title>' in response.body)
Exemple #2
0
    def test_params(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT_PARAM),
        )
        request = Request.blank('/')
        response = request.get_response(app)

        self.assertTrue(b'<p>defaultvalue</p>' in response.body)

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT_PARAM),
            someparam='value1',
        )
        request = Request.blank('/')
        response = request.get_response(app)

        self.assertTrue(b'<p>value1</p>' in response.body)
Exemple #3
0
    def test_html_serialization(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT_HTML))
        request = Request.blank('/')
        response = request.get_response(app)

        # HTML serialisation
        self.assertTrue(
            b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" '
            b'"http://www.w3.org/TR/html4/strict.dtd">' in response.body, )
        self.assertTrue(b'<br>' in response.body)

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT_XHTML),
        )
        request = Request.blank('/')
        response = request.get_response(app)

        # XHTML serialisation
        self.assertTrue(
            b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
            b'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
            in response.body, )
        self.assertTrue(b'<br />' in response.body)

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT_HTML5),
        )
        request = Request.blank('/')
        response = request.get_response(app)

        # HTML 5 serialisation
        self.assertTrue(b'<!DOCTYPE html>' in response.body)
        self.assertTrue(b'<br/>' in response.body)
Exemple #4
0
    def test_doctype_xhtml(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT_XHTML),
        )

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertEqual(
            response.headers['Content-Type'],
            'application/xhtml+xml; charset=UTF-8',
        )
Exemple #5
0
    def test_content_empty(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [
                ('Content-Type', 'text/html'),
                ('Content-MD5', 'd41d8cd98f00b204e9800998ecf8427e'),
            ]
            start_response(status, response_headers)
            return [b'']

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT),
            update_content_length=True,
        )

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertEqual(
            response.headers['Content-MD5'],
            'd41d8cd98f00b204e9800998ecf8427e',
        )
Exemple #6
0
    def test_content_range(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            content_length = len(HTML)
            content_range = 'bytes {start:d}-{end:d}/{length:d}'.format(
                start=0,
                end=content_length - 1,
                length=content_length,
            )
            response_headers = [
                ('Content-Type', 'text/html'),
                ('Content-Range', content_range),
                ('Content-Length', str(content_length)),
            ]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT),
        )

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertFalse('Content-Range' in response.headers)
Exemple #7
0
    def test_dont_update_content_length(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [
                ('Content-Type', 'text/html'),
                ('Content-Length', '1'),
            ]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT),
        )

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertEqual(response.headers.get('Content-Length'), None)
Exemple #8
0
    def test_head_request(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [
                ('Content-Type', 'text/html'),
                ('Content-Length', str(len(HTML))),
            ]
            start_response(status, response_headers)
            return ['']  # Empty response for HEAD request

        app = XSLTMiddleware(
            application,
            {},
            tree=etree.fromstring(XSLT),
        )

        env = dict(REQUEST_METHOD='HEAD')
        request = Request.blank('/', environ=env)
        # The *real* test is whether or not an exception is raised here.
        response = request.get_response(app)

        # Response headers for HEAD request must be updated.
        self.assertEqual(response.headers.get('Content-Length'), None)
        self.assertFalse(response.body)
Exemple #9
0
    def test_transform_filename(self):
        import tempfile
        import os

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        _, filename = tempfile.mkstemp()
        with open(filename, 'wb') as fp:
            fp.write(XSLT)

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(
            application,
            {},
            filename=filename,
        )
        os.unlink(filename)

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertEqual(
            response.headers['Content-Type'],
            'text/html; charset=UTF-8',
        )
        self.assertTrue(
            b'<div id="content">Content content</div>' in response.body, )
        self.assertTrue(b'<title>Transformed</title>' in response.body)
Exemple #10
0
    def test_doctype_html5(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(application, {},
                             tree=etree.fromstring(XSLT_XHTML),
                             doctype="<!DOCTYPE html>")

        request = Request.blank('/')
        response = request.get_response(app)

        self.assertTrue(response.body.startswith("<!DOCTYPE html>\n<html"))
Exemple #11
0
    def test_head_request(self):
        from lxml import etree

        from diazo.wsgi import XSLTMiddleware
        from webob import Request

        def application(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-Type', 'text/html')]
            start_response(status, response_headers)
            return [HTML]

        app = XSLTMiddleware(application, {}, tree=etree.fromstring(XSLT))

        env = dict(REQUEST_METHOD='HEAD')
        request = Request.blank('/', environ=env)
        # The *real* test is whether or not an exception is raised here.
        response = request.get_response(app)

        self.assertEqual(response.headers['Content-Type'],
                         'text/html; charset=UTF-8')
        self.assertFalse(response.body)