示例#1
0
    def test_text(self):
        e = Element('e')
        e.c = '<img src="foo">'
        self.assertEqual(e.__repr__(1), '<e><c>&lt;img src="foo"></c></e>')
        e.c = '2 > 4'
        self.assertEqual(e.__repr__(1), '<e><c>2 > 4</c></e>')
        e.c = 'CDATA sections are <em>closed</em> with ]]>.'
        self.assertEqual(
            e.__repr__(1),
            '<e><c>CDATA sections are &lt;em>closed&lt;/em> with ]]&gt;.</c></e>'
        )
        e.c = parse(
            '<div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div>'
        )
        self.assertEqual(e.__repr__(1), (
            '<e><c><div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you'
            '</div></c></e>'))

        e = Element('e')
        e('c', 'that "sucks"')
        self.assertEqual(e.__repr__(1), '<e c="that &quot;sucks&quot;"></e>')

        self.assertEqual(quote("]]>"), "]]&gt;")
        self.assertEqual(quote('< dkdkdsd dkd sksdksdfsd fsdfdsf]]> kfdfkg >'),
                         '&lt; dkdkdsd dkd sksdksdfsd fsdfdsf]]&gt; kfdfkg >')

        self.assertEqual(
            parse('<x a="&lt;"></x>').__repr__(1), '<x a="&lt;"></x>')
        self.assertEqual(
            parse('<a xmlns="http://a"><b xmlns="http://b"/></a>').__repr__(1),
            '<a xmlns="http://a"><b xmlns="http://b"></b></a>')
示例#2
0
 def test_str(self):
     self.assertEqual(str(parse("<doc />")), "")
     self.assertEqual(str(parse("<doc>I <b>love</b> you.</doc>")),
                      "I love you.")
     self.assertEqual(
         parse("<doc>\nmom\nwow\n</doc>")[0].strip(), "mom\nwow")
     self.assertEqual(
         str(parse('<bing>  <bang> <bong>center</bong> </bang>  </bing>')),
         "center")
     self.assertEqual(str(parse('<doc>\xcf\x80</doc>')), '\xcf\x80')
示例#3
0
 def test_xsiNilWithAtttrs(self):
     w = beatbox.SoapWriter()
     a = {(beatbox._envNs, "mustUnderstand"): "1"}
     w.writeStringElement("http://schemas.xmlsoap.org/soap/envelope/", "Header", None, a)
     xml = w.endDocument()
     # because attributes aren't ordered, we can't do a simple sting assert check here
     # we need to actually parse and verify the xml that way
     root = xmltramp.parse(xml)
     hdr = root[0]
     soapNs = xmltramp.Namespace("http://schemas.xmlsoap.org/soap/envelope/")
     xsiNs = xmltramp.Namespace("http://www.w3.org/2001/XMLSchema-instance")
     self.assertEqual("1", hdr(soapNs.mustUnderstand))
     self.assertEqual("true", hdr(xsiNs.nil))
示例#4
0
    def post(self, conn=None, alwaysReturnList=False):
        """Complete the envelope and send the request

        does all the grunt work,
          serializes the request,
          makes a http request,
          passes the response to tramp
          checks for soap fault
          todo: check for mU='1' headers
          returns the relevant result from the body child
        """
        headers = {
            "User-Agent": "BeatBox/" + __version__,
            "SOAPAction": '""',
            "Content-Type": "text/xml; charset=utf-8"
        }
        if beatbox.gzipResponse:
            headers['accept-encoding'] = 'gzip'
        if beatbox.gzipRequest:
            headers['content-encoding'] = 'gzip'
        close = False
        (scheme, host, path, params, query, frag) = urlparse(self.serverUrl)
        if conn is None:
            conn = makeConnection(scheme, host)
            close = True
        rawRequest = self.makeEnvelope()
        # print(rawRequest)
        conn.request("POST", self.serverUrl, rawRequest, headers)
        response = conn.getresponse()
        rawResponse = response.read()
        if response.getheader('content-encoding', '') == 'gzip':
            rawResponse = gzip.GzipFile(fileobj=BytesIO(rawResponse)).read()
        if close:
            conn.close()
        tramp = xmltramp.parse(rawResponse)
        try:
            faultString = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultstring)
            faultCode = str(
                tramp[_tSoapNS.Body][_tSoapNS.Fault].faultcode).split(':')[-1]
            raise SoapFaultError(faultCode, faultString)
        except KeyError:
            pass
        # first child of body is XXXXResponse
        result = tramp[_tSoapNS.Body][0]
        # it contains either a single child, or for a batch call multiple children
        if alwaysReturnList or len(result) > 1:
            return result[:]
        else:
            return result[0]
