summaryrefslogtreecommitdiff
path: root/src/zope/pagetemplate/tests/test_ptfile.py
blob: e5cc2e92fb4b507204046f5f536afe222b20dc1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
##############################################################################
#
# Copyright (c) 2004 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Tests of PageTemplateFile.
"""
import os
import tempfile
import unittest

import six
from zope.pagetemplate.pagetemplatefile import PageTemplateFile

class AbstractPTCase(object):

    def get_pt(self, text=b'<html />'):
        with tempfile.NamedTemporaryFile(mode='wb', delete=False) as f:
            f.write(text)
        self.addCleanup(os.unlink, f.name)
        pt = PageTemplateFile(f.name)
        pt.read()
        return pt

class TypeSniffingTestCase(AbstractPTCase,
                           unittest.TestCase):

    def check_content_type(self, text, expected_type):
        pt = self.get_pt(text)
        self.assertEqual(pt.content_type, expected_type)

    def test_sniffer_xml_ascii(self):
        self.check_content_type(
            b"<?xml version='1.0' encoding='ascii'?><doc/>",
            "text/xml")
        self.check_content_type(
            b"<?xml\tversion='1.0' encoding='ascii'?><doc/>",
            "text/xml")

    def test_sniffer_xml_utf8(self):
        # w/out byte order mark
        self.check_content_type(
            b"<?xml version='1.0' encoding='utf-8'?><doc/>",
            "text/xml")
        self.check_content_type(
            b"<?xml\tversion='1.0' encoding='utf-8'?><doc/>",
            "text/xml")
        # with byte order mark
        self.check_content_type(
            b"\xef\xbb\xbf<?xml version='1.0' encoding='utf-8'?><doc/>",
            "text/xml")
        self.check_content_type(
            b"\xef\xbb\xbf<?xml\tversion='1.0' encoding='utf-8'?><doc/>",
            "text/xml")

    def test_sniffer_xml_utf16_be(self):
        # w/out byte order mark
        self.check_content_type(
            b"\0<\0?\0x\0m\0l\0 \0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'"
            b"\0 \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>"
            b"\0<\0d\0o\0c\0/\0>",
            "text/xml")
        self.check_content_type(
            b"\0<\0?\0x\0m\0l\0\t\0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'"
            b"\0 \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>"
            b"\0<\0d\0o\0c\0/\0>",
            "text/xml")
        # with byte order mark
        self.check_content_type(
            b"\xfe\xff"
            b"\0<\0?\0x\0m\0l\0 \0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'"
            b"\0 \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>"
            b"\0<\0d\0o\0c\0/\0>",
            "text/xml")
        self.check_content_type(
            b"\xfe\xff"
            b"\0<\0?\0x\0m\0l\0\t\0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'"
            b"\0 \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>"
            b"\0<\0d\0o\0c\0/\0>",
            "text/xml")

    def test_sniffer_xml_utf16_le(self):
        # w/out byte order mark
        self.check_content_type(
            b"<\0?\0x\0m\0l\0 \0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'\0"
            b" \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>\0"
            b"<\0d\0o\0c\0/\0>\n",
            "text/xml")
        self.check_content_type(
            b"<\0?\0x\0m\0l\0\t\0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'\0"
            b" \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>\0"
            b"<\0d\0o\0c\0/\0>\0",
            "text/xml")
        # with byte order mark
        self.check_content_type(
            b"\xff\xfe"
            b"<\0?\0x\0m\0l\0 \0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'\0"
            b" \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>\0"
            b"<\0d\0o\0c\0/\0>\0",
            "text/xml")
        self.check_content_type(
            b"\xff\xfe"
            b"<\0?\0x\0m\0l\0\t\0v\0e\0r\0s\0i\0o\0n\0=\0'\01\0.\0000\0'\0"
            b" \0e\0n\0c\0o\0d\0i\0n\0g\0=\0'\0u\0t\0f\0-\08\0'\0?\0>\0"
            b"<\0d\0o\0c\0/\0>\0",
            "text/xml")

    HTML_PUBLIC_ID = "-//W3C//DTD HTML 4.01 Transitional//EN"
    HTML_SYSTEM_ID = "http://www.w3.org/TR/html4/loose.dtd"

    def test_sniffer_html_ascii(self):
        self.check_content_type(
            ("<!DOCTYPE html [ SYSTEM '%s' ]><html></html>"
             % self.HTML_SYSTEM_ID).encode("utf-8"),
            "text/html")
        self.check_content_type(
            b"<html><head><title>sample document</title></head></html>",
            "text/html")

    @unittest.expectedFailure
    def test_sniffer_xml_simple(self):
        # TODO: This reflects a case that simply isn't handled by the
        # sniffer; there are many, but it gets it right more often than
        # before. This case actually returns text/html
        self.check_content_type(b"<doc><element/></doc>",
                                "text/xml")

    def test_html_default_encoding(self):
        pt = self.get_pt(
            b"<html><head><title>"
            # 'Test' in russian (utf-8)
            b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82"
            b"</title></head></html>")
        rendered = pt()
        self.assertTrue(isinstance(rendered, six.text_type))
        self.assertEqual(rendered.strip(),
                         (u"<html><head><title>"
                          u"\u0422\u0435\u0441\u0442"
                          u"</title></head></html>"))

    def test_html_encoding_by_meta(self):
        pt = self.get_pt(
            b"<html><head><title>"
            # 'Test' in russian (windows-1251)
            b"\xd2\xe5\xf1\xf2"
            b'</title><meta http-equiv="Content-Type"'
            b' content="text/html; charset=windows-1251">'
            b"</head></html>")
        rendered = pt()
        self.assertTrue(isinstance(rendered, six.text_type))
        self.assertEqual(rendered.strip(),
                         (u"<html><head><title>"
                          u"\u0422\u0435\u0441\u0442"
                          u"</title></head></html>"))

    def test_xhtml(self):
        pt = self.get_pt(
            b"<html><head><title>"
            # 'Test' in russian (windows-1251)
            b"\xd2\xe5\xf1\xf2"
            b'</title><meta http-equiv="Content-Type"'
            b' content="text/html; charset=windows-1251"/>'
            b"</head></html>")
        rendered = pt()
        self.assertTrue(isinstance(rendered, six.text_type))
        self.assertEqual(rendered.strip(),
                         (u"<html><head><title>"
                          u"\u0422\u0435\u0441\u0442"
                          u"</title></head></html>"))


class TestPageTemplateFile(AbstractPTCase,
                           unittest.TestCase):

    def test_no_such_file(self):
        with self.assertRaises(ValueError):
            PageTemplateFile('this file does not exist')

    def test_prefix_str(self):
        pt = PageTemplateFile(os.path.basename(__file__),
                              _prefix=os.path.dirname(__file__))
        self.assertEqual(pt.filename, __file__)


    def test_cook_no_debug(self):
        pt = self.get_pt()
        pt._v_debug = False
        pt._cook_check()
        self.assertTrue(pt._v_last_read)
        lr = pt._v_last_read
        pt._cook_check()
        self.assertEqual(lr, pt._v_last_read)


    def test_cook_mtime_fails(self):
        pt = self.get_pt()

        getmtime = os.path.getmtime
        def bad(_path):
            raise OSError()
        os.path.getmtime = bad
        try:
            pt._cook_check()
        finally:
            os.path.getmtime = getmtime

        self.assertEqual(0, pt._v_last_read)

    def test_pickle_not_allowed(self):
        import pickle
        pt = self.get_pt()

        with self.assertRaises(TypeError):
            pickle.dumps(pt)