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

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

Closed
mattisking opened this issue Apr 29, 2022 · 17 comments
Closed

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

mattisking opened this issue Apr 29, 2022 · 17 comments
Assignees
Labels

Comments

@mattisking
Copy link
Contributor

mattisking commented Apr 29, 2022

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"
		}
	}
}
@StefH
Copy link
Collaborator

StefH commented 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?

@mattisking
Copy link
Contributor Author

mattisking commented 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.

@StefH
Copy link
Collaborator

StefH commented 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 StefH self-assigned this Apr 30, 2022
@mattisking
Copy link
Contributor Author

mattisking commented 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.

@StefH
Copy link
Collaborator

StefH commented 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
Copy link
Collaborator

StefH commented May 17, 2022

#755

@StefH
Copy link
Collaborator

StefH commented 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
Copy link
Collaborator

StefH commented May 20, 2022

@mattisking
Did you have to test this new functionality?

@mattisking
Copy link
Contributor Author

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

@StefH
Copy link
Collaborator

StefH commented May 20, 2022

@mattisking Thank your very much.

@mattisking
Copy link
Contributor Author

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.

@StefH
Copy link
Collaborator

StefH commented 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 StefH reopened this May 24, 2022
@StefH
Copy link
Collaborator

StefH commented Jun 2, 2022

@mattisking
Did you encounter any issues?

@mattisking
Copy link
Contributor Author

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 😉

@StefH
Copy link
Collaborator

StefH commented 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 StefH closed this as completed Jun 9, 2022
@mattisking
Copy link
Contributor Author

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.

@StefH
Copy link
Collaborator

StefH commented Jun 11, 2022

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

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

No branches or pull requests

2 participants