Allow MethodBodyBuilder to evaluate ExecutionProperty objects (#114)

This commit is contained in:
Andrew Fitzgerald
2016-10-18 02:20:17 -04:00
committed by Marcin Grzejszczak
parent 8d8cdfe89b
commit 74ea3fd27d
9 changed files with 52 additions and 8 deletions

View File

@@ -42,4 +42,9 @@ class ExecutionProperty {
String insertValue(String valueToInsert) {
return executionCommand.replaceAll(PLACEHOLDER_VALUE, valueToInsert)
}
@Override
public String toString() {
return executionCommand;
}
}

View File

@@ -153,7 +153,7 @@ class JUnitMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
@Override
protected String getHeaderString(Header header) {
return ".header(\"${getTestSideValue(header.name)}\", \"${getTestSideValue(header.serverValue)}\")"
return ".header(${getTestSideValue(header.name)}, ${getTestSideValue(header.serverValue)})"
}
@Override

View File

@@ -125,7 +125,7 @@ abstract class JUnitMethodBodyBuilder extends RequestProcessingMethodBodyBuilder
@Override
protected String getHeaderString(Header header) {
return ".header(\"${getTestSideValue(header.name)}\", \"${getTestSideValue(header.serverValue)}\")"
return ".header(${getTestSideValue(header.name)}, ${getTestSideValue(header.serverValue)})"
}
@Override

View File

@@ -359,9 +359,18 @@ abstract class MethodBodyBuilder {
* combines them into a String representation
*/
protected String getTestSideValue(Object object) {
return MapConverter.getTestSideValues(object).toString()
return '"' + MapConverter.getTestSideValues(object).toString() + '"'
}
/**
* Extracts the executable test side values and
* returns the code of the executable
*/
protected String getTestSideValue(ExecutionProperty executionProperty) {
return executionProperty.toString()
}
/**
* Appends to the {@link BlockBuilder} the assertion for the given body element
*/

View File

@@ -109,7 +109,7 @@ abstract class RequestProcessingMethodBodyBuilder extends MethodBodyBuilder {
String url = buildUrl(request)
String method = request.method.serverValue.toString().toLowerCase()
bb.addLine(/.${method}("$url")/)
bb.addLine(/.${method}($url)/)
addColonIfRequired(bb)
bb.unindent()
}

View File

@@ -150,7 +150,7 @@ class SpockMessagingMethodBodyBuilder extends MessagingMethodBodyBuilder {
@Override
protected String getHeaderString(Header header) {
return "'${getTestSideValue(header.name)}': '${getTestSideValue(header.serverValue)}'"
return "${getTestSideValue(header.name)}: ${getTestSideValue(header.serverValue)}"
}
@Override

View File

@@ -106,7 +106,7 @@ abstract class SpockMethodRequestProcessingBodyBuilder extends RequestProcessing
@Override
protected String getHeaderString(Header header) {
return ".header('${getTestSideValue(header.name)}', '${getTestSideValue(header.serverValue)}')"
return ".header(${getTestSideValue(header.name)}, ${getTestSideValue(header.serverValue)})"
}
@Override

View File

@@ -268,7 +268,7 @@ class ContractHttpDocsSpec extends Specification {
"""
given:
def request = given()
.header('Content-Type', 'application/json')
.header("Content-Type", "application/json")
.body('''{"email":"abc@abc.com","callback_url":"http://partners.com"}''')
when:

View File

@@ -1557,7 +1557,7 @@ World.'''"""
def test = blockBuilder.toString()
def strippedTest = test.replace('\n', '').stripIndent().stripMargin()
then:
strippedTest.matches(""".*header\\('header', 'application\\/vnd\\.fraud\\.v1\\+json;.*'\\).*""")
strippedTest.matches(""".*header\\("header", "application\\/vnd\\.fraud\\.v1\\+json;.*"\\).*""")
strippedTest.matches(""".*body\\('''\\{"requestElement":"[0-9]{5}"\\}'''\\).*""")
strippedTest.matches(""".*put\\("/foo/[0-9]{5}"\\).*""")
strippedTest.contains("""response.header('Content-Type') ==~ java.util.regex.Pattern.compile('application/vnd.fraud.v1.json.*')""")
@@ -1592,4 +1592,34 @@ World.'''"""
test.contains('assertThatRejectionReasonIsNull(parsedJson.read(\'$.rejectionReason.title\'))')
}
@Issue('#111')
def "should execute custom method for request headers"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
urlPath '/get'
headers {
header('authorization', value(consumer('Bearer token'), producer(execute('getOAuthTokenHeader()'))))
}
}
response {
status 200
body([
fraudCheckStatus: "OK",
rejectionReason : [
title: $(consumer(null), producer(execute('assertThatRejectionReasonIsNull($it)')))
]
])
}
}
MethodBodyBuilder builder = new MockMvcSpockMethodRequestProcessingBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.given(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('.header("authorization", getOAuthTokenHeader())')
}
}