diff --git a/client_test.go b/client_test.go index 8e36fcf..2118c95 100644 --- a/client_test.go +++ b/client_test.go @@ -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")). diff --git a/string_value_matcher.go b/string_value_matcher.go index f170016..105c4a7 100644 --- a/string_value_matcher.go +++ b/string_value_matcher.go @@ -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) diff --git a/stub_rule.go b/stub_rule.go index 4491416..90960a8 100644 --- a/stub_rule.go +++ b/stub_rule.go @@ -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 } @@ -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 } diff --git a/testdata/must-equal-to-json.json b/testdata/must-equal-to-json.json new file mode 100644 index 0000000..8cf34ae --- /dev/null +++ b/testdata/must-equal-to-json.json @@ -0,0 +1,18 @@ +{ + "uuid": "%s", + "id": "%s", + "request": { + "method": "PATCH", + "urlPattern": "/example", + "bodyPatterns": [ + { + "equalToJson": "{\"meta\":\"information\"}", + "ignoreArrayOrder" : true, + "ignoreExtraElements" : true + } + ] + }, + "response": { + "status": 200 + } +} \ No newline at end of file