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 d569204b10..4da97ee58b 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 @@ -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) 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 d8bfefcf5e..e9301d0c70 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 @@ -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"') } + + }