[#82] Fixed wrong request creation when having list in a map

This commit is contained in:
Marcin Grzejszczak
2015-06-15 16:45:52 +02:00
parent 9c3c172ad2
commit 69e5020e35
3 changed files with 46 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ import io.codearte.accurest.dsl.internal.Request
import io.codearte.accurest.dsl.internal.Response
import io.codearte.accurest.dsl.internal.UrlPath
import io.codearte.accurest.util.ContentType
import io.codearte.accurest.util.JsonConverter
import java.util.regex.Pattern
@@ -42,10 +43,7 @@ class SpockMethodBodyBuilder {
addLine(".header('${header.name}', '${header.serverValue}')")
}
if (request.body) {
Object bodyValue = request.body.serverValue
if (bodyValue instanceof GString) {
bodyValue = extractValue(bodyValue, {DslProperty dslProperty -> dslProperty.serverValue})
}
Object bodyValue = extractServerValueFromBody(request.body.serverValue)
String matches = new JsonOutput().toJson(bodyValue)
addLine(".body('$matches')")
}
@@ -97,6 +95,15 @@ class SpockMethodBodyBuilder {
}
}
private Object extractServerValueFromBody(bodyValue) {
if (bodyValue instanceof GString) {
bodyValue = extractValue(bodyValue, { DslProperty dslProperty -> dslProperty.serverValue })
} else {
bodyValue = JsonConverter.transformValues(bodyValue, { it instanceof DslProperty ? it.serverValue : it })
}
return bodyValue
}
private String buildUrl(Request request) {
if (request.url)
return request.url.serverValue;

View File

@@ -2,6 +2,7 @@ package io.codearte.accurest.util
import groovy.json.JsonException
import groovy.json.JsonSlurper
import groovy.transform.TypeChecked
import groovy.util.logging.Slf4j
import io.codearte.accurest.dsl.internal.DslProperty
import io.codearte.accurest.dsl.internal.Headers
import io.codearte.accurest.dsl.internal.MatchingStrategy
@@ -14,6 +15,7 @@ import static org.apache.commons.lang3.StringEscapeUtils.escapeJson
import static org.apache.commons.lang3.StringEscapeUtils.escapeXml11
@TypeChecked
@Slf4j
class ContentUtils {
private static final Pattern TEMPORARY_PATTERN_HOLDER = Pattern.compile('REGEXP>>(.*)<<')
@@ -40,12 +42,19 @@ class ContentUtils {
}
// else Brute force :(
try {
log.debug("No content type provided so trying to parse as JSON")
return extractValueForJSON(bodyAsValue, valueProvider)
} catch(JsonException e) {
// Not a JSON format
return extractValueForXML(bodyAsValue, valueProvider)
log.debug("Failed to parse as JSON - trying to parse as XML", e)
try {
return extractValueForXML(bodyAsValue, valueProvider)
} catch (Exception exception) {
log.debug("No content type provided and failed to parse as XML - returning the value back to the user", exception)
return bodyAsValue
}
}
return bodyAsValue
}
public static Object extractValue(GString bodyAsValue, Closure valueProvider) {

View File

@@ -34,7 +34,7 @@ class SpockMethodBuilderSpec extends Specification {
}
@Issue("#79")
def "should generate assertions for simple response body constructed from map"() {
def "should generate assertions for simple response body constructed from map with a list"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
@@ -62,6 +62,29 @@ class SpockMethodBuilderSpec extends Specification {
blockBuilder.toString().contains("responseBody.property2[1].b == \"sthElse\"")
}
@Issue("#82")
def "should generate proper request when body constructed from map with a list"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
request {
method "GET"
url "test"
body (
items: ['HOP']
)
}
response {
status 200
}
}
SpockMethodBodyBuilder builder = new SpockMethodBodyBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
then:
blockBuilder.toString().contains(".body('{\"items\":[\"HOP\"]}')")
}
def "should generate assertions for array in response body"() {
given:
GroovyDsl contractDsl = GroovyDsl.make {
@@ -236,5 +259,4 @@ class SpockMethodBuilderSpec extends Specification {
spockTest.contains('responseBody.property1 == "a"')
spockTest.contains('responseBody.property2 == "b"')
}
}