Help converting a slightly more complex Wiremock.org template #417

Closed
opened 2025-12-29 15:23:15 +01:00 by adam · 17 comments
Owner

Originally created by @mattisking on GitHub (Apr 29, 2022).

Originally assigned to: @StefH on GitHub.

I'm experimenting using this instead of wiremock.org and am doing some POC stuff. I've hit a couple of problems with Body matching using XPath, and I could use some guidance on how to accomplish the response request->response items and dynamically created response parts. I have a template here (cnp.json
config.zip
)
and a partially converted one along with what to post.
https://localhost:9443/vap/communicator/online

The current issue is that my XPath matching rules both have to fail for it to fail to match, but I need ANY rule to cause it to fail. As it stands, if I change one of the values in the post but not the second it still says it's a match. Also, any help with building the response would be great.

cnp.json

{
	"Title": "Authorization",
	"Request": {
		"Path": {
			"Matchers": [
				{
					"Name": "WildcardMatcher",
					"Pattern": "/vap/communicator/online"
				}
			]
		},
		"Methods": [
			"post"
		],
		"Body": {
			"Matchers": [
				{
					"Name": "XPathMatcher",
					"Pattern": "//*[local-name() = 'amount']/text() = 100000"
				},
				{
					"Name": "XPathMatcher",
					"Pattern": "//*[local-name() = 'name']/text() = 'Jane John'"
				}
			]
		}
	},
	"Response": {
		"StatusCode": 200,
		"Body": "<xml>ok</xml>",
		"UseTransformer": false,
		"Headers": {
			"Content-Type": "application/xml"
		}
	}
}
Originally created by @mattisking on GitHub (Apr 29, 2022). Originally assigned to: @StefH on GitHub. I'm experimenting using this instead of wiremock.org and am doing some POC stuff. I've hit a couple of problems with Body matching using XPath, and I could use some guidance on how to accomplish the response request->response items and dynamically created response parts. I have a template here (cnp.json [config.zip](https://github.com/WireMock-Net/WireMock.Net/files/8594601/config.zip) ) and a partially converted one along with what to post. https://localhost:9443/vap/communicator/online The current issue is that my XPath matching rules both have to fail for it to fail to match, but I need ANY rule to cause it to fail. As it stands, if I change one of the values in the post but not the second it still says it's a match. Also, any help with building the response would be great. **cnp.json** ``` json { "Title": "Authorization", "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/vap/communicator/online" } ] }, "Methods": [ "post" ], "Body": { "Matchers": [ { "Name": "XPathMatcher", "Pattern": "//*[local-name() = 'amount']/text() = 100000" }, { "Name": "XPathMatcher", "Pattern": "//*[local-name() = 'name']/text() = 'Jane John'" } ] } }, "Response": { "StatusCode": 200, "Body": "<xml>ok</xml>", "UseTransformer": false, "Headers": { "Content-Type": "application/xml" } } } ```
adam added the question label 2025-12-29 15:23:15 +01:00
adam closed this issue 2025-12-29 15:23:16 +01:00
Author
Owner

@StefH commented on GitHub (Apr 30, 2022):

@mattisking
So in your case, this mapping should only match if both Matchers are matching.

And currently WireMock.Net works like : any are matching.

Correct?

@StefH commented on GitHub (Apr 30, 2022): @mattisking So in your case, this mapping should only match if **both** Matchers are matching. And currently WireMock.Net works like : **any** are matching. Correct?
Author
Owner

@mattisking commented on GitHub (Apr 30, 2022):

That’s correct. It’s doing an OR match and I need an AND match.

some test cases I’ll need to set 5 or more AND matches.

I also started on building the response. Using one of the examples I was looking at the random data generator which seems to work when using BodyAsJson, but there’s no BodyAsXml option and just using Body doesn’t seem to work? (I forgot how much a pain namespaces in Xml are.) This is an external service I’m mocking. Our services are Json based but I can’t control this one.

@mattisking commented on GitHub (Apr 30, 2022): That’s correct. It’s doing an OR match and I need an AND match. some test cases I’ll need to set 5 or more AND matches. I also started on building the response. Using one of the examples I was looking at the random data generator which seems to work when using BodyAsJson, but there’s no BodyAsXml option and just using Body doesn’t seem to work? (I forgot how much a pain namespaces in Xml are.) This is an external service I’m mocking. Our services are Json based but I can’t control this one.
Author
Owner

@StefH commented on GitHub (Apr 30, 2022):

  1. Sometimes the logic in WireMock.Net uses an or, sometimes it uses an average. I guess I need to verify the logic from all matchers so that these behave the same. What would be the best default behavior? Using an or or and or average?

  2. I think you can use templating for that, also for a body as a string or xml. See https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating.

@StefH commented on GitHub (Apr 30, 2022): 1. Sometimes the logic in WireMock.Net uses an `or`, sometimes it uses an `average`. I guess I need to verify the logic from all matchers so that these behave the same. What would be the best default behavior? Using an `or` or `and` or `average`? 2. I think you can use templating for that, also for a body as a string or xml. See https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating.
Author
Owner

@mattisking commented on GitHub (Apr 30, 2022):

As long as there is a way to change it between OR and AND (or average) I’m not sure that it matters. But from a logical perspective, if each item in the config is a “Matcher” of some sort I would personally expect it must follow all the matches to match. That’s my personal belief. If this is the way it works today, however, changing it might break it for others if they update in the future… that might be more a pain then me adjusting these templates out of the gate.

  • I have managed to use the link you provided to build out my response using Random Integer and Text. If you're going to build long id numbers (15-17 digit), I accomplished this using Random Text:
    {{Random Type='Text' Min=17 Max=17 UseNumber=true UseSpecial=false UseLetter=false UseSpace=false UseNullValues=false UseUppercase=false UseLowercase=false}}
    I also got the XPath.SelectSingleNode to work for me eventually, as well as eventually finding DateTime.Now as additional HandleBar.Net Helpers. So, just getting matching working the way I need is all I lack right now. Thank you.
@mattisking commented on GitHub (Apr 30, 2022): As long as there is a way to change it between OR and AND (or average) I’m not sure that it matters. But from a logical perspective, if each item in the config is a “Matcher” of some sort I would personally expect it must follow all the matches to match. That’s my personal belief. If this is the way it works today, however, changing it might break it for others if they update in the future… that might be more a pain then me adjusting these templates out of the gate. * I have managed to use the link you provided to build out my response using Random Integer and Text. If you're going to build long id numbers (15-17 digit), I accomplished this using Random Text: {{Random Type='Text' Min=17 Max=17 UseNumber=true UseSpecial=false UseLetter=false UseSpace=false UseNullValues=false UseUppercase=false UseLowercase=false}} I also got the XPath.SelectSingleNode to work for me eventually, as well as eventually finding DateTime.Now as additional HandleBar.Net Helpers. So, just getting matching working the way I need is all I lack right now. Thank you.
Author
Owner

@StefH commented on GitHub (May 14, 2022):

@mattisking
I'll take a look on how to correctly implement the OR and AND matching. I keep you posted here.

@StefH commented on GitHub (May 14, 2022): @mattisking I'll take a look on how to correctly implement the OR and AND matching. I keep you posted here.
Author
Owner

@StefH commented on GitHub (May 17, 2022):

https://github.com/WireMock-Net/WireMock.Net/pull/755

@StefH commented on GitHub (May 17, 2022): https://github.com/WireMock-Net/WireMock.Net/pull/755
Author
Owner

@StefH commented on GitHub (May 18, 2022):

@mattisking
I've implemented this functionality for path,url,clientip and body matchers.

See preview version: 1.4.42-ci-16200 (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)

It can be used like:

"Body": {
                "Matchers": [
                    {
                        "Name": "ExactMatcher",
                        "Pattern": "a"
                    },
                    {
                        "Name": "ExactMatcher",
                        "Pattern": "b"
                    }
                ],
                "MatchOperator": "Or"
            }
@StefH commented on GitHub (May 18, 2022): @mattisking I've implemented this functionality for path,url,clientip and body matchers. See preview version: `1.4.42-ci-16200` (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions) It can be used like: ``` json "Body": { "Matchers": [ { "Name": "ExactMatcher", "Pattern": "a" }, { "Name": "ExactMatcher", "Pattern": "b" } ], "MatchOperator": "Or" } ```
Author
Owner

@StefH commented on GitHub (May 20, 2022):

@mattisking
Did you have to test this new functionality?

@StefH commented on GitHub (May 20, 2022): @mattisking Did you have to test this new functionality?
Author
Owner

@mattisking commented on GitHub (May 20, 2022):

I will be testing it this weekend. Overwhelmed with a large release the past few days. Thanks a lot for looking into this.

@mattisking commented on GitHub (May 20, 2022): I will be testing it this weekend. Overwhelmed with a large release the past few days. Thanks a lot for looking into this.
Author
Owner

@StefH commented on GitHub (May 20, 2022):

@mattisking Thank your very much.

@StefH commented on GitHub (May 20, 2022): @mattisking Thank your very much.
Author
Owner

@mattisking commented on GitHub (May 23, 2022):

So far this is working well. I've tried out a couple of scenarios and I'll go ahead and bang out a few more from our scripts.

@mattisking commented on GitHub (May 23, 2022): So far this is working well. I've tried out a couple of scenarios and I'll go ahead and bang out a few more from our scripts.
Author
Owner

@StefH commented on GitHub (May 24, 2022):

I'll just keep this open for now, in case you encounter other issues.
If there are no problems for a week, I'll merge and create a new version.

@StefH commented on GitHub (May 24, 2022): I'll just keep this open for now, in case you encounter other issues. If there are no problems for a week, I'll merge and create a new version.
Author
Owner

@StefH commented on GitHub (Jun 2, 2022):

@mattisking
Did you encounter any issues?

@StefH commented on GitHub (Jun 2, 2022): @mattisking Did you encounter any issues?
Author
Owner

@mattisking commented on GitHub (Jun 4, 2022):

Not so far. I appreciate the effort. I like wiremock and in this case you’ve really added some nice additions. Now we should build on wiremock studio 😉

@mattisking commented on GitHub (Jun 4, 2022): Not so far. I appreciate the effort. I like wiremock and in this case you’ve really added some nice additions. Now we should build on wiremock studio 😉
Author
Owner

@StefH commented on GitHub (Jun 9, 2022):

@mattisking I'll merge the PR and create a new version (1.5.0)

wiremock studio ? You mean a version for this WireMock.NET ?

@StefH commented on GitHub (Jun 9, 2022): @mattisking I'll merge the PR and create a new version (1.5.0) wiremock studio ? You mean a version for this WireMock.NET ?
Author
Owner

@mattisking commented on GitHub (Jun 11, 2022):

Yes I just mean that the open source wiremock studio isn’t bad and is a decent editor. One of the things I’m working to accomplish is to empower our QA people with tools to build their own mocks for their test cases. That’s a stretch goal. I’m more than capable of editing template files.

@mattisking commented on GitHub (Jun 11, 2022): Yes I just mean that the open source wiremock studio isn’t bad and is a decent editor. One of the things I’m working to accomplish is to empower our QA people with tools to build their own mocks for their test cases. That’s a stretch goal. I’m more than capable of editing template files.
Author
Owner

@StefH commented on GitHub (Jun 11, 2022):

Maybe a good use case to build this in .net Maui 😀

@StefH commented on GitHub (Jun 11, 2022): Maybe a good use case to build this in .net Maui 😀
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#417