Dont parse body if not specified in contract (#624)

This commit is contained in:
Axel Hodler
2018-05-01 15:32:12 +02:00
committed by Marcin Grzejszczak
parent db31e37944
commit 87d6d9e4f1
4 changed files with 41 additions and 2 deletions

View File

@@ -73,7 +73,9 @@ class JaxRsClientJUnitMethodBodyBuilder extends JUnitMethodBodyBuilder {
bb.unindent()
bb.addEmptyLine()
bb.addLine("String responseAsString = response.readEntity(String.class);")
if (expectsResponseBody()) {
bb.addLine("String responseAsString = response.readEntity(String.class);")
}
}
protected void appendUrlPathAndQueryParameters(BlockBuilder bb) {

View File

@@ -69,7 +69,9 @@ class JaxRsClientSpockMethodRequestProcessingBodyBuilder extends SpockMethodRequ
bb.unindent()
bb.addEmptyLine()
bb.addLine("String responseAsString = response.readEntity(String)")
if (expectsResponseBody()) {
bb.addLine("String responseAsString = response.readEntity(String)")
}
}
protected void appendRequestWithRequiredResponseContentType(BlockBuilder bb) {

View File

@@ -73,6 +73,13 @@ abstract class RequestProcessingMethodBodyBuilder extends MethodBodyBuilder {
return true
}
/**
* Returns {@code true} if a response body is expected
*/
protected boolean expectsResponseBody() {
return response.body != null;
}
/**
* Returns {@code true} if the query parameter is allowed
*/

View File

@@ -687,6 +687,34 @@ class JaxRsClientMethodBuilderSpec extends Specification implements WireMockStub
"JaxRsClientJUnitMethodBodyBuilder" | { org.springframework.cloud.contract.spec.Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties) } | 'entity("", "application/octet-stream"'
}
def "should not parse the response body if there is no response body specified in the contract"() {
given:
Contract contractDsl = Contract.make {
request {
method "HEAD"
url "head"
}
response {
status OK()
}
}
MethodBodyBuilder builder = methodBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
!test.contains(bodyParsingString)
and:
stubMappingIsValidWireMockStub(contractDsl)
and:
SyntaxChecker.tryToCompile(methodBuilderName, blockBuilder.toString())
where:
methodBuilderName | methodBuilder | bodyParsingString
"JaxRsClientSpockMethodRequestProcessingBodyBuilder" | { org.springframework.cloud.contract.spec.Contract dsl -> new JaxRsClientSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | "String responseAsString = response.readEntity(String)"
"JaxRsClientJUnitMethodBodyBuilder" | { org.springframework.cloud.contract.spec.Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties) } | "String responseAsString = response.readEntity(String.class);"
}
def "should generate test for String in response body with #methodBodyName"() {
given:
Contract contractDsl = Contract.make {