def test_gen_getitem2(self): orig = """ <a xmlns="example.com"> <b xmlns="example.com/2"> <c>ddd</c> </b> </a> """ doc1 = xml() doc1.a.ns = 'example.com' doc1.a.b.ns = 'example.com/2' doc1.a.b.c = 'ddd' doc2 = xml() doc2['a'].ns = 'example.com' doc2.a['b'].ns = 'example.com/2' doc2.a.b['c'] = 'ddd' doc3 = xml() doc3['a'].ns = 'example.com' doc3['a']['b'].ns = 'example.com/2' doc3['a'].b['c'] = 'ddd' compare_xml(orig, doc1.to_xml()) compare_xml(orig, doc2.to_xml()) compare_xml(orig, doc3.to_xml())
def test_gen_attr2(self): orig = """ <a aa="1" xmlns="example.com"> <b bb="2" bbb="22" xmlns="example.com/2"> <c cc="3" ccc="33">ddd</c> </b> </a> """ doc1 = xml() doc1.ns = 'example.com' doc1.a._aa = '1' doc1.a.b.ns = 'example.com/2' doc1.a.b._bb = '2' doc1.a.b._bbb = '22' doc1.a.b.c._cc = '3' doc1.a.b.c._ccc = '33' doc1.a.b.c = 'ddd' doc2 = xml() doc2.a.ns = 'example.com' doc2.a._aa = '1' doc2.a.b.ns = 'example.com/2' doc2.a.b._bb = '2' doc2.a.b._bbb = '22' doc2.a.b.c._cc = '3' doc2.a.b.c._ccc = '33' doc2.a.b.c = 'ddd' compare_xml(orig, doc1.to_xml()) compare_xml(orig, doc2.to_xml())
def test_to_xml_to_lxml_cleanup_ns_false(self): orig = """ <root> <a xmlns:x="bar/x"> <x:b foo="bar/foo1"> <x:ccc xmlns:rep="bar/rep">111</x:ccc> </x:b> <x:b foo="bar/foo2"/> </a> </root> """ doc = xml() doc.ns_map += {'x':'bar/x', 'rep':'bar/rep'} root = doc.root b = root.a.x_b[0] b._foo = 'bar/foo1' b.x_ccc = '111' root.a.x_b[1]._foo = 'bar/foo2' expected = b'<root><a><ns0:b xmlns:ns0="bar/x" foo="bar/foo1"><ns0:ccc>111</ns0:ccc></ns0:b><ns1:b xmlns:ns1="bar/x"\ foo="bar/foo2"/></a></root>' result = doc.to_xml(cleanup_ns=False) # Compare compare_xml(orig, result) # Here lxml assigned its own namespace prefixes self.assertEquals(expected, result)
def main(n): for x in range(n): doc = xml() doc.ns_map += {'rem':'http://remoting.example.com/'}, default_ns.s12 header = doc.s12_Envelope.s12_Header header.wsa_Action = 'urn:hl7-org:v3:MCCI_IN000002UV01' header.wsa_Action._s12_mustUnderstand = '1' header.wsa_Action._actor_type = '2' header.wsa_MessageID = 'uuid:123' header.wsa_ReplyTo.wsa_Address = 'http://www.w3.org/2005/08/addressing/anonymous' arg0 = doc.s12_Envelope.s12_Body.rem_usrOrgRoleLogin.arg0 arg0.user = '******' arg0._rem_is_req = 'true' arg0.pwd = 'my-password' arg0.role = 'my-role' arg0.org = 'my-org' arg0.rem_access[0] = 'no' arg0.rem_access[0]._rem_type = '0' arg0.rem_access[0].access = '000' arg0.rem_access[1] = 'yes' arg0.rem_access[1]._rem_type = '1' arg0.rem_access[1].access = '111' doc.to_json() return doc
def test_gen5(self): orig = """ <ReferenceList xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> <DataReference URI="#invoice34"> <ds:Transforms> <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"> <ds:XPath xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> self::xenc:EncryptedData[@Id="example1"] </ds:XPath> </ds:Transform> </ds:Transforms> </DataReference> </ReferenceList> """ doc = xml() doc.ns_map += default_ns.dsig, default_ns.xenc doc.ReferenceList.DataReference._URI = '#invoice34' transforms = doc.ReferenceList.DataReference.dsig_Transforms transforms.dsig_Transform._Algorithm = 'http://www.w3.org/TR/1999/REC-xpath-19991116' transforms.dsig_Transform.dsig_XPath = 'self::xenc:EncryptedData[@Id="example1"]' # Compare compare_xml(orig, doc.to_xml())
def test_gen2(self): orig = """ <?xml version='1.0'?> <PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <CreditCard Limit='5,000' Currency='USD'> <Number>4019 2445 0277 5567</Number> <Issuer>Example Bank</Issuer> <Expiration>04/02</Expiration> </CreditCard> </PaymentInfo> """ doc = xml() doc.ns = 'http://example.org/paymentv2' info = doc.PaymentInfo info.Name = 'John Smith' info.CreditCard._Limit = '5,000' info.CreditCard._Currency = 'USD' info.CreditCard.Number = '4019 2445 0277 5567' info.CreditCard.Issuer = 'Example Bank' info.CreditCard.Expiration = '04/02' # Compare compare_xml(orig, doc.to_xml())
def test_gen4(self): orig = """ <CipherReference URI="http://www.example.com/CipherValues.xml"> <Transforms xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"> <ds:XPath xmlns:rep="http://www.example.org/repository"> self::text()[parent::rep:CipherValue[@Id="example1"]] </ds:XPath> </ds:Transform> <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#base64"/> </Transforms> </CipherReference> """ doc = xml() doc.ns_map += default_ns.dsig doc.ns_map += {'rep':'http://www.example.org/repository'} cipher_ref = doc.CipherReference cipher_ref._URI = 'http://www.example.com/CipherValues.xml' transform = cipher_ref.Transforms.dsig_Transform[0] transform._Algorithm = 'http://www.w3.org/TR/1999/REC-xpath-19991116' transform.dsig_XPath = 'self::text()[parent::rep:CipherValue[@Id="example1"]]' cipher_ref.Transforms.dsig_Transform[1]._Algorithm = 'http://www.w3.org/2000/09/xmldsig#base64' # Compare compare_xml(orig, doc.to_xml())
def test_gen3(self): orig = """ <?xml version='1.0'?> <PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <CreditCard Limit='5,000' Currency='USD'> <EncryptedData xmlns='http://www.w3.org/2001/04/xmlenc#' Type='http://www.w3.org/2001/04/xmlenc#Content'> <CipherData> <CipherValue>A23B45C56</CipherValue> </CipherData> </EncryptedData> </CreditCard> </PaymentInfo> """ doc = xml() doc.ns = 'http://example.org/paymentv2' info = doc.PaymentInfo info.Name = 'John Smith' info.CreditCard._Limit = '5,000' info.CreditCard._Currency = 'USD' enc_data = info.CreditCard.EncryptedData enc_data.ns = default_ns.xenc enc_data._Type = 'http://www.w3.org/2001/04/xmlenc#Content' enc_data.CipherData.CipherValue = 'A23B45C56' # Compare compare_xml(orig, doc.to_xml())
def test_to_xml_to_lxml_cleanup_ns_true(self): orig = """ <root> <a xmlns:x="bar/x"> <x:b foo="bar/foo1"> <x:ccc xmlns:rep="bar/rep">111</x:ccc> </x:b> <x:b foo="bar/foo2"/> </a> </root> """ doc = xml() doc.ns_map += {'x':'bar/x', 'rep':'bar/rep'} root = doc.root b = root.a.x_b[0] b._foo = 'bar/foo1' b.x_ccc = '111' root.a.x_b[1]._foo = 'bar/foo2' expected = b'<root xmlns:x="bar/x"><a><x:b foo="bar/foo1"><x:ccc>111</x:ccc></x:b><x:b foo="bar/foo2"/></a></root>' result = doc.to_xml() # Compare compare_xml(orig, result) # Now, we expect for our namespace to be defined as in 'result', i.e. each namespace is declared once self.assertEquals(expected, result)
def test_diff_noparse_markup(self): orig = """ <root> <a> <b>ccc</b> </a> </root> """ doc = xml() doc.root.a.b.ddd = 'eee' expected = """ Expected: <root> <a> <b>ccc</b> </a> </root> Got: <root><a><b><ddd>eee</ddd></b></a></root> """ try: compare_xml(orig, doc.to_xml(), NOPARSE_MARKUP) except AssertionError as e: self.assertEquals(e.args[0].strip(), expected.strip())
def test_gen1(self): orig = """ <EncryptedData xmlns='http://www.w3.org/2001/04/xmlenc#' Type='http://www.w3.org/2001/04/xmlenc#Element'> <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#tripledes-cbc'/> <ds:KeyInfo xmlns:ds='http://www.w3.org/2000/09/xmldsig#'> <ds:KeyName>John Smith</ds:KeyName> </ds:KeyInfo> <CipherData> <CipherValue>ABCDEF</CipherValue> </CipherData> </EncryptedData> """ # Main structure + NS doc = xml() doc.ns = default_ns.xenc doc.ns_map += default_ns.dsig # Top-level data = doc.EncryptedData data._Type = 'http://www.w3.org/2001/04/xmlenc#Element' # Children data.EncryptionMethod._Algorithm = 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' data.dsig_KeyInfo.dsig_KeyName = 'John Smith' data.CipherData.CipherValue = 'ABCDEF' # Compare compare_xml(orig, doc.to_xml())
def test_to_xml_no_root(self): doc = xml() try: doc.to_xml() except ValueError as e: self.assertEquals(e.args[0], 'No root node found') else: self.fail('No root yet no ValueError raised')
def test_gen1(self): orig = """ <ns0:Envelope xmlns:ns0="http://www.w3.org/2003/05/soap-envelope" xmlns:rem="http://remoting.example.com" xmlns:wsa="http://www.w3.org/2005/08/addressing"> <ns0:Header> <wsa:Action ns0:mustUnderstand="1" ns0:type="2">urn:hl7-org:v3:MCCI_IN000002UV01</wsa:Action> <wsa:MessageID>uuid:123</wsa:MessageID> <wsa:ReplyTo> <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address> </wsa:ReplyTo> </ns0:Header> <ns0:Body> <rem:usrOrgRoleLogin> <arg0 rem:keep_alive="true"> <user>my-user</user> <pwd>my-password</pwd> <role>my-role</role> <org>my-org</org> <rem:access rem:type="0"> no <access>000</access> </rem:access> <rem:access rem:type="1"> yes <access>111</access> </rem:access> </arg0> </rem:usrOrgRoleLogin> </ns0:Body> </ns0:Envelope> """ doc = xml() doc.ns_map += default_ns.s12, default_ns.wsa, {'rem':'http://remoting.example.com'} header = doc.s12_Envelope.s12_Header header.wsa_Action = 'urn:hl7-org:v3:MCCI_IN000002UV01' header.wsa_Action._s12_mustUnderstand = '1' header.wsa_Action._s12_type = '2' header.wsa_MessageID = 'uuid:123' header.wsa_ReplyTo.wsa_Address = 'http://www.w3.org/2005/08/addressing/anonymous' arg0 = doc.s12_Envelope.s12_Body.rem_usrOrgRoleLogin.arg0 arg0.user = '******' arg0._rem_keep_alive = 'true' arg0.pwd = 'my-password' arg0.role = 'my-role' arg0.org = 'my-org' arg0.rem_access[0] = 'no' arg0.rem_access[0]._rem_type = '0' arg0.rem_access[0].access = '000' arg0.rem_access[1] = 'yes' arg0.rem_access[1]._rem_type = '1' arg0.rem_access[1].access = '111' compare_xml(orig, doc.to_xml())
def test_gen1(self): orig = """ <greeting>Hello, world!</greeting> """ doc = xml() doc.greeting = 'Hello, world!' compare_xml(orig, doc.to_xml())
def test_gen2(self): orig = """ <edi:price xmlns:edi='http://ecommerce.example.org/schema' units='Euro'>32.18</edi:price> """ doc = xml() doc.ns_map += {'edi':'http://ecommerce.example.org/schema'} doc.edi_price = '32.18' doc.edi_price._units = 'Euro' compare_xml(orig, doc.to_xml())
def test_gen1(self): orig = """ <x xmlns:edi='http://ecommerce.example.org/schema'> </x> """ doc = xml() doc.ns_map += {'edi':'http://ecommerce.example.org/schema'} doc.x = '' compare_xml(orig, doc.to_xml())
def test_to_xml_defaults(self): orig = """ <root xmlns="example.com"> <aaa>111</aaa> </root> """ doc = xml() doc.root.ns = 'example.com' doc.root.aaa = '111' compare_xml(orig, doc.to_xml())
def test_ok(self): orig = """ <root> <a> <b>ccc</b> </a> </root> """ doc = xml() doc.root.a.b = 'ccc' compare_xml(orig, doc.to_xml())
def test_to_xml_serialize_non_root(self): expected = """ <d> <e> <f>111</f> </e> </d> """ doc = xml() doc.a.b.c.d.e.f = '111' compare_xml(expected, doc.a.b.c.d.to_xml())
def test_ok_ns_custom(self): orig = """ <root xmlns:x="example.com" xmlns:q="example.com/2"> <x:a> <q:b>ccc</q:b> </x:a> </root> """ doc = xml() doc.ns_map += {'x':'example.com', 'q':'example.com/2'} doc.root.x_a.q_b = 'ccc' compare_xml(orig, doc.to_xml())
def test_ok_ns_default(self): orig = """ <root xmlns="example.com"> <a> <b>ccc</b> </a> </root> """ doc = xml() doc.ns = 'example.com' doc.root.a.b = 'ccc' compare_xml(orig, doc.to_xml())
def test_to_xml_multiple_roots(self): doc = xml() doc.aaa = '123' doc.bbb = '456' try: doc.to_xml() except ValueError as e: self.assertRegexpMatches( e.args[0], r'Multiple roots found: `\[<xml at 0x\w+ /aaa `123`>, <xml at 0x\w+ /bbb `456`>\]`') else: self.fail('Multiple roots yet no ValueError raised')
def test_gen_default_ns_elem(self): orig = """ <root xmlns="example.com"> <x:a xmlns:x="example.com/x"> <b>123</b> </x:a> </root> """ doc = xml() doc.root.ns = 'example.com' doc.root.ns_map += {'x':'example.com/x'} doc.root.x_a.b = '123' compare_xml(orig, doc.to_xml())
def test_to_xml_to_lxml(self): orig = """ <root xmlns="example.com"> <aaa>111</aaa> </root> """ doc = xml() doc.root.ns = 'example.com' doc.root.aaa = '111' result = doc.to_xml(False) self.assertIsInstance(result, _Element) compare_xml(orig, tostring(result))
def test_gen_set_already_existing_ns(self): orig = """ <root xmlns="example.com"> <x:a xmlns:x="example.com/x"> <b>123</b> </x:a> </root> """ doc = xml() doc.root.ns = 'example.com' doc.root.ns_map += {'x':'example.com/x'} doc.root.x_a.b = '123' doc.root.x_a.ns = 'example.com/x' # Set explicitely and expected to reuse prefix defined in doc.root.ns_map compare_xml(orig, doc.to_xml())
def test_ok(self): orig = """ <aaa> <bbb>0</bbb> <bbb>1</bbb> <bbb>2</bbb> <bbb>3</bbb> </aaa> """ doc = xml() doc.aaa.bbb[0] = '0' doc.aaa.bbb[1] = '1' doc.aaa.bbb[2] = '2' doc.aaa.bbb[3] = '3' compare_xml(orig, doc.to_xml())
def test_gen4(self): orig = """ <root> <bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <bk:title>Cheaper by the Dozen</bk:title> <isbn:number>1568491379</isbn:number> </bk:book> </root> """ doc = xml() doc.ns_map += {'bk':'urn:loc.gov:books', 'isbn':'urn:ISBN:0-395-36341-6'} doc.root.bk_book.bk_title = 'Cheaper by the Dozen' doc.root.bk_book.isbn_number = '1568491379' compare_xml(orig, doc.to_xml())
def test_gen_attr1(self): orig = """ <a aa="1"> <b bb="2" bbb="22"> <c cc="3" ccc="33">ddd</c> </b> </a> """ doc = xml() doc.a._aa = '1' doc.a.b._bb = '2' doc.a.b._bbb = '22' doc.a.b.c._cc = '3' doc.a.b.c._ccc = '33' doc.a.b.c = 'ddd' compare_xml(orig, doc.to_xml())
def test_gen7(self): orig = """ <x xmlns:n1="example.com" xmlns="example.com"> <good a="1" b="2" /> <good a="1" n1:a="2" /> </x> """ doc = xml() doc.ns = 'example.com' doc.ns_map += {'n1':'example.com'} doc.x.good[0]._a = '1' doc.x.good[0]._b = '2' doc.x.good[1]._a = '1' doc.x.good[1]._n1_a = '2' compare_xml(orig, doc.to_xml())
def test_one_missing_ns_custom(self): """ <z:aaa z:xmlns="example.com"> <bbb>0</bbb> <bbb>1</bbb> </z:aaa> """ doc = xml() doc.ns_map += {'z': 'example.com'} doc.z_aaa.bbb[0] = '0' doc.z_aaa.bbb[1] = '1' try: doc.z_aaa.bbb[3] = '3' except IndexError as e: self.assertEquals(e.args[0], 'Cannot access idx 3, /z:aaa/bbb[2] is missing') else: self.fail('Expected IndexError not raised')