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