-
Notifications
You must be signed in to change notification settings - Fork 228
add XML templated matcher #939
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
Merged
tommysitu
merged 4 commits into
SpectoLabs:master
from
openstandia:release/xml-partial-matcher
Jun 10, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package matchers | ||
|
|
||
| import ( | ||
| "regexp" | ||
|
|
||
| "github.com/beevik/etree" | ||
| ) | ||
|
|
||
| var XmlTemplated = "xmltemplated" | ||
|
|
||
| var ignoreExpr = regexp.MustCompile("^\\s*\\{\\{\\s*ignore\\s*\\}\\}\\s*$") | ||
| var regExpr = regexp.MustCompile("^\\s*\\{\\{\\s*regex:(.*)\\}\\}\\s*$") | ||
|
|
||
| func XmlTemplatedMatch(match interface{}, toMatch string) bool { | ||
| matchString, ok := match.(string) | ||
| if !ok { | ||
| return false | ||
| } | ||
|
|
||
| // parse xml in mock data into dom tree | ||
| expected := etree.NewDocument() | ||
| if err := expected.ReadFromString(matchString); err != nil { | ||
| return false | ||
| } | ||
|
|
||
| // parse xml in actual request body into dom tree | ||
| actual := etree.NewDocument() | ||
| if err := actual.ReadFromString(toMatch); err != nil { | ||
| return false | ||
| } | ||
|
|
||
| // tree matching | ||
| return compareTree(expected.Root(), actual.Root()) | ||
| } | ||
|
|
||
| func compareTree(expected *etree.Element, actual *etree.Element) bool { | ||
| // compare constructure | ||
| // step 1. compare tag name | ||
| if expected.Tag != actual.Tag { | ||
| return false | ||
| } | ||
| // step 2. compare node content | ||
| // case 1: leaf | ||
| if isLeaf(expected) { | ||
| // compare text content | ||
| return compareValue(expected.Text(), actual.Text()) | ||
| } | ||
| // case 2: children element matching | ||
| actual_children := actual.ChildElements() | ||
| // for each expected tag | ||
|
tommysitu marked this conversation as resolved.
|
||
| for _, match := range expected.ChildElements() { | ||
| // find one in actual | ||
| matched := false | ||
| for i, ele := range actual_children { | ||
| if compareTree(match, ele) { | ||
| matched = true | ||
| // remove matched | ||
| actual_children = append(actual_children[:i], actual_children[i+1:]...) | ||
| break | ||
| } | ||
| } | ||
| // all elements in actual data is not matched | ||
| // or, too many elements in expected data | ||
| if matched == false { | ||
| return false | ||
| } | ||
| } | ||
| // too many elements in actual data | ||
| if len(actual_children) > 0 { | ||
| return false | ||
| } | ||
|
tommysitu marked this conversation as resolved.
|
||
| return true | ||
| } | ||
|
|
||
| // check element text content | ||
| func compareValue(expected string, actual string) bool { | ||
| // pattern 1: ignore value => always be true | ||
| if ignoreExpr.MatchString(expected) { | ||
| return true | ||
| } | ||
| // pattern 2: regex | ||
| // parse node content | ||
| group := regExpr.FindStringSubmatch(expected) | ||
| // if it matchs the grammer like {{regex: ... }} ==> otherwise, take it as plain text | ||
| if len(group) > 1 { | ||
| matcher, err := regexp.Compile(group[1]) | ||
| // can not compile regular expression --> invalid regex --> false | ||
| if err != nil { | ||
| return false | ||
| } | ||
| // use regular expression to match actual value | ||
| return matcher.MatchString(actual) | ||
| } | ||
| // pattern 3: exact equal | ||
| return expected == actual | ||
| } | ||
|
|
||
| // check if an element is leaf | ||
| func isLeaf(expected *etree.Element) bool { | ||
| // it is a leaf node if it does not have any children | ||
| return len(expected.ChildElements()) == 0 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 whenever you have some time.