diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/QueryParameter.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/QueryParameter.groovy index 78d8e34009..042da081f2 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/QueryParameter.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/QueryParameter.groovy @@ -2,7 +2,9 @@ package io.codearte.accurest.dsl.internal; import groovy.transform.CompileStatic; import groovy.transform.EqualsAndHashCode; -import groovy.transform.ToString; +import groovy.transform.ToString + +import static io.codearte.accurest.util.ValidateUtils.validateServerValueIsAvailable; @EqualsAndHashCode(includeFields = true) @ToString(includePackage = false, includeFields = true, ignoreNulls = true, includeNames = true) @@ -13,16 +15,19 @@ class QueryParameter extends DslProperty { QueryParameter(String name, DslProperty dslProperty) { super(dslProperty.clientValue, dslProperty.serverValue) + validateServerValueIsAvailable(dslProperty.serverValue, "Query parameter '$name'") this.name = name } QueryParameter(String name, MatchingStrategy matchingStrategy) { super(matchingStrategy) + validateServerValueIsAvailable(matchingStrategy, "Query parameter '$name'") this.name = name } QueryParameter(String name, Object value) { super(value) + validateServerValueIsAvailable(value, "Query parameter '$name'") this.name = name } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Url.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Url.groovy index 42a26eb26c..09eae6d094 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Url.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Url.groovy @@ -4,6 +4,8 @@ import groovy.transform.CompileStatic import groovy.transform.EqualsAndHashCode import groovy.transform.ToString +import static io.codearte.accurest.util.ValidateUtils.validateServerValueIsAvailable + @ToString(includePackage = false, includeFields = true, includeNames = true) @EqualsAndHashCode(includeFields = true) @CompileStatic @@ -13,10 +15,12 @@ class Url extends DslProperty { Url(DslProperty prop) { super(prop.clientValue, prop.serverValue) + validateServerValueIsAvailable(prop.serverValue, "Url") } Url(Object url) { super(url) + validateServerValueIsAvailable(url, "Url") } void queryParameters(@DelegatesTo(QueryParameters) Closure closure) { diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/ValidateUtils.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/util/ValidateUtils.groovy new file mode 100644 index 0000000000..9c07eaa3e4 --- /dev/null +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/ValidateUtils.groovy @@ -0,0 +1,39 @@ +package io.codearte.accurest.util + +import io.codearte.accurest.dsl.internal.DslProperty +import io.codearte.accurest.dsl.internal.MatchingStrategy + +import java.util.regex.Pattern + +class ValidateUtils { + + static Object validateServerValueIsAvailable(Object value) { + validateServerValueIsAvailable(value, "Server value") + return value + } + + static Object validateServerValueIsAvailable(Object value, String msg) { + validateServerValue(value, msg) + return value + } + + static void validateServerValue(Pattern pattern, String msg) { + throw new IllegalStateException("$msg can't be a pattern") + } + + static void validateServerValue(MatchingStrategy matchingStrategy, String msg) { + if (matchingStrategy.type != MatchingStrategy.Type.EQUAL_TO) { + throw new IllegalStateException("$msg can't be of matching type: $matchingStrategy.type") + } + validateServerValue(matchingStrategy.serverValue, msg) + } + + static void validateServerValue(DslProperty value, String msg) { + validateServerValue(value.serverValue, msg) + } + + static void validateServerValue(Object value, String msg) { + // OK + } + +} 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 de857e0efc..e5e33d9e04 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 @@ -322,13 +322,13 @@ class WiremockGroovyDslSpec extends WiremockSpec { method 'GET' urlPath($(client("users"), server("items"))) { queryParameters { - parameter 'limit': $(client(equalTo("20")), server(containing("10"))) - parameter 'offset': containing("10") + parameter 'limit': $(client(equalTo("20")), server("10")) + parameter 'offset': $(client(containing("10")), server("10")) parameter 'filter': "email" - parameter 'sort': ~/^[0-9]{10}$/ - parameter 'search': $(client(notMatching(~/^\/[0-9]{2}$/)), server(containing("10"))) - parameter 'age': notMatching("^\\w*\$") - parameter 'name': matching("Denis.*") + parameter 'sort': $(client(~/^[0-9]{10}$/), server("1234567890")) + parameter 'search': $(client(notMatching(~/^\/[0-9]{2}$/)), server("10")) + parameter 'age': $(client(notMatching("^\\w*\$")), server(10)) + parameter 'name': $(client(matching("Denis.*")), server("Denis")) } } } @@ -435,9 +435,9 @@ class WiremockGroovyDslSpec extends WiremockSpec { stubMappingIsValidWiremockStub(json) } - def "should generate request with url and queryParameters for client side"() { - given: - GroovyDsl groovyDsl = GroovyDsl.make { + def "should not allow regexp in url for server value"() { + when: + GroovyDsl.make { request { method 'GET' url(regex(/users\/[0-9]*/)) { @@ -451,6 +451,68 @@ class WiremockGroovyDslSpec extends WiremockSpec { status 200 } } + then: + def e = thrown(IllegalStateException) + e.message.contains "Url can't be a pattern" + } + + 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 + } + } + then: + def e = thrown(IllegalStateException) + e.message.contains "Query parameter 'age' can't be a pattern" + } + + 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 + } + } + then: + def e = thrown(IllegalStateException) + e.message.contains "Query parameter 'age' can't be of matching type: NOT_MATCHING" + } + + def "should generate request with url and queryParameters for client side"() { + given: + GroovyDsl groovyDsl = GroovyDsl.make { + request { + method 'GET' + url($(client(regex(/users\/[0-9]*/)), server("users/123"))) { + queryParameters { + parameter 'age': $(client(notMatching("^\\w*\$")), server(10)) + parameter 'name': $(client(matching("Denis.*")), server("Denis")) + } + } + } + response { + status 200 + } + } when: def json = toWiremockClientJsonStub(groovyDsl) then: