Merge pull request #89 from Codearte/issue/88-additional-quotes-added-in-test-when-body-is-GString

[Issue#88] Additional quotes added in test when body is String
This commit is contained in:
Olga Maciaszek-Sharma
2015-06-18 13:48:26 +02:00
2 changed files with 40 additions and 8 deletions

View File

@@ -17,8 +17,8 @@ import io.codearte.accurest.util.JsonConverter
import java.util.regex.Pattern
import static io.codearte.accurest.util.ContentUtils.extractValue
import static io.codearte.accurest.util.ContentUtils.recognizeContentTypeFromHeader
import static io.codearte.accurest.util.ContentUtils.recognizeContentTypeFromContent
import static io.codearte.accurest.util.ContentUtils.recognizeContentTypeFromHeader
/**
* @author Jakub Kubrynski
@@ -44,7 +44,7 @@ class SpockMethodBodyBuilder {
}
if (request.body) {
Object bodyValue = extractServerValueFromBody(request.body.serverValue)
String matches = new JsonOutput().toJson(bodyValue)
String matches = trimRepeatedQuotes(new JsonOutput().toJson(bodyValue))
addLine(".body('$matches')")
}
@@ -95,6 +95,13 @@ class SpockMethodBodyBuilder {
}
}
private String trimRepeatedQuotes(String toTrim) {
if (toTrim.startsWith('"')) {
return toTrim.replaceAll('"', '')
}
return toTrim
}
private Object extractServerValueFromBody(bodyValue) {
if (bodyValue instanceof GString) {
bodyValue = extractValue(bodyValue, { DslProperty dslProperty -> dslProperty.serverValue })
@@ -141,7 +148,7 @@ class SpockMethodBodyBuilder {
}
} else if (value instanceof Map) {
processMapElement(value, blockBuilder, property)
}else if (value instanceof Map.Entry) {
} else if (value instanceof Map.Entry) {
processEntryElement(blockBuilder, property, value)
} else if (value instanceof List) {
processArrayElements(value, property, blockBuilder)

View File

@@ -43,11 +43,11 @@ class SpockMethodBuilderSpec extends Specification {
}
response {
status 200
body (
body(
property1: 'a',
property2: [
[a: 'sth'],
[b: 'sthElse']
[a: 'sth'],
[b: 'sthElse']
]
)
}
@@ -69,7 +69,7 @@ class SpockMethodBuilderSpec extends Specification {
request {
method "GET"
url "test"
body (
body(
items: ['HOP']
)
}
@@ -85,6 +85,29 @@ class SpockMethodBuilderSpec extends Specification {
blockBuilder.toString().contains(".body('{\"items\":[\"HOP\"]}')")
}
@Issue("#88")
def "should generate proper request when body constructed from GString"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "GET"
url "test"
body(
"property1=VAL1"
)
}
response {
status 200
}
}
SpockMethodBodyBuilder builder = new SpockMethodBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
then:
blockBuilder.toString().contains(".body('property1=VAL1')")
}
def "should generate assertions for array in response body"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
@@ -205,7 +228,7 @@ class SpockMethodBuilderSpec extends Specification {
}
response {
status 200
body( """{"property1":"a","property2":"${value(client('123'), server(regex('[0-9]{3}')))}"}""")
body("""{"property1":"a","property2":"${value(client('123'), server(regex('[0-9]{3}')))}"}""")
headers {
header('Content-Type': 'application/json')
@@ -259,4 +282,6 @@ class SpockMethodBuilderSpec extends Specification {
spockTest.contains('responseBody.property1 == "a"')
spockTest.contains('responseBody.property2 == "b"')
}
}