示例#5
0
    def test_text(self):
        e = Element('e')
        e.c = '<img src="foo">'
        self.assertEqual(e.__repr__(1), '<e><c>&lt;img src="foo"></c></e>')
        e.c = '2 > 4'
        self.assertEqual(e.__repr__(1), '<e><c>2 > 4</c></e>')
        e.c = 'CDATA sections are <em>closed</em> with ]]>.'
        self.assertEqual(e.__repr__(1), '<e><c>CDATA sections are &lt;em>closed&lt;/em> with ]]&gt;.</c></e>')
        e.c = parse('<div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div>')
        self.assertEqual(e.__repr__(1), (
                '<e><c><div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you'
                '</div></c></e>'))

        e = Element('e')
        e('c', 'that "sucks"')
        self.assertEqual(e.__repr__(1), '<e c="that &quot;sucks&quot;"></e>')

        self.assertEqual(quote("]]>"), "]]&gt;")
        self.assertEqual(quote('< dkdkdsd dkd sksdksdfsd fsdfdsf]]> kfdfkg >'),
                         '&lt; dkdkdsd dkd sksdksdfsd fsdfdsf]]&gt; kfdfkg >')

        self.assertEqual(parse('<x a="&lt;"></x>').__repr__(1), '<x a="&lt;"></x>')
        self.assertEqual(parse('<a xmlns="http://a"><b xmlns="http://b"/></a>').__repr__(1),
                         '<a xmlns="http://a"><b xmlns="http://b"></b></a>')
示例#6
0
    def post(self, conn=None, alwaysReturnList=False):
        """Complete the envelope and send the request

        does all the grunt work,
          serializes the request,
          makes a http request,
          passes the response to tramp
          checks for soap fault
          todo: check for mU='1' headers
          returns the relevant result from the body child
        """
        headers = {"User-Agent": "BeatBox/" + __version__,
                   "SOAPAction": '""',
                   "Content-Type": "text/xml; charset=utf-8"}
        if beatbox.gzipResponse:
            headers['accept-encoding'] = 'gzip'
        if beatbox. gzipRequest:
            headers['content-encoding'] = 'gzip'
        close = False
        (scheme, host, path, params, query, frag) = urlparse(self.serverUrl)
        if conn is None:
            conn = makeConnection(scheme, host)
            close = True
        rawRequest = self.makeEnvelope()
        # print(rawRequest)
        conn.request("POST", self.serverUrl, rawRequest, headers)
        response = conn.getresponse()
        rawResponse = response.read()
        if response.getheader('content-encoding', '') == 'gzip':
            rawResponse = gzip.GzipFile(fileobj=BytesIO(rawResponse)).read()
        if close:
            conn.close()
        tramp = xmltramp.parse(rawResponse)
        try:
            faultString = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultstring)
            faultCode = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultcode).split(':')[-1]
            raise SoapFaultError(faultCode, faultString)
        except KeyError:
            pass
        # first child of body is XXXXResponse
        result = tramp[_tSoapNS.Body][0]
        # it contains either a single child, or for a batch call multiple children
        if alwaysReturnList or len(result) > 1:
            return result[:]
        else:
            return result[0]
示例#7
0
 def test_str(self):
     self.assertEqual(str(parse("<doc />")), "")
     self.assertEqual(str(parse("<doc>I <b>love</b> you.</doc>")), "I love you.")
     self.assertEqual(parse("<doc>\nmom\nwow\n</doc>")[0].strip(), "mom\nwow")
     self.assertEqual(str(parse('<bing>  <bang> <bong>center</bong> </bang>  </bing>')), "center")
     self.assertEqual(str(parse('<doc>\xcf\x80</doc>')), '\xcf\x80')
示例#8
0
    def test_ns(self):
        d = Element('foo')

        doc = Namespace("http://example.org/bar")
        bbc = Namespace("http://example.org/bbc")
        dc = Namespace("http://purl.org/dc/elements/1.1/")
        d = parse("""
        <doc version="2.7182818284590451"
          xmlns="http://example.org/bar"
          xmlns:dc="http://purl.org/dc/elements/1.1/"
          xmlns:bbc="http://example.org/bbc">
            <author>John Polk and John Palfrey</author>
            <dc:creator>John Polk</dc:creator>
            <dc:creator>John Palfrey</dc:creator>
            <bbc:show bbc:station="4">Buffy</bbc:show>
        </doc>""")

        self.assertEqual(repr(d), '<doc version="2.7182818284590451">...</doc>')
        # the order of xmlns attributes is not garanteed invariant
        self.assertEqual(
                d.__repr__(1),
                '<doc xmlns="http://example.org/bar" xmlns:bbc="http://example.org/bbc"'
                ' xmlns:dc="http://purl.org/dc/elements/1.1/"'
                ' version="2.7182818284590451">'
                '<author>John Polk and John Palfrey</author>'
                '<dc:creator>John Polk</dc:creator>'
                '<dc:creator>John Palfrey</dc:creator>'
                '<bbc:show bbc:station="4">Buffy</bbc:show>'
                '</doc>'
                )
        self.assertEqual(
                d.__repr__(1, 1),
                '<doc xmlns="http://example.org/bar"'
                ' xmlns:bbc="http://example.org/bbc" xmlns:dc="http://purl.org/dc/elements/1.1/"'
                ' version="2.7182818284590451">\n'
                '\t<author>John Polk and John Palfrey</author>\n'
                '\t<dc:creator>John Polk</dc:creator>\n'
                '\t<dc:creator>John Palfrey</dc:creator>\n'
                '\t<bbc:show bbc:station="4">Buffy</bbc:show>\n'
                '</doc>',
                )

        self.assertEqual(
                parse('<doc>a<baz>f<b>o</b>ob<b>a</b>r</baz>a</doc>').__repr__(1, 1),
                '<doc>\n'
                '\ta\n'
                '\t<baz>\n'
                '\t\tf\n'
                '\t\t<b>o</b>\n'
                '\t\tob\n'
                '\t\t<b>a</b>\n'
                '\t\tr\n'
                '\t</baz>\n'
                '\ta\n'
                '</doc>')

        self.assertEqual(repr(parse("<doc xml:lang='en' />")), '<doc xml:lang="en"></doc>')

        self.assertEqual(str(d.author), str(d['author']))
        self.assertEqual(str(d.author), "John Polk and John Palfrey")
        self.assertEqual(d.author._name, doc.author)
        self.assertEqual(str(d[dc.creator]), "John Polk")
        self.assertEqual(d[dc.creator]._name, dc.creator)
        self.assertEqual(str(d[dc.creator:][1]), "John Palfrey")
        d[dc.creator] = "Me!!!"
        self.assertEqual(str(d[dc.creator]), "Me!!!")
        self.assertEqual(len(d[dc.creator:]), 1)
        d[dc.creator:] = "You!!!"
        self.assertEqual(len(d[dc.creator:]), 2)

        self.assertEqual(d[bbc.show](bbc.station), "4")
        d[bbc.show](bbc.station, "5")
        self.assertEqual(d[bbc.show](bbc.station), "5")
