From 24d88fdce7bb102662aefb146a9b51a8e8c54023 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Sat, 30 Apr 2016 16:41:12 +0200 Subject: [PATCH] Added possibility to provide optional query param fixes #219 --- .../dsl/WireMockRequestStubStrategy.groovy | 4 + .../accurest/dsl/WireMockGroovyDslSpec.groovy | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WireMockRequestStubStrategy.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WireMockRequestStubStrategy.groovy index f5c4f6945c..59d252a30c 100755 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WireMockRequestStubStrategy.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/WireMockRequestStubStrategy.groovy @@ -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) { 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 78ddfc8ca3..269d1c3b6f 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 @@ -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) + } }