add XML templated matcher#939
Conversation
* add test cases * fmt
|
This matcher sounds interesting. My only reservation is the naming, because what Then it will match on any XML document with that node. However this new matcher you are proposing still matches the entire XML, but with some part of it replaced with templates. It would be good to call it something else, to avoid the discrepancy with the JSON partial matcher. |
|
how about |
|
Thanks for your explanation. At first, we made it with features of
Then, we realized that these two features may need very complex rules if we use them together. For example, we can not do the list matching described above, because partial matching means some nodes are already ignored. Finally, to keep sample, we removed the second feature to avoid very complex matching rules. |
|
OK. I changed all of the |
tommysitu
left a comment
There was a problem hiding this comment.
top notch testing, and no major issues. Happy to make those changes for you if you prefer, as I'm going to do a release in the coming day.
| var XmlTemplated = "xmltemplated" | ||
|
|
||
| var ignoreExpr = regexp.MustCompile("^\\s*\\{\\{\\s*ignore\\s*\\}\\}\\s*$") | ||
| var regExpr = regexp.MustCompile("^\\s*\\{\\{\\s*regex:(.*)\\}\\}\\s*$") |
There was a problem hiding this comment.
The Hoverfly response template function is in this format: {{ function_name arg1, arg2.. }}
So I would suggest replacing the : with space as delimiter such as {{ regex your_regex_string_value }} for consistency.
There was a problem hiding this comment.
Thank you for merged it, and sorry for long time no commits.
I am a little busy recently. After we finish our tasks, I will add a commit for this.
There was a problem hiding this comment.
👍 whenever you have some time.
XML Templated Matcher
Motivation (Why we made it)
When mocking HTTP request with XML body, a matcher support partial matching is needed if we want to ignore some data like timestamp, or to match an auto generated string with a fixed pattern.
Design
We add a matcher called
xmlTemplated, who can define the expected XML data with 3 kinds of expressions for the value in a node; that is, in XML node<tag>expression</tag>,expressioncan be one of the following three.{{ignore}}{{regex:RegularExpression}}※ The reserved keyword
ignoreandregexare case-sensitive.{ "matcher" : "xmlpartial", "value" : "<xml><item>123<item><item>{{regex:^[A-Z]\\d{5}$}}</item><item>{{ignore}}</item></xml>" }If more than one kind of expression occurs in an list node of XML data, the matching process will loop on all nodes in the matcher, and in each round, pick one matched item from the actual data in HTTP request and mark it as consumed which can not be picked in the next rounds. If no matched item found in a round, the matching fails immediately. Otherwise, matching will succeed if all items matched.
Obviously, this matching process depends on the children's order of the given list node. Therefore, this matching rule is just for completeness of design and is not recommended to be used in practice unless the children of list node in XML data have a fixed order.
Here is an example to show the matching process.
<list><hoge>123</hoge><hoge>{{regex:^[A-Z]\d{5}$}}</hoge><hoge>{{ignore}}</hoge></list><list><hoge>A12345</hoge><hoge>here can be any string</hoge><hoge>123</hoge></list><list><hoge>123</hoge><hoge>{{regex:^[A-Z]\d{5}$}}</hoge><hoge>{{ignore}}</hoge></list><list><hoge>A12345</hoge><hoge>here can be any string</hoge><hoge>123</hoge></list><list><hoge>123</hoge><hoge>{{regex:^[A-Z]\d{5}$}}</hoge><hoge>{{ignore}}</hoge></list><list><hoge>A12345</hoge><hoge>here can be any string</hoge><hoge>123</hoge></list>Finally, items on both sides are consumed, and the matching of this list is succeeded.
If we change the children's order of
<list>as below, the matching will fail.<list><hoge>{{ignore}}</hoge><hoge>123</hoge><hoge>{{regex:^[A-Z]\d{5}$}}</hoge></list><list><hoge>A12345</hoge><hoge>here can be any string</hoge><hoge>123</hoge></list><list><hoge>{{ignore}}</hoge><hoge>123</hoge><hoge>{{regex:^[A-Z]\d{5}$}}</hoge></list><list><hoge>A12345</hoge><hoge>here can be any string</hoge><hoge>123</hoge></list><list><hoge>{{ignore}}</hoge><hoge>123</hoge><hoge>{{regex:^[A-Z]\d{5}$}}</hoge></list><list><hoge>A12345</hoge><hoge>here can be any string</hoge><hoge>123</hoge></list>In the last round, there is no item found to match the given pattern (matcher expression)
<hoge>{{regex:^[A-Z]\d{5}$}}</hoge>, which means the matching result of this list is failed.Example
Test (How to test it)
For
xml_partial_match.go, more than one hundred unit test cases are added inxml_partial_match_test.go. Those test cases cover all input patterns for each function as possible.In file
ft_simulation_matching_test.go, we add test cases to make sure the matcher working properly in a simulation file.