Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML body matching #243

Closed
hjoliveira opened this issue Apr 11, 2016 · 16 comments
Closed

XML body matching #243

hjoliveira opened this issue Apr 11, 2016 · 16 comments

Comments

@hjoliveira
Copy link

Hi there,

Are there any examples for XML response body matching in consumer tests? This pull request suggests this has been implemented, but it is not very clear to me how to do this.

Thanks!

@uglyog
Copy link
Member

uglyog commented Apr 19, 2016

Nobody has provided any examples of XML body matching, and there is no DSL like for JSON bodies.

@andersthorbeck
Copy link

XML response matching would be very useful to me as well.
I attempted doing so with a regular expression approach, but it was very difficult to get right, and eventually failed because the DOTALL embedded flag (?s) was marked in the javascript pact consumer as

SyntaxError: Invalid regular expression: /(?s).*<rootElement xmlns="http://example\.com/ns/xml">.*/: Invalid group

It seems to me, from the following tests

My question is, is there any way to match XML which uses an explicit namespace and not just the default namespace? How can this be specified using the Pact Specification?

@uglyog
Copy link
Member

uglyog commented Aug 26, 2018

XML matching is quite basic, and can not be applied based on namespaces. There is also no DSL like for JSON. You'll have to setup the matchers correctly, but it will work.

@mkinoo
Copy link

mkinoo commented Mar 16, 2020

Hi, is there any update on an XML DSL for matching?

@uglyog
Copy link
Member

uglyog commented Mar 21, 2020

@mkinoo We're added an initial version of XML support to Pact-JS with a DSL. Once we have some feedback from the community, that can be ported to Pact-JVM.

What testing framework and language would you use?

@mkinoo
Copy link

mkinoo commented Mar 23, 2020

@uglyog thanks for the info!
We'll be using junit5 & java.

@michielboekhoff
Copy link

Having tried the new XML DSL, I really like it! Thanks for implementing it so quickly.

One problem I have found is when omitting the namespace from the PactXmlBuilder - it throws an exception:

java.lang.IllegalStateException: doc.documentElement must not be null

	at au.com.dius.pact.consumer.xml.PactXmlBuilder.build(PactXmlBuilder.kt:45)
	at pacttests.consumer.butters.ButtersAddressLookupConsumerPactTest.createPact(ButtersAddressLookupConsumerPactTest.java:56)
	at au.com.dius.pact.consumer.junit.ConsumerPactTest.testPact(ConsumerPactTest.java:28)

Looking at the code in question, I suspect it's because when the namespace is omitted, builder.newDocument() is called, which returns an empty document (without the root element). The other branch - the one taken when the namespace is present - does the following:

DocumentImpl doc = new DocumentImpl(doctype);
Element e = doc.createElementNS( namespaceURI, qualifiedName);
doc.appendChild(e);

which seems to add the root element to the document.

The following code replicates this behaviour for me:

new PactXmlBuilder("address").build(root -> {
    root.appendElement("organisationName", emptyMap(), organisationName -> 
        organisationName.setAttributes(singletonMap("name", "organisationName0")));
}

Furthermore, within this DSL, how would one add values to a tag as opposed to children?

@uglyog
Copy link
Member

uglyog commented Jun 9, 2020

4.1.1 has been released with the fix

@uglyog uglyog closed this as completed Sep 30, 2020
@mfbieber
Copy link

Hi,

I've had a look at the PactXmlBuilder - thanks for the updates on that!

Unfortunately, what we really need is a DSL that generates the matching rules for an XML response, as it is implemented for JSON. I belive this is also what other people commenting on this issue were looking for?

Could you reopen the issue and give an update on whether (and possibly when) you are planning to implement this?

Thanks a lot and best regards,
Michelle

@uglyog
Copy link
Member

uglyog commented Oct 23, 2020

@mfbieber
Copy link

Oh yes, you are right of course. In the example I tested it didn't. Thanks for pointing to the other example, I think that might work for us!

Sorry for misunderstanding ;)

@shivkavitha
Copy link

Hi,

Do we have an example of PactXmlBuilder for apache kafka please ?

Many Thanks!

uglyog pushed a commit that referenced this issue Jan 20, 2021
@uglyog
Copy link
Member

uglyog commented Jan 31, 2021

@shivkavitha MessagePactBuilder already supports messages built with PactXmlBuilder

@shakul26
Copy link

is there a way to do only Schema Matchings for xml response using PactXmlBuilder ?

i can see we can add matchers for attributes but it requires exact content to match to pass from provider side

@rholshausen
Copy link
Contributor

No, that is no support for schema matching. There are lots of tools that would do that, and it makes no sense to make Pact another one.

@shakul26
Copy link

I later found we can still achieve same behaviour that is it can support schema testing as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants