Fixed the issue that a null value wasn't considered an optional property

without this change an optional property was such that had an empty or non empty string. Null wasn't supported
with this change we're adding null back

fixes gh-1257
This commit is contained in:
Marcin Grzejszczak
2019-11-06 12:29:39 +01:00
parent ddc714b91b
commit de372ed224
3 changed files with 74 additions and 2 deletions

View File

@@ -233,7 +233,7 @@ class Common {
}
void assertThatSidesMatch(OptionalProperty stubSide, Object testSide) {
assert testSide ==~ Pattern.compile(stubSide.optionalPattern())
assert testSide == null || testSide ==~ Pattern.compile(stubSide.optionalPattern())
}
void assertThatSidesMatch(Pattern pattern, String value) {
@@ -245,7 +245,7 @@ class Common {
}
void assertThatSidesMatch(MatchingStrategy firstSide, MatchingStrategy secondSide) {
if (firstSide.type == MatchingStrategy.Type.ABSENT && secondSide != MatchingStrategy.Type.ABSENT) {
if (firstSide.type == MatchingStrategy.Type.ABSENT && secondSide.type != MatchingStrategy.Type.ABSENT) {
throwAbsentError()
}
}

View File

@@ -398,4 +398,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

@@ -2713,6 +2713,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())
}
@@ -2732,6 +2773,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"))