Merge pull request #170 from Codearte/issues/169-fixed-query-params
[#169] Query params - fixes #169
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package io.codearte.accurest.builder
|
||||
|
||||
import java.util.regex.Pattern
|
||||
import groovy.transform.PackageScope
|
||||
import groovy.transform.TypeChecked
|
||||
import groovy.transform.TypeCheckingMode
|
||||
@@ -7,9 +8,8 @@ import io.codearte.accurest.dsl.GroovyDsl
|
||||
import io.codearte.accurest.dsl.internal.Header
|
||||
import io.codearte.accurest.dsl.internal.QueryParameter
|
||||
import io.codearte.accurest.dsl.internal.Request
|
||||
import io.codearte.accurest.dsl.internal.UrlPath
|
||||
|
||||
import java.util.regex.Pattern
|
||||
import io.codearte.accurest.dsl.internal.Url
|
||||
import io.codearte.accurest.util.MapConverter
|
||||
|
||||
@PackageScope
|
||||
@TypeChecked
|
||||
@@ -67,24 +67,27 @@ class MockMvcSpockMethodBodyBuilder extends SpockMethodBodyBuilder {
|
||||
|
||||
protected String buildUrl(Request request) {
|
||||
if (request.url)
|
||||
return getTestSideValue(request.url.serverValue)
|
||||
return getTestSideValue(buildUrlFromUrlPath(request.url))
|
||||
if (request.urlPath)
|
||||
return getTestSideValue(buildUrlFromUrlPath(request.urlPath))
|
||||
throw new IllegalStateException("URL is not set!")
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
protected String buildUrlFromUrlPath(UrlPath urlPath) {
|
||||
String params = ""
|
||||
if (urlPath.queryParameters) {
|
||||
params = urlPath.queryParameters.parameters
|
||||
protected String buildUrlFromUrlPath(Url url) {
|
||||
if (hasQueryParams(url)) {
|
||||
String params = url.queryParameters.parameters
|
||||
.findAll(this.&allowedQueryParameter)
|
||||
.inject([] as List<String>) { List<String> result, QueryParameter param ->
|
||||
result << "${param.name}=${resolveParamValue(param).toString()}"
|
||||
}
|
||||
.join('&')
|
||||
return "${MapConverter.getTestSideValues(url.serverValue)}?$params"
|
||||
}
|
||||
return "$urlPath.serverValue?$params"
|
||||
return MapConverter.getTestSideValues(url.serverValue)
|
||||
}
|
||||
|
||||
private boolean hasQueryParams(Url url) {
|
||||
return url.queryParameters
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@ import static io.codearte.accurest.util.ContentUtils.*
|
||||
@TypeChecked
|
||||
abstract class SpockMethodBodyBuilder {
|
||||
|
||||
private static final Boolean TEST_SIDE = false
|
||||
|
||||
protected final Request request
|
||||
protected final Response response
|
||||
|
||||
@@ -211,6 +209,6 @@ abstract class SpockMethodBodyBuilder {
|
||||
}
|
||||
|
||||
protected String getTestSideValue(Object object) {
|
||||
return MapConverter.getClientOrServerSideValues(object, TEST_SIDE).toString()
|
||||
return MapConverter.getTestSideValues(object).toString()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,8 @@ import static io.codearte.accurest.util.MapConverter.transformValues
|
||||
@TypeChecked
|
||||
abstract class BaseWireMockStubStrategy {
|
||||
|
||||
private static final Boolean STUB_SIDE = true
|
||||
|
||||
protected getStubSideValue(Object object) {
|
||||
return MapConverter.getClientOrServerSideValues(object, STUB_SIDE)
|
||||
return MapConverter.getStubSideValues(object)
|
||||
}
|
||||
|
||||
private static Closure transform = {
|
||||
@@ -66,7 +64,7 @@ abstract class BaseWireMockStubStrategy {
|
||||
}
|
||||
|
||||
public String parseBody(Map map, ContentType contentType) {
|
||||
def transformedMap = MapConverter.getClientOrServerSideValues(map, true)
|
||||
def transformedMap = MapConverter.getStubSideValues(map)
|
||||
return parseBody(toJson(transformedMap), contentType)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@ import io.codearte.accurest.dsl.internal.DslProperty
|
||||
*/
|
||||
class MapConverter {
|
||||
|
||||
|
||||
public static final boolean STUB_SIDE = true
|
||||
public static final boolean TEST_SIDE = false
|
||||
|
||||
static def transformToClientValues(def value) {
|
||||
return transformValues(value) {
|
||||
it instanceof DslProperty ? it.clientValue : it
|
||||
@@ -73,4 +77,12 @@ class MapConverter {
|
||||
return it
|
||||
}
|
||||
}
|
||||
|
||||
static Object getStubSideValues(json) {
|
||||
return getClientOrServerSideValues(json, STUB_SIDE)
|
||||
}
|
||||
|
||||
static Object getTestSideValues(json) {
|
||||
return getClientOrServerSideValues(json, TEST_SIDE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,6 +337,50 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
|
||||
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
|
||||
}
|
||||
|
||||
@Issue('#169')
|
||||
def "should generate a call with an url path and query parameters with url containing a pattern"() {
|
||||
given:
|
||||
GroovyDsl contractDsl = GroovyDsl.make {
|
||||
request {
|
||||
method 'GET'
|
||||
url($(stub(regex('/foo/[0-9]+')), test('/foo/123456'))){
|
||||
queryParameters {
|
||||
parameter 'limit': $(client(equalTo("20")), server(equalTo("10")))
|
||||
parameter 'offset': $(client(containing("20")), server(equalTo("20")))
|
||||
parameter 'filter': "email"
|
||||
parameter 'sort': equalTo("name")
|
||||
parameter 'search': $(client(notMatching(~/^\/[0-9]{2}$/)), server("55"))
|
||||
parameter 'age': $(client(notMatching("^\\w*\$")), server("99"))
|
||||
parameter 'name': $(client(matching("Denis.*")), server("Denis.Stepanov"))
|
||||
parameter 'email': "bob@email.com"
|
||||
parameter 'hello': $(client(matching("Denis.*")), server(absent()))
|
||||
parameter 'hello': absent()
|
||||
}
|
||||
}
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
body """
|
||||
{
|
||||
"property1": "a",
|
||||
"property2": "b"
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl)
|
||||
BlockBuilder blockBuilder = new BlockBuilder(" ")
|
||||
when:
|
||||
builder.appendTo(blockBuilder)
|
||||
def spockTest = blockBuilder.toString()
|
||||
then:
|
||||
spockTest.contains('get("/foo/123456?limit=10&offset=20&filter=email&sort=name&search=55&age=99&name=Denis.Stepanov&email=bob@email.com")')
|
||||
spockTest.contains('$[?(@.property2 == \'b\')]')
|
||||
spockTest.contains('$[?(@.property1 == \'a\')]')
|
||||
and:
|
||||
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
|
||||
}
|
||||
|
||||
def "should generate test for empty body"() {
|
||||
given:
|
||||
GroovyDsl contractDsl = GroovyDsl.make {
|
||||
|
||||
Reference in New Issue
Block a user