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 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)  # needed for Py3
     18
     19  from .common_imports import (
     20      etree, HelperTestCase, BytesIO, _bytes)
     21  from .dummy_http_server import webserver, HTTPRequestCollector
     22
     23
     24 -class HttpIOTestCase(HelperTestCase):
     25      etree = etree
     26
     27 -    def _parse_from_http(self, data, code=200, headers=None, parser=None):
     28          handler = HTTPRequestCollector(data, code, headers)
     29          with webserver(handler) as host_url:
     30              tree = self.etree.parse(host_url + 'TEST', parser=parser)
     31          self.assertEqual([('/TEST', [])], handler.requests)
     32          return tree
     33
     34 -    def test_http_client(self):
     35          tree = self._parse_from_http(_bytes('<root><a/></root>'))
     36          self.assertEqual('root', tree.getroot().tag)
     37          self.assertEqual('a', tree.getroot()[0].tag)
     38
     39 -    def test_http_client_404(self):
     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
     47 -    def test_http_client_gzip(self):
     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
     60 -    def test_parser_input_mix(self):
     61          data = _bytes('<root><a/></root>')
     62          handler = HTTPRequestCollector(data)
     63
     64          with webserver(handler) as host_url:
     65              tree = self.etree.parse(host_url)
     66              root = tree.getroot()
     67              self.assertEqual('a', root[0].tag)
     68
     69              root = self.etree.fromstring(data)
     70              self.assertEqual('a', root[0].tag)
     71
     72              tree = self.etree.parse(host_url)
     73              root = tree.getroot()
     74              self.assertEqual('a', root[0].tag)
     75
     76              root = self.etree.fromstring(data)
     77              self.assertEqual('a', root[0].tag)
     78
     79          root = self.etree.fromstring(data)
     80          self.assertEqual('a', root[0].tag)
     81
     82 -    def test_network_dtd(self):
     83          data = [_bytes(textwrap.dedent(s)) for s in [
     84              # XML file
     85              '''\
     86              <?xml version="1.0"?>
     87              <!DOCTYPE root SYSTEM "./file.dtd">
     88              <root>&myentity;</root>
     89              ''',
     90              # DTD
     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              # DTD network loading enabled
    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)  # all read
    107              root = tree.getroot()
    108              self.assertEqual('DEFINED', root.text)
    109
    110              # DTD network loading disabled
    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))  # DTD not read
    122
    123
    124 -def test_suite():
    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