Skip to content

Commit

Permalink
Add MustEqualToJson param matcher (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerus authored Sep 24, 2024
1 parent 5e1a33c commit 21343b1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
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
}
}

0 comments on commit 21343b1

Please sign in to comment.