From 69e5020e3514dee6ee3a32ba79f5e20d8894c1f2 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 15 Jun 2015 16:45:52 +0200 Subject: [PATCH] [#82] Fixed wrong request creation when having list in a map --- .../builder/SpockMethodBodyBuilder.groovy | 15 ++++++++--- .../accurest/util/ContentUtils.groovy | 13 ++++++++-- .../builder/SpockMethodBuilderSpec.groovy | 26 +++++++++++++++++-- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy index 544abb98b1..d569204b10 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy @@ -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; diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy index 1a187595b9..6cab8d76fd 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/ContentUtils.groovy @@ -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) { diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy index a2d03358a2..d8bfefcf5e 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/SpockMethodBuilderSpec.groovy @@ -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"') } - }