Added possibility to provide optional query param

fixes #219
This commit is contained in:
Marcin Grzejszczak
2016-04-30 16:41:12 +02:00
parent 8de4d742fe
commit 24d88fdce7
2 changed files with 82 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ import io.codearte.accurest.dsl.internal.Body
import io.codearte.accurest.dsl.internal.DslProperty
import io.codearte.accurest.dsl.internal.MatchingStrategy
import io.codearte.accurest.dsl.internal.NamedProperty
import io.codearte.accurest.dsl.internal.OptionalProperty
import io.codearte.accurest.dsl.internal.QueryParameters
import io.codearte.accurest.dsl.internal.RegexPatterns
import io.codearte.accurest.dsl.internal.Request
@@ -152,6 +153,9 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
case Pattern:
Pattern value = object as Pattern
return ValuePattern.matches(value.pattern())
case OptionalProperty:
OptionalProperty value = object as OptionalProperty
return ValuePattern.matches(value.optionalPattern())
case MatchingStrategy:
MatchingStrategy value = object as MatchingStrategy
switch (value.type) {

View File

@@ -1483,4 +1483,82 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
and:
stubMappingIsValidWireMockStub(wireMockStub)
}
@Issue('#219')
def "should generate request with an optional queryParameter for client side"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
request {
method 'GET'
urlPath ('/some/api') {
queryParameters {
parameter 'size': value(
client(regex('[0-9]+')),
server(1)
)
parameter 'page': value(
client(regex('[0-9]+')),
server(0)
)
parameter sort: value(
client(optional(regex('^[a-z]+$'))),
server('id')
)
}
}
}
response {
status 200
body(
content: [[
id : '00000000-0000-0000-0000-000000000000',
type : 'Extraordinary',
state : 'ACTIVE',
]],
totalPages: 1,
totalElements: 1,
last: true,
sort: [[
direction: 'ASC',
property: 'id',
ignoreCase: false,
nullHandling: 'NATIVE',
ascending: true
]],
first: true,
numberOfElements: 1,
size: 1,
number: 0
)
}
}
when:
def json = toWireMockClientJsonStub(groovyDsl)
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
"request" : {
"urlPath" : "/some/api",
"method" : "GET",
"queryParameters" : {
"size" : {
"matches" : "[0-9]+"
},
"page" : {
"matches" : "[0-9]+"
},
"sort" : {
"matches" : "(^[a-z]+$)?"
}
}
},
"response" : {
"status" : 200,
"body" : "{\\"content\\":[{\\"id\\":\\"00000000-0000-0000-0000-000000000000\\",\\"type\\":\\"Extraordinary\\",\\"state\\":\\"ACTIVE\\"}],\\"totalPages\\":1,\\"totalElements\\":1,\\"last\\":true,\\"sort\\":[{\\"direction\\":\\"ASC\\",\\"property\\":\\"id\\",\\"ignoreCase\\":false,\\"nullHandling\\":\\"NATIVE\\",\\"ascending\\":true}],\\"first\\":true,\\"numberOfElements\\":1,\\"size\\":1,\\"number\\":0}"
}
}
'''), json)
and:
stubMappingIsValidWireMockStub(json)
}
}