Should allow regex in headers message matching. Fixes gh-1382.

This commit is contained in:
Olga Maciaszek-Sharma
2020-04-27 18:27:13 +02:00
parent 802e64ff53
commit eae3f449c3
4 changed files with 60 additions and 6 deletions

View File

@@ -233,8 +233,8 @@ class StubRunnerIntegrationMessageSelector implements MessageSelector {
Object value = it.getClientValue();
Object valueInHeader = headers.get(name);
boolean matches;
if (value instanceof RegexProperty) {
Pattern pattern = ((RegexProperty) value).getPattern();
if (value instanceof RegexProperty || value instanceof Pattern) {
Pattern pattern = new RegexProperty(value).getPattern();
matches = pattern.matcher(valueInHeader.toString()).matches();
}
else {

View File

@@ -234,8 +234,8 @@ class StubRunnerStreamMessageSelector implements MessageSelector {
Object value = it.getClientValue();
Object valueInHeader = headers.get(name);
boolean matches;
if (value instanceof RegexProperty) {
Pattern pattern = ((RegexProperty) value).getPattern();
if (value instanceof RegexProperty || value instanceof Pattern) {
Pattern pattern = new RegexProperty(value).getPattern();
matches = pattern.matcher(valueInHeader.toString()).matches();
}
else {

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.contract.stubrunner.messaging.integration
import spock.lang.Issue
import spock.lang.Specification
import org.springframework.cloud.contract.spec.Contract
@@ -181,7 +182,8 @@ class StubRunnerIntegrationMessageSelectorSpec extends Specification {
}
}
and:
StubRunnerIntegrationMessageSelector predicate = new StubRunnerIntegrationMessageSelector(dsl)
StubRunnerIntegrationMessageSelector predicate = new StubRunnerIntegrationMessageSelector(
dsl)
message.headers >> [
foo: 123
]
@@ -191,4 +193,29 @@ class StubRunnerIntegrationMessageSelectorSpec extends Specification {
expect:
predicate.accept(message)
}
@Issue("1382")
def "should return true if header matches regex"() {
given:
Contract dsl = Contract.make {
input {
messageFrom "foo"
messageHeaders {
header("foo", $(anyUuid()))
}
messageBody(foo: 123)
}
}
and:
StubRunnerIntegrationMessageSelector predicate = new StubRunnerIntegrationMessageSelector(
dsl)
message.headers >> [
foo: "fbcc2ed3-dbac-47e7-9a4b-c2d55709792c"
]
message.payload >> [
foo: 123
]
expect:
predicate.accept(message)
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.contract.stubrunner.messaging.stream
import spock.lang.Issue
import spock.lang.Specification
import org.springframework.cloud.contract.spec.Contract
@@ -210,7 +211,8 @@ class StubRunnerStreamMessageSelectorSpec extends Specification {
}
}
and:
StubRunnerStreamMessageSelector predicate = new StubRunnerStreamMessageSelector(dsl)
StubRunnerStreamMessageSelector predicate = new StubRunnerStreamMessageSelector(
dsl)
message.headers >> [
foo: 123
]
@@ -220,4 +222,29 @@ class StubRunnerStreamMessageSelectorSpec extends Specification {
expect:
predicate.accept(message)
}
@Issue("1382")
def "should return true if header matches regex"() {
given:
Contract dsl = Contract.make {
input {
messageFrom "foo"
messageHeaders {
header("foo", $(anyUuid()))
}
messageBody(foo: 123)
}
}
and:
StubRunnerStreamMessageSelector predicate = new StubRunnerStreamMessageSelector(
dsl)
message.headers >> [
foo: "fbcc2ed3-dbac-47e7-9a4b-c2d55709792c"
]
message.payload >> [
foo: 123
]
expect:
predicate.accept(message)
}
}