Added execute support for text based responses

without this change it's impossible to execute any custom method to assert a response for a body that is just text
with this change you can do it

fixes #150
This commit is contained in:
Marcin Grzejszczak
2016-11-17 13:15:25 +01:00
parent d683d3d6f0
commit df80cc8f1f
7 changed files with 73 additions and 1 deletions

View File

@@ -71,6 +71,11 @@ class JUnitMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
return ""
}
@Override
protected String getResponseBodyPropertyComparisonString(String property, ExecutionProperty value) {
return ""
}
@Override
protected void processBodyElement(BlockBuilder blockBuilder, String property, ExecutionProperty exec) {
blockBuilder.addLine("${exec.insertValue("parsedJson.read('\\\$$property')")}")

View File

@@ -81,6 +81,11 @@ abstract class JUnitMethodBodyBuilder extends RequestProcessingMethodBodyBuilder
return "assertThat(responseBody${property}).${buildEscapedMatchesMethod(value)}"
}
@Override
protected String getResponseBodyPropertyComparisonString(String property, ExecutionProperty value) {
return value.insertValue("responseBody${property}")
}
@Override
protected void processBodyElement(BlockBuilder blockBuilder, String property, ExecutionProperty exec) {
blockBuilder.addLine("${exec.insertValue("parsedJson.read(\"\\\$$property\")")};")

View File

@@ -91,6 +91,12 @@ abstract class MethodBodyBuilder {
*/
protected abstract String getResponseBodyPropertyComparisonString(String property, Pattern value)
/**
* Builds the code that for the given {@code property} will match it to
* the given {@link ExecutionProperty} value
*/
protected abstract String getResponseBodyPropertyComparisonString(String property, ExecutionProperty value)
/**
* Appends to the {@link BlockBuilder} the assertion for the given body element
*/

View File

@@ -53,7 +53,12 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
@Override
protected String getResponseBodyPropertyComparisonString(String property, Pattern value) {
return null
return ""
}
@Override
protected String getResponseBodyPropertyComparisonString(String property, ExecutionProperty value) {
return ""
}
@Override

View File

@@ -60,6 +60,11 @@ abstract class SpockMethodRequestProcessingBodyBuilder extends RequestProcessing
return "responseBody$property ${patternComparison(value)}"
}
@Override
protected String getResponseBodyPropertyComparisonString(String property, ExecutionProperty value) {
return value.insertValue("responseBody${property}")
}
@Override
protected void processBodyElement(BlockBuilder blockBuilder, String property, ExecutionProperty exec) {
blockBuilder.addLine("${exec.insertValue("parsedJson.read('\\\$$property')")}")

View File

@@ -766,6 +766,29 @@ class JaxRsClientMethodBuilderSpec extends Specification implements WireMockStub
test.contains("assertThat(responseBody).matches(\".*\");")
}
@Issue('#150')
def "should support custom method execution in response"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
url '/get'
}
response {
status 200
status 200
body(value(stub("HELLO FROM STUB"), server(execute('foo($it)'))))
}
}
MethodBodyBuilder builder = new JaxRsClientJUnitMethodBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.then(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains("foo(responseBody);")
}
private String stripped(String string) {
return string.stripMargin().stripIndent().replace('\t', '').replace('\n', '').replace(' ','')
}

View File

@@ -1645,4 +1645,27 @@ World.'''"""
test.contains("responseBody ==~ java.util.regex.Pattern.compile('.*')")
}
@Issue('#150')
def "should support custom method execution in response"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
url '/get'
}
response {
status 200
status 200
body(value(stub("HELLO FROM STUB"), server(execute('foo($it)'))))
}
}
MethodBodyBuilder builder = new MockMvcSpockMethodRequestProcessingBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.then(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains("foo(responseBody)")
}
}