| 1 |
import io |
| 2 |
import unittest |
| 3 |
from http import client |
| 4 |
from test.test_httplib import FakeSocket |
| 5 |
from unittest import mock |
| 6 |
from xml.dom import getDOMImplementation, minidom, xmlbuilder |
| 7 |
|
| 8 |
SMALL_SAMPLE = b"""<?xml version="1.0"?> |
| 9 |
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xdc="http://www.xml.com/books"> |
| 10 |
<!-- A comment --> |
| 11 |
<title>Introduction to XSL</title> |
| 12 |
<hr/> |
| 13 |
<p><xdc:author xdc:attrib="prefixed attribute" attrib="other attrib">A. Namespace</xdc:author></p> |
| 14 |
</html>""" |
| 15 |
|
| 16 |
|
| 17 |
class XMLBuilderTest(unittest.TestCase): |
| 18 |
def test_entity_resolver(self): |
| 19 |
body = ( |
| 20 |
b"HTTP/1.1 200 OK\r\nContent-Type: text/xml; charset=utf-8\r\n\r\n" |
| 21 |
+ SMALL_SAMPLE |
| 22 |
) |
| 23 |
|
| 24 |
sock = FakeSocket(body) |
| 25 |
response = client.HTTPResponse(sock) |
| 26 |
response.begin() |
| 27 |
attrs = {"open.return_value": response} |
| 28 |
opener = mock.Mock(**attrs) |
| 29 |
|
| 30 |
resolver = xmlbuilder.DOMEntityResolver() |
| 31 |
|
| 32 |
with mock.patch("urllib.request.build_opener") as mock_build: |
| 33 |
mock_build.return_value = opener |
| 34 |
source = resolver.resolveEntity(None, "http://example.com/2000/svg") |
| 35 |
|
| 36 |
self.assertIsInstance(source, xmlbuilder.DOMInputSource) |
| 37 |
self.assertIsNone(source.publicId) |
| 38 |
self.assertEqual(source.systemId, "http://example.com/2000/svg") |
| 39 |
self.assertEqual(source.baseURI, "http://example.com/2000/") |
| 40 |
self.assertEqual(source.encoding, "utf-8") |
| 41 |
self.assertIs(source.byteStream, response) |
| 42 |
|
| 43 |
self.assertIsNone(source.characterStream) |
| 44 |
self.assertIsNone(source.stringData) |
| 45 |
|
| 46 |
def test_builder(self): |
| 47 |
imp = getDOMImplementation() |
| 48 |
self.assertIsInstance(imp, xmlbuilder.DOMImplementationLS) |
| 49 |
|
| 50 |
builder = imp.createDOMBuilder(imp.MODE_SYNCHRONOUS, None) |
| 51 |
self.assertIsInstance(builder, xmlbuilder.DOMBuilder) |
| 52 |
|
| 53 |
def test_parse_uri(self): |
| 54 |
body = ( |
| 55 |
b"HTTP/1.1 200 OK\r\nContent-Type: text/xml; charset=utf-8\r\n\r\n" |
| 56 |
+ SMALL_SAMPLE |
| 57 |
) |
| 58 |
|
| 59 |
sock = FakeSocket(body) |
| 60 |
response = client.HTTPResponse(sock) |
| 61 |
response.begin() |
| 62 |
attrs = {"open.return_value": response} |
| 63 |
opener = mock.Mock(**attrs) |
| 64 |
|
| 65 |
with mock.patch("urllib.request.build_opener") as mock_build: |
| 66 |
mock_build.return_value = opener |
| 67 |
|
| 68 |
imp = getDOMImplementation() |
| 69 |
builder = imp.createDOMBuilder(imp.MODE_SYNCHRONOUS, None) |
| 70 |
document = builder.parseURI("http://example.com/2000/svg") |
| 71 |
|
| 72 |
self.assertIsInstance(document, minidom.Document) |
| 73 |
self.assertEqual(len(document.childNodes), 1) |
| 74 |
|
| 75 |
def test_parse_with_systemId(self): |
| 76 |
response = io.BytesIO(SMALL_SAMPLE) |
| 77 |
|
| 78 |
with mock.patch("urllib.request.urlopen") as mock_open: |
| 79 |
mock_open.return_value = response |
| 80 |
|
| 81 |
imp = getDOMImplementation() |
| 82 |
source = imp.createDOMInputSource() |
| 83 |
builder = imp.createDOMBuilder(imp.MODE_SYNCHRONOUS, None) |
| 84 |
source.systemId = "http://example.com/2000/svg" |
| 85 |
document = builder.parse(source) |
| 86 |
|
| 87 |
self.assertIsInstance(document, minidom.Document) |
| 88 |
self.assertEqual(len(document.childNodes), 1) |