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

Add MustEqualToJson param matcher #31

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func TestStubRule_ToJson(t *testing.T) {
WillSetStateTo("Stopped"),
ExpectedFileName: "expected-template-scenario.json",
},
{
Name: "MustEqualToJson",
StubRule: NewStubRule("PATCH", URLMatching("/example")).
WithBodyPattern(MustEqualToJson(map[string]interface{}{"meta": "information"}, IgnoreArrayOrder, IgnoreExtraElements)),
ExpectedFileName: "must-equal-to-json.json",
},
{
Name: "StubRuleWithBearerToken_StartsWithMatcher",
StubRule: Post(URLPathEqualTo("/example")).
Expand Down
14 changes: 14 additions & 0 deletions string_value_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ func EqualToJson(param string, equalJsonFlags ...EqualFlag) BasicParamMatcher {
return NewStringValueMatcher(ParamEqualToJson, param, flags...)
}

// MustEqualToJson returns a matcher that matches when the parameter is equal to the specified JSON.
// This method panics if param cannot be marshaled to JSON.
func MustEqualToJson(param any, equalJsonFlags ...EqualFlag) BasicParamMatcher {
if str, ok := param.(string); ok {
return EqualToJson(str, equalJsonFlags...)
}

if jsonParam, err := json.Marshal(param); err != nil {
panic(fmt.Sprintf("Unable to marshal parameter to JSON: %v", err))
} else {
return EqualToJson(string(jsonParam), equalJsonFlags...)
}
}

// MatchingXPath returns a matcher that matches when the parameter matches the specified XPath.
func MatchingXPath(param string) BasicParamMatcher {
return NewStringValueMatcher(ParamMatchesXPath, param)
Expand Down
4 changes: 2 additions & 2 deletions stub_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {
func (s *StubRule) WithAuthToken(tokenMatcher BasicParamMatcher) *StubRule {
methodPrefix := "Token "
m := addAuthMethodToMatcher(tokenMatcher, methodPrefix)
s.WithHeader(authorizationHeader, HasExactly(StartsWith(methodPrefix), m))
s.WithHeader(authorizationHeader, StartsWith(methodPrefix).And(m))
return s
}

Expand All @@ -113,7 +113,7 @@ func (s *StubRule) WithBearerToken(tokenMatcher BasicParamMatcher) *StubRule {
func (s *StubRule) WithDigestAuth(matcher BasicParamMatcher) *StubRule {
methodPrefix := "Digest "
m := addAuthMethodToMatcher(matcher, methodPrefix)
s.WithHeader(authorizationHeader, HasExactly(StartsWith(methodPrefix), m))
s.WithHeader(authorizationHeader, StartsWith(methodPrefix).And(m))
return s
}

Expand Down
18 changes: 18 additions & 0 deletions testdata/must-equal-to-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"uuid": "%s",
"id": "%s",
"request": {
"method": "PATCH",
"urlPattern": "/example",
"bodyPatterns": [
{
"equalToJson": "{\"meta\":\"information\"}",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
}
]
},
"response": {
"status": 200
}
}
Loading