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 59b4d726ee..b42cadcf5c 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
@@ -34,6 +34,9 @@ class ContentUtils {
* @return JSON structure with replaced client / server side parts
*/
public static Object extractValue(GString bodyAsValue, ContentType contentType, Closure valueProvider) {
+ if (bodyAsValue.isEmpty()){
+ return bodyAsValue
+ }
if (contentType == ContentType.JSON) {
return extractValueForJSON(bodyAsValue, valueProvider)
}
@@ -164,6 +167,9 @@ class ContentUtils {
}
public static boolean isJsonType(GString gstring) {
+ if (gstring.isEmpty()) {
+ return false
+ }
GString stringWithoutValues = new GStringImpl(
gstring.values.collect({
it instanceof String || it instanceof GString ? it.toString() : escapeJson(it.toString())
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 964f03811b..49c1d94981 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
@@ -286,5 +286,24 @@ class SpockMethodBuilderSpec extends Specification {
spockTest.contains('responseBody.property2 == "b"')
}
-
+ def "should generate test for empty body"() {
+ given:
+ GroovyDsl contractDsl = GroovyDsl.make {
+ request {
+ method('POST')
+ url("/ws/payments")
+ body("")
+ }
+ response {
+ status 406
+ }
+ }
+ SpockMethodBodyBuilder builder = new SpockMethodBodyBuilder(contractDsl)
+ BlockBuilder blockBuilder = new BlockBuilder(" ")
+ when:
+ builder.appendTo(blockBuilder)
+ def spockTest = blockBuilder.toString()
+ then:
+ spockTest.contains(".body('')")
+ }
}
diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WireMockGroovyDslSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WireMockGroovyDslSpec.groovy
index e7e6886b5d..12aca1f271 100755
--- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WireMockGroovyDslSpec.groovy
+++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/WireMockGroovyDslSpec.groovy
@@ -111,7 +111,11 @@ class WireMockGroovyDslSpec extends WireMockSpec {
headers {
header("Content-Type": 'application/x-www-form-urlencoded')
}
- body("""paymentType=INCOMING&transferType=BANK&amount=${value(client(regex('[0-9]{3}\\.[0-9]{2}')), server(500.00))}&bookingDate=${value(client(regex('[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])')), server('2015-05-18'))}""")
+ body("""paymentType=INCOMING&transferType=BANK&amount=${
+ value(client(regex('[0-9]{3}\\.[0-9]{2}')), server(500.00))
+ }&bookingDate=${
+ value(client(regex('[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])')), server('2015-05-18'))
+ }""")
}
response {
status 204
@@ -290,7 +294,7 @@ class WireMockGroovyDslSpec extends WireMockSpec {
}
''')
and:
- stubMappingIsValidWireMockStub(wireMockStub)
+ stubMappingIsValidWireMockStub(wireMockStub)
}
def 'should use equalToJson when content type ends with json'() {
@@ -349,7 +353,9 @@ class WireMockGroovyDslSpec extends WireMockSpec {
headers {
header "Content-Type", "customtype/xml"
}
- body """${value(client('Jozo'), server('Denis'))}${value(client(""), server('1234567890'))}"""
+ body """${value(client('Jozo'), server('Denis'))}${
+ value(client(""), server('1234567890'))
+ }"""
}
response {
status 200
@@ -389,7 +395,9 @@ class WireMockGroovyDslSpec extends WireMockSpec {
request {
method 'GET'
url "/users"
- body """${value(client('Jozo'), server('Denis'))}${value(client(""), server('1234567890'))}"""
+ body """${value(client('Jozo'), server('Denis'))}${
+ value(client(""), server('1234567890'))
+ }"""
}
response {
status 200
@@ -427,7 +435,9 @@ class WireMockGroovyDslSpec extends WireMockSpec {
}
response {
status 200
- body """${value(client('Jozo'), server('Denis'))}${value(client(""), server('1234567890'))}"""
+ body """${value(client('Jozo'), server('Denis'))}${
+ value(client(""), server('1234567890'))
+ }"""
}
}
when:
@@ -490,7 +500,9 @@ class WireMockGroovyDslSpec extends WireMockSpec {
request {
method 'GET'
url "/users"
- body equalToXml("""${value(client('Jozo'), server('Denis'))}${value(client(""), server('1234567890'))}""")
+ body equalToXml("""${value(client('Jozo'), server('Denis'))}${
+ value(client(""), server('1234567890'))
+ }""")
}
response {
status 200
@@ -522,28 +534,28 @@ class WireMockGroovyDslSpec extends WireMockSpec {
def 'should convert groovy dsl stub with regexp Body as String to wireMock stub for the client side'() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
- request {
- method('GET')
- url $(client(regex('/[0-9]{2}')), server('/12'))
- body """
+ request {
+ method('GET')
+ url $(client(regex('/[0-9]{2}')), server('/12'))
+ body """
{
"personalId": "${value(client(regex('^[0-9]{11}$')), server('57593728525'))}"
}
"""
- }
- response {
- status 200
- body("""\
+ }
+ response {
+ status 200
+ body("""\
{
"name": "Jan"
}
"""
- )
- headers {
- header 'Content-Type': 'text/plain'
+ )
+ headers {
+ header 'Content-Type': 'text/plain'
+ }
}
}
- }
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
then:
@@ -827,43 +839,43 @@ class WireMockGroovyDslSpec extends WireMockSpec {
def "should not allow regexp in query parameter for server value"() {
when:
- GroovyDsl.make {
- request {
- method 'GET'
- url("abc") {
- queryParameters {
- parameter 'age': $(client(notMatching("^\\w*\$")), server(regex(".*")))
- }
- }
- }
- response {
- status 200
- }
- }
+ GroovyDsl.make {
+ request {
+ method 'GET'
+ url("abc") {
+ queryParameters {
+ parameter 'age': $(client(notMatching("^\\w*\$")), server(regex(".*")))
+ }
+ }
+ }
+ response {
+ status 200
+ }
+ }
then:
- def e = thrown(IllegalStateException)
- e.message.contains "Query parameter 'age' can't be a pattern for the server side"
+ def e = thrown(IllegalStateException)
+ e.message.contains "Query parameter 'age' can't be a pattern for the server side"
}
def "should not allow query parameter unresolvable for a server value"() {
when:
- GroovyDsl.make {
- request {
- method 'GET'
- urlPath("users") {
- queryParameters {
- parameter 'age': notMatching("^\\w*\$")
- parameter 'name': matching("Denis.*")
- }
- }
- }
- response {
- status 200
- }
- }
+ GroovyDsl.make {
+ request {
+ method 'GET'
+ urlPath("users") {
+ queryParameters {
+ parameter 'age': notMatching("^\\w*\$")
+ parameter 'name': matching("Denis.*")
+ }
+ }
+ }
+ response {
+ status 200
+ }
+ }
then:
- def e = thrown(IllegalStateException)
- e.message.contains "Query parameter 'age' can't be of a matching type: NOT_MATCHING for the server side"
+ def e = thrown(IllegalStateException)
+ e.message.contains "Query parameter 'age' can't be of a matching type: NOT_MATCHING for the server side"
}
def "should not allow query parameter with a different absent variation for server/client"() {
@@ -874,45 +886,45 @@ class WireMockGroovyDslSpec extends WireMockSpec {
e.message.contains "Absent cannot only be used only on one side"
where:
dsl << [
- {
- request {
- method 'GET'
- urlPath("users") {
- queryParameters {
- parameter 'name': $(client(absent()), server(""))
+ {
+ request {
+ method 'GET'
+ urlPath("users") {
+ queryParameters {
+ parameter 'name': $(client(absent()), server(""))
+ }
}
}
- }
- response {
- status 200
- }
- },
- {
- request {
- method 'GET'
- urlPath("users") {
- queryParameters {
- parameter 'name': $(client(""), server(absent()))
+ response {
+ status 200
+ }
+ },
+ {
+ request {
+ method 'GET'
+ urlPath("users") {
+ queryParameters {
+ parameter 'name': $(client(""), server(absent()))
+ }
}
}
- }
- response {
- status 200
- }
- },
- {
- request {
- method 'GET'
- urlPath("users") {
- queryParameters {
- parameter 'name': $(client(absent()), server(matching("abc")))
+ response {
+ status 200
+ }
+ },
+ {
+ request {
+ method 'GET'
+ urlPath("users") {
+ queryParameters {
+ parameter 'name': $(client(absent()), server(matching("abc")))
+ }
}
}
+ response {
+ status 200
+ }
}
- response {
- status 200
- }
- }
]
}
@@ -1107,6 +1119,39 @@ class WireMockGroovyDslSpec extends WireMockSpec {
''')
}
+ def "should generate stub for empty body"() {
+ given:
+ GroovyDsl groovyDsl = GroovyDsl.make {
+ request {
+ method('POST')
+ url("test")
+ body("")
+ }
+ response {
+ status 406
+ }
+ }
+ when:
+ def json = toWireMockClientJsonStub(groovyDsl)
+ then:
+ parseJson(json) == parseJson('''
+ {
+ "request": {
+ "method": "POST",
+ "url": "test",
+ "bodyPatterns": [
+ {
+ "equalTo": ""
+ }
+ ]
+ },
+ "response": {
+ "status": 406
+ }
+ }
+''')
+ }
+
String toJsonString(value) {
new JsonBuilder(value).toPrettyString()
}