Package lxml :: Package tests :: Module test_http_io
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_http_io

  1  # -*- coding: utf-8 -*- 
  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   
18 -class HttpIOTestCase(HelperTestCase):
19 etree = etree 20
21 - def _parse_from_http(self, data, code=200, headers=None, parser=None):
22 handler = HTTPRequestCollector(data, code, headers) 23 with webserver(handler) as host_url: 24 tree = self.etree.parse(host_url + 'TEST', parser=parser) 25 self.assertEqual([('/TEST', [])], handler.requests) 26 return tree
27
28 - def test_http_client(self):
29 tree = self._parse_from_http(_bytes('<root><a/></root>')) 30 self.assertEqual('root', tree.getroot().tag) 31 self.assertEqual('a', tree.getroot()[0].tag)
32
33 - def test_http_client_404(self):
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
41 - def test_http_client_gzip(self):
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
54 - def test_parser_input_mix(self):
55 data = _bytes('<root><a/></root>') 56 handler = HTTPRequestCollector(data) 57 58 with webserver(handler) as host_url: 59 tree = self.etree.parse(host_url) 60 root = tree.getroot() 61 self.assertEqual('a', root[0].tag) 62 63 root = self.etree.fromstring(data) 64 self.assertEqual('a', root[0].tag) 65 66 tree = self.etree.parse(host_url) 67 root = tree.getroot() 68 self.assertEqual('a', root[0].tag) 69 70 root = self.etree.fromstring(data) 71 self.assertEqual('a', root[0].tag) 72 73 root = self.etree.fromstring(data) 74 self.assertEqual('a', root[0].tag)
75
76 - def test_network_dtd(self):
77 data = [_bytes(textwrap.dedent(s)) for s in [ 78 # XML file 79 '''\ 80 <?xml version="1.0"?> 81 <!DOCTYPE root SYSTEM "./file.dtd"> 82 <root>&myentity;</root> 83 ''', 84 # DTD 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 # DTD network loading enabled 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) # all read 101 root = tree.getroot() 102 self.assertEqual('DEFINED', root.text) 103 104 # DTD network loading disabled 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)) # DTD not read
116 117
118 -def test_suite():
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