1
2
3 """
4 Web IO test cases that need Python 2.5+ (wsgiref)
5 """
6
7 from __future__ import with_statement
8
9 import unittest
10 import textwrap
11 import os
12 import sys
13 import gzip
14
15 this_dir = os.path.dirname(__file__)
16 if this_dir not in sys.path:
17 sys.path.insert(0, this_dir)
18
19 from .common_imports import (
20 etree, HelperTestCase, BytesIO, _bytes)
21 from .dummy_http_server import webserver, HTTPRequestCollector
22
23
25 etree = etree
26
33
38
40 try:
41 self._parse_from_http(_bytes('<root/>'), code=404)
42 except IOError:
43 self.assertTrue(True)
44 else:
45 self.assertTrue(False, "expected IOError")
46
48 f = BytesIO()
49 gz = gzip.GzipFile(fileobj=f, mode='w', filename='test.xml')
50 gz.write(_bytes('<root><a/></root>'))
51 gz.close()
52 data = f.getvalue()
53 del f, gz
54
55 headers = [('Content-Encoding', 'gzip')]
56 tree = self._parse_from_http(data, headers=headers)
57 self.assertEqual('root', tree.getroot().tag)
58 self.assertEqual('a', tree.getroot()[0].tag)
59
81
83 data = [_bytes(textwrap.dedent(s)) for s in [
84
85 '''\
86 <?xml version="1.0"?>
87 <!DOCTYPE root SYSTEM "./file.dtd">
88 <root>&myentity;</root>
89 ''',
90
91 '<!ENTITY myentity "DEFINED">',
92 ]]
93
94 responses = []
95 def handler(environ, start_response):
96 start_response('200 OK', [])
97 return [responses.pop()]
98
99 with webserver(handler) as host_url:
100
101 responses = data[::-1]
102 tree = self.etree.parse(
103 host_url + 'dir/test.xml',
104 parser=self.etree.XMLParser(
105 load_dtd=True, no_network=False))
106 self.assertFalse(responses)
107 root = tree.getroot()
108 self.assertEqual('DEFINED', root.text)
109
110
111 responses = data[::-1]
112 try:
113 self.etree.parse(
114 host_url + 'dir/test.xml',
115 parser=self.etree.XMLParser(
116 load_dtd=True, no_network=True))
117 except self.etree.XMLSyntaxError:
118 self.assertTrue("myentity" in str(sys.exc_info()[1]))
119 else:
120 self.assertTrue(False)
121 self.assertEqual(1, len(responses))
122
123
125 suite = unittest.TestSuite()
126 suite.addTests([unittest.makeSuite(HttpIOTestCase)])
127 return suite
128
129
130 if __name__ == '__main__':
131 print('to test use test.py %s' % __file__)
132