Fix handling of ClientDslProperties when generating wiremock stubs (#1716)

Correctly handle client specific DslProperties (e.g. anyAlphanumeric()) in request headers, cookies, etc.

Co-authored-by: Dominik Meister <dominik.meister@helsana.ch>
This commit is contained in:
Dominik Meister
2021-09-20 10:09:27 +02:00
committed by GitHub
parent 061abb652e
commit 760387617d
2 changed files with 73 additions and 0 deletions

View File

@@ -46,6 +46,7 @@ import org.springframework.cloud.contract.spec.Contract;
import org.springframework.cloud.contract.spec.internal.Body;
import org.springframework.cloud.contract.spec.internal.BodyMatcher;
import org.springframework.cloud.contract.spec.internal.BodyMatchers;
import org.springframework.cloud.contract.spec.internal.ClientDslProperty;
import org.springframework.cloud.contract.spec.internal.DslProperty;
import org.springframework.cloud.contract.spec.internal.FromFileProperty;
import org.springframework.cloud.contract.spec.internal.MatchingStrategy;
@@ -369,6 +370,10 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
}
protected ContentPattern<?> convertToValuePattern(Object object) {
if (object instanceof ClientDslProperty) {
object = ((ClientDslProperty) object).getClientValue();
}
if (object instanceof Pattern || object instanceof RegexProperty) {
return WireMock.matching(new RegexProperty(object).pattern());
}

View File

@@ -3062,6 +3062,74 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
server?.shutdown()
}
def "should work with client DSL properties"() {
given:
org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.
make {
request {
method('GET')
url("/api/foo")
headers {
header("foo", $(client(anyAlphaNumeric()), server("123")))
}
cookies {
cookie("cookie1", $(client("foo"), server("bar")))
cookie("cookie2", $(client(~/[a-z]+/), server("bar")))
cookie("cookie3", $(client(anyAlphaNumeric()), server("bar")))
cookie("cookie4", $(anyAlphaNumeric()))
}
}
response {
status OK()
body("ok")
headers {
header 'Content-Type': 'text/plain'
}
}
}
when:
String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).
toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual('''
{
"request" : {
"url" : "/api/foo",
"method" : "GET",
"headers" : {
"foo" : {
"matches" : "[a-zA-Z0-9]+"
}
},
"cookies" : {
"cookie1" : {
"equalTo" : "foo"
},
"cookie2" : {
"matches" : "[a-z]+"
},
"cookie3" : {
"matches" : "[a-zA-Z0-9]+"
},
"cookie4" : {
"matches" : "[a-zA-Z0-9]+"
}
}
},
"response" : {
"status" : 200,
"body" : "ok",
"headers" : {
"Content-Type" : "text/plain"
},
"transformers" : [ "response-template", "foo-transformer" ]
}
}
''', wireMockStub)
and:
stubMappingIsValidWireMockStub(wireMockStub)
}
WireMockConfiguration config() {
return new WireMockConfiguration().extensions(responseTemplateTransformer())
}