示例#9
0
    def test_ns(self):
        d = Element('foo')

        doc = Namespace("http://example.org/bar")
        bbc = Namespace("http://example.org/bbc")
        dc = Namespace("http://purl.org/dc/elements/1.1/")
        d = parse("""
        <doc version="2.7182818284590451"
          xmlns="http://example.org/bar"
          xmlns:dc="http://purl.org/dc/elements/1.1/"
          xmlns:bbc="http://example.org/bbc">
            <author>John Polk and John Palfrey</author>
            <dc:creator>John Polk</dc:creator>
            <dc:creator>John Palfrey</dc:creator>
            <bbc:show bbc:station="4">Buffy</bbc:show>
        </doc>""")

        self.assertEqual(repr(d),
                         '<doc version="2.7182818284590451">...</doc>')
        # the order of xmlns attributes is not garanteed invariant
        self.assertEqual(
            d.__repr__(1),
            '<doc xmlns="http://example.org/bar" xmlns:bbc="http://example.org/bbc"'
            ' xmlns:dc="http://purl.org/dc/elements/1.1/"'
            ' version="2.7182818284590451">'
            '<author>John Polk and John Palfrey</author>'
            '<dc:creator>John Polk</dc:creator>'
            '<dc:creator>John Palfrey</dc:creator>'
            '<bbc:show bbc:station="4">Buffy</bbc:show>'
            '</doc>')
        self.assertEqual(
            d.__repr__(1, 1),
            '<doc xmlns="http://example.org/bar"'
            ' xmlns:bbc="http://example.org/bbc" xmlns:dc="http://purl.org/dc/elements/1.1/"'
            ' version="2.7182818284590451">\n'
            '\t<author>John Polk and John Palfrey</author>\n'
            '\t<dc:creator>John Polk</dc:creator>\n'
            '\t<dc:creator>John Palfrey</dc:creator>\n'
            '\t<bbc:show bbc:station="4">Buffy</bbc:show>\n'
            '</doc>',
        )

        self.assertEqual(
            parse('<doc>a<baz>f<b>o</b>ob<b>a</b>r</baz>a</doc>').__repr__(
                1, 1), '<doc>\n'
            '\ta\n'
            '\t<baz>\n'
            '\t\tf\n'
            '\t\t<b>o</b>\n'
            '\t\tob\n'
            '\t\t<b>a</b>\n'
            '\t\tr\n'
            '\t</baz>\n'
            '\ta\n'
            '</doc>')

        self.assertEqual(repr(parse("<doc xml:lang='en' />")),
                         '<doc xml:lang="en"></doc>')

        self.assertEqual(str(d.author), str(d['author']))
        self.assertEqual(str(d.author), "John Polk and John Palfrey")
        self.assertEqual(d.author._name, doc.author)
        self.assertEqual(str(d[dc.creator]), "John Polk")
        self.assertEqual(d[dc.creator]._name, dc.creator)
        self.assertEqual(str(d[dc.creator:][1]), "John Palfrey")
        d[dc.creator] = "Me!!!"
        self.assertEqual(str(d[dc.creator]), "Me!!!")
        self.assertEqual(len(d[dc.creator:]), 1)
        d[dc.creator:] = "You!!!"
        self.assertEqual(len(d[dc.creator:]), 2)

        self.assertEqual(d[bbc.show](bbc.station), "4")
        d[bbc.show](bbc.station, "5")
        self.assertEqual(d[bbc.show](bbc.station), "5")