Merge branch '2.1.x'

This commit is contained in:
Marcin Grzejszczak
2019-11-06 12:38:51 +01:00
3 changed files with 75 additions and 0 deletions

View File

@@ -275,6 +275,9 @@ public class Common {
public void assertThatSidesMatch(Object firstSide, Object secondSide) {
if (firstSide instanceof OptionalProperty) {
if (secondSide == null) {
return;
}
assertThat(
secondSide.toString()
.matches(((OptionalProperty) firstSide).optionalPattern()),

View File

@@ -421,4 +421,28 @@ then:
assertThat(contract.request.bodyMatchers.hasMatchers()).isTrue()
assertThat(contract.response.bodyMatchers.hasMatchers()).isTrue()
}
def 'should work with optional and null value of a field'() {
given:
def contract = Contract.make {
description("Creating user")
name("Create user")
request {
method 'POST'
url '/api/user'
body(
address: $(consumer(optional(regex(alphaNumeric()))), producer(null)),
name: $(consumer(optional(regex(alphaNumeric()))), producer(''))
)
headers {
contentType(applicationJson())
}
}
response {
status 201
}
}
expect:
contract != null
}
}

View File

@@ -2862,6 +2862,47 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
@Issue("#1257")
def "should work with null request element on the client side and optional stub entry"() {
given:
Contract contractDsl = Contract.make {
description("Creating user")
name("Create user")
request {
method 'POST'
url '/api/user'
body(
address: $(consumer(optional(regex(alphaNumeric()))), producer(null)),
name: $(consumer(optional(regex(alphaNumeric()))), producer(''))
)
headers {
contentType(applicationJson())
}
}
response {
status 201
}
}
and:
String wireMockStub = new WireMockStubStrategy("Test",
new ContractMetadata(null, false, 0, null, contractDsl), contractDsl)
.toWireMockClientStub()
and:
int port = SocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
and:
stubMappingIsValidWireMockStub(wireMockStub)
server.addStubMapping(WireMockStubMapping.buildFrom(wireMockStub))
when:
ResponseEntity<String> entity = callWithOptionalAndEmpty(port)
then:
entity.statusCodeValue == 201
cleanup:
server?.shutdown()
}
WireMockConfiguration config() {
return new WireMockConfiguration().extensions(responseTemplateTransformer())
}
@@ -2882,6 +2923,13 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
.body("{\"foo\":\"bar\",\"baz\":5}"), String.class)
}
ResponseEntity<String> callWithOptionalAndEmpty(int port) {
return new TestRestTemplate().exchange(
RequestEntity.post(URI.create("http://localhost:" + port + "/api/user"))
.header("Content-Type", "application/json")
.body("{\"foo\":null,\"name\":\"\"}"), String.class)
}
ResponseEntity<byte[]> callBytes(int port, File request) {
return new TestRestTemplate().exchange(
RequestEntity.put(URI.create("http://localhost:" + port + "/1"))