Skip to content

Commit

Permalink
fix: Message matching rules can be defined under content instead of b…
Browse files Browse the repository at this point in the history
…ody #1509
  • Loading branch information
rholshausen committed Apr 22, 2024
1 parent fc5c5fa commit 891605d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Expand Up @@ -238,4 +238,9 @@ data class MatchingRuleCategory @JvmOverloads constructor(
it.key.replace(prefix, newRoot)
}.toMutableMap())
}

/**
* If this MatchingRuleCategory is not empty, return it, otherwise, return the other set of rules
*/
fun orElse(otherRules: MatchingRuleCategory): MatchingRuleCategory = if (isEmpty()) otherRules else this
}
Expand Up @@ -113,4 +113,29 @@ class MatchingRuleCategorySpec extends Specification {
category.matchingRules[''].rules == [ new RegexMatcher('/api/test/\\d{1,8}', null) ]
category.matchingRules[''].ruleLogic == RuleLogic.OR
}

@Issue('#1509')
def 'orElse can default to another rule set if empty'() {
given:
def categoryA = new MatchingRuleCategory('A')
def categoryB = new MatchingRuleCategory('B', [
a: new MatchingRuleGroup(),
b: new MatchingRuleGroup()
])
def categoryC = new MatchingRuleCategory('C', [
a: new MatchingRuleGroup([ TypeMatcher.INSTANCE ]),
b: new MatchingRuleGroup()
])
def categoryD = new MatchingRuleCategory('D', [
a: new MatchingRuleGroup([ TypeMatcher.INSTANCE ]),
b: new MatchingRuleGroup([ TypeMatcher.INSTANCE ])
])

expect:
categoryA.orElse(categoryA) == categoryA
categoryA.orElse(categoryB) == categoryB
categoryA.orElse(categoryC) == categoryC
categoryC.orElse(categoryA) == categoryC
categoryC.orElse(categoryD) == categoryC
}
}
Expand Up @@ -241,7 +241,9 @@ class ResponseComparison(
): ComparisonResult {
val (bodyMismatches, metadataMismatches) = when (message) {
is V4Interaction.AsynchronousMessage -> {
val bodyContext = MatchingContext(message.contents.matchingRules.rulesForCategory("body"),
val bodyContext = MatchingContext(
message.contents.matchingRules.rulesForCategory("content")
.orElse(message.contents.matchingRules.rulesForCategory("body")),
true, pluginConfiguration)
val metadataContext = MatchingContext(message.contents.matchingRules.rulesForCategory("metadata"),
true, pluginConfiguration)
Expand All @@ -258,7 +260,8 @@ class ResponseComparison(
}

is Message -> {
val bodyContext = MatchingContext(message.matchingRules.rulesForCategory("body"),
val bodyContext = MatchingContext(message.matchingRules.rulesForCategory("content")
.orElse(message.matchingRules.rulesForCategory("body")),
true, pluginConfiguration)
val metadataContext = MatchingContext(message.matchingRules.rulesForCategory("metadata"),
true, pluginConfiguration)
Expand All @@ -282,7 +285,7 @@ class ResponseComparison(

override fun compareSynchronousMessage(
interaction: V4Interaction.SynchronousMessages,
actual: OptionalBody,
body: OptionalBody,
messageMetadata: Map<String, Any>?,
pluginConfiguration: Map<String, PluginConfiguration>
): ComparisonResult {
Expand All @@ -296,15 +299,15 @@ class ResponseComparison(
true, pluginConfiguration)
val metadataContext = MatchingContext(messageContents.matchingRules.rulesForCategory("metadata"),
true, pluginConfiguration)
val bodyMismatches = compareMessageBody(interaction, actual, bodyContext)
val bodyMismatches = compareMessageBody(interaction, body, bodyContext)
val metadataMismatches = when (messageMetadata) {
null -> emptyList()
else -> Matching.compareMessageMetadata(messageContents.metadata, messageMetadata, metadataContext)
}
val messageContentType = messageContents.getContentType().or(ContentType.TEXT_PLAIN)
val responseComparison = ResponseComparison(
mapOf("Content-Type" to listOf(messageContentType.toString())), messageContents.contents,
messageContentType.isJson(), messageContentType, actual)
messageContentType.isJson(), messageContentType, body)
val bodyResult = responseComparison.bodyResult(bodyMismatches, SystemPropertyResolver)
return ComparisonResult(bodyMismatches = bodyResult,
metadataMismatches = metadataMismatches.groupBy { it.key })
Expand Down

0 comments on commit 891605d

Please sign in to comment.