diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/messaging/stream/StubRunnerStreamConfiguration.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/messaging/stream/StubRunnerStreamConfiguration.java index f36b7b0cda..543cc1bce4 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/messaging/stream/StubRunnerStreamConfiguration.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/messaging/stream/StubRunnerStreamConfiguration.java @@ -57,7 +57,8 @@ import org.springframework.util.StringUtils; */ @Configuration @ConditionalOnClass({ IntegrationFlows.class, EnableBinding.class }) -@ConditionalOnProperty(name = "stubrunner.stream.enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnProperty(name = "stubrunner.stream.enabled", havingValue = "true", + matchIfMissing = true) @AutoConfigureBefore(StubRunnerIntegrationConfiguration.class) public class StubRunnerStreamConfiguration { diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockRequestStubStrategy.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockRequestStubStrategy.groovy index fe90c12b3c..354d077ea1 100755 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockRequestStubStrategy.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockRequestStubStrategy.groovy @@ -115,24 +115,31 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy { if (!request.body) { return } + boolean bodyHasMatchingStrategy = request.body.clientValue instanceof MatchingStrategy MatchingStrategy matchingStrategy = getMatchingStrategyFromBody(request.body) if (contentType == ContentType.JSON) { def originalBody = matchingStrategy?.clientValue - def body = JsonToJsonPathsConverter. - removeMatchingJsonPaths(originalBody, request.bodyMatchers) - JsonPaths values = JsonToJsonPathsConverter. - transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(body) - if ((values.empty && !request.bodyMatchers?.hasMatchers()) - || - onlySizeAssertionsArePresent(values)) { - requestPattern.withRequestBody(WireMock.equalToJson(JsonOutput.toJson( - getMatchingStrategy(request.body.clientValue).clientValue), - false, false)) + if (bodyHasMatchingStrategy) { + requestPattern.withRequestBody( + convertToValuePattern(matchingStrategy)) } else { - values.findAll { !it.assertsSize() }.each { - requestPattern.withRequestBody(WireMock. - matchingJsonPath(it.jsonPath().replace("\\\\", "\\"))) + def body = JsonToJsonPathsConverter. + removeMatchingJsonPaths(originalBody, request.bodyMatchers) + JsonPaths values = JsonToJsonPathsConverter. + transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(body) + if ((values.empty && !request.bodyMatchers?.hasMatchers()) + || + onlySizeAssertionsArePresent(values)) { + requestPattern.withRequestBody(WireMock.equalToJson(JsonOutput.toJson( + getMatchingStrategy(request.body.clientValue).clientValue), + false, false)) + } + else { + values.findAll { !it.assertsSize() }.each { + requestPattern.withRequestBody(WireMock. + matchingJsonPath(it.jsonPath().replace("\\\\", "\\"))) + } } } request.bodyMatchers?.matchers()?.each { @@ -144,12 +151,18 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy { } else if (contentType == ContentType.XML) { Object originalBody = matchingStrategy?.clientValue - Object body = XmlToXPathsConverter - .removeMatchingXPaths(originalBody, request.bodyMatchers) - List byEqualityMatchersFromXml = new XmlToXPathsConverter() - .mapToMatchers(body) - byEqualityMatchersFromXml.each { - addWireMockStubMatchingSection(it, requestPattern, originalBody) + if (bodyHasMatchingStrategy) { + requestPattern.withRequestBody( + convertToValuePattern(matchingStrategy)) + } + else { + Object body = XmlToXPathsConverter + .removeMatchingXPaths(originalBody, request.bodyMatchers) + List byEqualityMatchersFromXml = new XmlToXPathsConverter() + .mapToMatchers(body) + byEqualityMatchersFromXml.each { + addWireMockStubMatchingSection(it, requestPattern, originalBody) + } } request.bodyMatchers?.matchers()?.each { addWireMockStubMatchingSection(it, requestPattern, originalBody) @@ -191,7 +204,7 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy { requestPattern.withRequestBody(WireMock.matchingXPath(pathMatcher.path(), XPathBodyMatcherToWireMockValuePatternConverter .mapToPattern(pathMatcher.matchingType(), - String.valueOf(retrievedValue)))) + String.valueOf(retrievedValue)))) } private boolean onlySizeAssertionsArePresent(JsonPaths values) { @@ -301,31 +314,31 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy { @TypeChecked(TypeCheckingMode.SKIP) private ContentPattern convertToValuePattern(Object object) { switch (object) { - case Pattern: - case RegexProperty: - return WireMock.matching(new RegexProperty(object).pattern()) - case OptionalProperty: - OptionalProperty value = object as OptionalProperty - return WireMock.matching(value.optionalPattern()) - case MatchingStrategy: - MatchingStrategy value = object as MatchingStrategy - switch (value.type) { - case MatchingStrategy.Type.NOT_MATCHING: - return WireMock.notMatching(value.clientValue.toString()) - case MatchingStrategy.Type.ABSENT: - return WireMock.absent() + case Pattern: + case RegexProperty: + return WireMock.matching(new RegexProperty(object).pattern()) + case OptionalProperty: + OptionalProperty value = object as OptionalProperty + return WireMock.matching(value.optionalPattern()) + case MatchingStrategy: + MatchingStrategy value = object as MatchingStrategy + switch (value.type) { + case MatchingStrategy.Type.NOT_MATCHING: + return WireMock.notMatching(value.clientValue.toString()) + case MatchingStrategy.Type.ABSENT: + return WireMock.absent() + default: + try { + return WireMock."${value.type.name}"( + clientBody(value.clientValue, contentType)) + } + catch (Throwable t) { + log.error("Exception occurred while trying to call WireMock.${value.type.name}(${value.clientValue})", t) + throw t + } + } default: - try { - return WireMock."${value.type.name}"( - clientBody(value.clientValue, contentType)) - } - catch (Throwable t) { - log.error("Exception occurred while trying to call WireMock.${value.type.name}(${value.clientValue})", t) - throw t - } - } - default: - return WireMock.equalTo(object.toString()) + return WireMock.equalTo(object.toString()) } } @@ -404,14 +417,14 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy { private MatchingStrategy appendBodyRegexpMatchPattern(Object value, ContentType contentType) { switch (contentType) { - case ContentType.JSON: - return new MatchingStrategy( - buildJSONRegexpMatch(value), MatchingStrategy.Type.MATCHING) - case ContentType.UNKNOWN: - return new MatchingStrategy( - buildGStringRegexpForStubSide(value), MatchingStrategy.Type.MATCHING) - case ContentType.XML: - throw new IllegalStateException("XML pattern matching is not implemented yet") + case ContentType.JSON: + return new MatchingStrategy( + buildJSONRegexpMatch(value), MatchingStrategy.Type.MATCHING) + case ContentType.UNKNOWN: + return new MatchingStrategy( + buildGStringRegexpForStubSide(value), MatchingStrategy.Type.MATCHING) + case ContentType.XML: + throw new IllegalStateException("XML pattern matching is not implemented yet") } } diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy index e4164fefe7..acac2ed89a 100755 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy @@ -40,32 +40,35 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should convert groovy dsl stub to wireMock stub for the client side'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('GET') - url $(consumer(~/\/[0-9]{2}/), producer('/12')) - } - response { - status OK() - body( - id: value( - consumer('123'), - producer(regex('[0-9]+')) - ), - surname: $( - consumer('Kowalsky'), - producer(regex('[a-zA-Z]+')) - ), - name: 'Jan', - created: $(consumer('2014-02-02 12:23:43'), producer(execute('currentDate($it)'))) - ) - headers { - header 'Content-Type': 'application/json' + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('GET') + url $(consumer(~/\/[0-9]{2}/), producer('/12')) + } + response { + status OK() + body( + id: value( + consumer('123'), + producer(regex('[0-9]+')) + ), + surname: $( + consumer('Kowalsky'), + producer(regex('[a-zA-Z]+')) + ), + name: 'Jan', + created: $(consumer('2014-02-02 12:23:43'), + producer(execute('currentDate($it)'))) + ) + headers { + header 'Content-Type': 'application/json' + } + } } - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: AssertionUtil.assertThatJsonsAreEqual(''' { @@ -90,30 +93,32 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue("#79") def 'should convert groovy dsl stub to wireMock stub for the client side with a body containing a map'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url '/ingredients' - headers { - header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json' + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url '/ingredients' + headers { + header 'Content-Type': 'application/vnd.pl.devoxx.aggregatr.v1+json' + } + } + response { + status OK() + body( + ingredients: [ + [type: 'MALT', quantity: 100], + [type: 'WATER', quantity: 200], + [type: 'HOP', quantity: 300], + [type: 'YIEST', quantity: 400] + ] + ) + } } - } - response { - status OK() - body( - ingredients: [ - [type: 'MALT', quantity: 100], - [type: 'WATER', quantity: 200], - [type: 'HOP', quantity: 300], - [type: 'YIEST', quantity: 400] - ] - ) - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual(''' + AssertionUtil.assertThatJsonsAreEqual(''' { "request" : { "url" : "/ingredients", @@ -138,31 +143,37 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue("#86") def 'should convert groovy dsl stub with GString and regexp'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('POST') - url('/ws/payments') - headers { - header("Content-Type": 'application/x-www-form-urlencoded') + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('POST') + url('/ws/payments') + headers { + header("Content-Type": 'application/x-www-form-urlencoded') + } + body("""paymentType=INCOMING&transferType=BANK&amount=${ + value(consumer(regex('[0-9]{3}\\.[0-9]{2}')), + producer(500.00)) + }&bookingDate=${ + value(consumer( + regex('[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])')), + producer('2015-05-18')) + }""") + } + response { + status 204 + body( + paymentId: value(consumer('4'), + producer(regex('[1-9][0-9]*'))), + foundExistingPayment: false + ) + } } - body("""paymentType=INCOMING&transferType=BANK&amount=${ - value(consumer(regex('[0-9]{3}\\.[0-9]{2}')), producer(500.00)) - }&bookingDate=${ - value(consumer(regex('[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])')), producer('2015-05-18')) - }""") - } - response { - status 204 - body( - paymentId: value(consumer('4'), producer(regex('[1-9][0-9]*'))), - foundExistingPayment: false - ) - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual(''' + AssertionUtil.assertThatJsonsAreEqual(''' { "request": { "method": "POST", @@ -191,29 +202,36 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should convert groovy dsl stub with Body as String to wireMock stub for the client side'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('GET') - url $(consumer(~/\/[0-9]{2}/), producer('/12')) - } - response { - status OK() - body("""\ + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('GET') + url $(consumer(~/\/[0-9]{2}/), producer('/12')) + } + response { + status OK() + body("""\ { "id": "${value(consumer('123'), producer('321'))}", - "surname": "${value(consumer('Kowalsky'), producer(regex('[a-zA-Z]+')))}", + "surname": "${ + value(consumer('Kowalsky'), producer(regex('[a-zA-Z]+'))) + }", "name": "Jan", - "created" : "${$(consumer('2014-02-02 12:23:43'), producer('2999-09-09 01:23:45'))}" + "created" : "${ + $(consumer('2014-02-02 12:23:43'), + producer('2999-09-09 01:23:45')) + }" } """ - ) - headers { - header 'Content-Type': 'application/json' + ) + headers { + header 'Content-Type': 'application/json' + } + } } - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: def actual = new JsonSlurper().parseText(wireMockStub) def expected = new JsonSlurper().parseText(''' @@ -244,33 +262,35 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should convert groovy dsl stub with simple Body as String to wireMock stub for the client side'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('GET') - url $(consumer(regex('/[0-9]{2}')), producer('/12')) - body """ + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('GET') + url $(consumer(regex('/[0-9]{2}')), producer('/12')) + body """ { "name": "Jan" } """ - } - response { - status OK() - body("""\ + } + response { + status OK() + body("""\ { "name": "Jan" } """ - ) - headers { - header 'Content-Type': 'application/json' + ) + headers { + header 'Content-Type': 'application/json' + } + } } - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual(''' + AssertionUtil.assertThatJsonsAreEqual(''' { "request" : { "urlPattern" : "/[0-9]{2}", @@ -295,31 +315,34 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should use equalToJson when body match is defined as map'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('GET') - url $(consumer(~/\/[0-9]{2}/), producer('/12')) - body( - id: value( - consumer(regex('[0-9]+')), - producer('123'), - ), - surname: $( - consumer(regex('[a-zA-Z]+')), - producer('Kowalsky'), - ), - name: 'Jan', - created: $(consumer('2014-02-02 12:23:43'), producer(execute('currentDate($it)'))) - ) - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('GET') + url $(consumer(~/\/[0-9]{2}/), producer('/12')) + body( + id: value( + consumer(regex('[0-9]+')), + producer('123'), + ), + surname: $( + consumer(regex('[a-zA-Z]+')), + producer('Kowalsky'), + ), + name: 'Jan', + created: $(consumer('2014-02-02 12:23:43'), + producer(execute('currentDate($it)'))) + ) + } + response { + status OK() + } + } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "urlPattern" : "/[0-9]{2}", @@ -346,27 +369,28 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should use equalToJson when content type ends with json'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url "/users" - headers { - header "Content-Type", "customtype/json" - } - body """ + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + headers { + header "Content-Type", "customtype/json" + } + body """ { "name": "Jan" } """ - } - response { - status OK() - } - } + } + response { + status OK() + } + } when: String json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "url" : "/users", @@ -392,25 +416,28 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should use xml matchers when content type ends with xml'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url "/users" - headers { - header "Content-Type", "customtype/xml" + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + headers { + header "Content-Type", "customtype/xml" + } + body """${ + value(consumer('Jozo'), producer('Denis')) + }${ + value(consumer("<test>"), producer('1234567890')) + }""" + } + response { + status OK() + } } - body """${value(consumer('Jozo'), producer('Denis'))}${ - value(consumer("<test>"), producer('1234567890')) - }""" - } - response { - status OK() - } - } when: String json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "url": "/users", @@ -450,22 +477,25 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should use xml matchers when content type is parsable xml'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url "/users" - body """${value(consumer('Jozo'), producer('Denis'))}${ - value(consumer("<test>"), producer('1234567890')) - }""" - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + body """${ + value(consumer('Jozo'), producer('Denis')) + }${ + value(consumer("<test>"), producer('1234567890')) + }""" + } + response { + status OK() + } + } when: String json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "url": "/users", @@ -500,22 +530,25 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should support xml as a response body'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url "/users" - } - response { - status OK() - body """${value(consumer('Jozo'), producer('Denis'))}${ - value(consumer(""), producer('1234567890')) - }""" - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + } + response { + status OK() + body """${ + value(consumer('Jozo'), producer('Denis')) + }${ + value(consumer(""), producer('1234567890')) + }""" + } + } when: String json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -534,20 +567,21 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should use equalToJson'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url "/users" - body equalToJson('''{"name":"Jan"}''') - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + body equalToJson('''{"name":"Jan"}''') + } + response { + status OK() + } + } when: String json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -568,24 +602,73 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie stubMappingIsValidWireMockStub(json) } - def 'should use equalToXml'() { + def 'should use equalToJson and bodyMatchers with json content type'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url "/users" - body equalToXml("""${value(consumer('Jozo'), producer('Denis'))}${ - value(consumer("<test>"), producer('1234567890')) - }""") - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + headers { + header 'Content-Type': 'application/json' + } + body equalToJson('''{"name":"Jan"}''') + bodyMatchers { + jsonPath('$.name', byRegex('[A-Z]{3}')) + } + } + response { + status OK() + } + } when: String json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' + { + "request": { + "method": "GET", + "url": "/users", + "bodyPatterns": [ + { + "equalToJson":"{\\"name\\":\\"Jan\\"}" + }, + { + "matchesJsonPath" : "$[?(@.name =~ /([A-Z]{3})/)]" + } + ] + }, + "response": { + "status": 200, + "transformers" : [ "response-template", "foo-transformer" ] + } + } + '''), json) + and: + stubMappingIsValidWireMockStub(json) + } + + def 'should use equalToXml'() { + given: + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + body equalToXml("""${ + value(consumer('Jozo'), producer('Denis')) + }${ + value(consumer("<test>"), producer('1234567890')) + }""") + } + response { + status OK() + } + } + when: + String json = toWireMockClientJsonStub(groovyDsl) + then: + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -606,25 +689,77 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie stubMappingIsValidWireMockStub(json) } + def 'should use equalToXml and bodyMatchers with xml content type'() { + given: + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url "/users" + headers { + header "Content-Type", "customtype/xml" + } + body equalToXml( + """${ + value(consumer('Jozo'), + producer('Denis')) + }1234567890""" + ) + bodyMatchers { + xPath('/foo/jobId/text()', byRegex('[0-9]{10}')) + } + } + response { + status OK() + } + } + when: + String json = toWireMockClientJsonStub(groovyDsl) + then: + AssertionUtil.assertThatJsonsAreEqual((''' + { + "request": { + "method": "GET", + "url": "/users", + "bodyPatterns" : [ { + "equalToXml" : "Jozo1234567890" + }, { + "matchesXPath" : { + "expression" : "/foo/jobId/text()", + "matches" : "[0-9]{10}" + } + } ] + }, + "response": { + "status": 200, + "transformers" : [ "response-template", "foo-transformer" ] + } + } + '''), json) + and: + stubMappingIsValidWireMockStub(json) + } + def 'should create stub with body from the file'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - headers { - header 'Content-Type': 'application/xml' - } - url "/users" - body file('classpath/request.xml') - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + headers { + header 'Content-Type': 'application/xml' + } + url "/users" + body file('classpath/request.xml') + } + response { + status OK() + } + } when: - String json = toWireMockClientJsonStub(groovyDsl) + String json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -651,38 +786,43 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } '''), json) and: - stubMappingIsValidWireMockStub(json) + stubMappingIsValidWireMockStub(json) } def 'should convert groovy dsl stub with regexp Body as String to wireMock stub for the client side'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('GET') - url $(consumer(regex('/[0-9]{2}')), producer('/12')) - body """ + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('GET') + url $(consumer(regex('/[0-9]{2}')), producer('/12')) + body """ { - "personalId": "${value(consumer(regex('^[0-9]{11}$')), producer('57593728525'))}" + "personalId": "${ + value(consumer(regex('^[0-9]{11}$')), + producer('57593728525')) + }" } """ - } - response { - status OK() - body("""\ + } + response { + status OK() + body("""\ { "name": "Jan" } """ - ) - headers { - header 'Content-Type': 'application/json' + ) + headers { + header 'Content-Type': 'application/json' + } + } } - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "urlPattern" : "/[0-9]{2}", @@ -707,38 +847,44 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should convert groovy dsl stub with a regexp and an integer in request body'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'PUT' - url '/fraudcheck' - body(""" + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'PUT' + url '/fraudcheck' + body(""" { - "clientPesel":"${value(consumer(regex('[0-9]{10}')), producer('1234567890'))}", + "clientPesel":"${ + value(consumer(regex('[0-9]{10}')), + producer('1234567890')) + }", "loanAmount":123.123 } """ - ) - headers { - header('Content-Type', 'application/vnd.fraud.v1+json') - } + ) + headers { + header('Content-Type', 'application/vnd.fraud.v1+json') + } - } - response { - status OK() - body( - fraudCheckStatus: "OK", - rejectionReason: $(consumer(null), producer(execute('assertThatRejectionReasonIsNull($it)'))) - ) - headers { - header('Content-Type': 'application/vnd.fraud.v1+json') - } - } + } + response { + status OK() + body( + fraudCheckStatus: "OK", + rejectionReason: $(consumer(null), producer( + execute('assertThatRejectionReasonIsNull($it)'))) + ) + headers { + header('Content-Type': 'application/vnd.fraud.v1+json') + } + } - } + } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "url" : "/fraudcheck", @@ -770,30 +916,38 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should generate request with urlPath and queryParameters for client side"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - urlPath($(consumer("users"), producer("items"))) { - queryParameters { - parameter 'limit': $(consumer(equalTo("20")), producer("10")) - parameter 'offset': $(consumer(containing("10")), producer("10")) - parameter 'filter': "email" - parameter 'sort': $(consumer(~/^[0-9]{10}$/), producer("1234567890")) - parameter 'search': $(consumer(notMatching(~/^\/[0-9]{2}$/)), producer("10")) - parameter 'age': $(consumer(notMatching("^\\w*\$")), producer(10)) - parameter 'name': $(consumer(matching("Denis.*")), producer("Denis")) - parameter 'credit': absent() + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + urlPath($(consumer("users"), producer("items"))) { + queryParameters { + parameter 'limit': + $(consumer(equalTo("20")), producer("10")) + parameter 'offset': + $(consumer(containing("10")), producer("10")) + parameter 'filter': "email" + parameter 'sort': $(consumer(~/^[0-9]{10}$/), + producer("1234567890")) + parameter 'search': + $(consumer(notMatching(~/^\/[0-9]{2}$/)), + producer("10")) + parameter 'age': $(consumer(notMatching("^\\w*\$")), + producer(10)) + parameter 'name': $(consumer(matching("Denis.*")), + producer("Denis")) + parameter 'credit': absent() + } + } + } + response { + status OK() } } - } - response { - status OK() - } - } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -838,22 +992,23 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue("#353") def "should generate request with absent header for client side"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - urlPath( '/some/path/*') - headers { - header('''Authentication''', absent()) + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + urlPath('/some/path/*') + headers { + header('''Authentication''', absent()) + } + } + response { + status OK() + } } - } - response { - status OK() - } - } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "urlPath" : "/some/path/*", @@ -878,23 +1033,28 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should generate request with urlPathPattern and queryParameters for client side\ when both contains regular expressions"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - urlPath($(consumer(regex("/users/[0-9]+")), producer("/users/1"))) { - queryParameters { - parameter 'search': $(consumer(notMatching(~/^\/[0-9]{2}$/)), producer("10")) + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + urlPath($( + consumer(regex("/users/[0-9]+")), + producer("/users/1"))) { + queryParameters { + parameter 'search': + $(consumer(notMatching(~/^\/[0-9]{2}$/)), + producer("10")) + } + } + } + response { + status OK() + } } - } - } - response { - status OK() - } - } when: - def json = toWireMockClientJsonStub(groovyDsl) + def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -912,25 +1072,26 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } '''), json) and: - stubMappingIsValidWireMockStub(json) + stubMappingIsValidWireMockStub(json) } def "should generate request with urlPath for client side"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - urlPath $(consumer("boxes"), producer("items")) - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + urlPath $(consumer("boxes"), producer("items")) + } + response { + status OK() + } + } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -948,19 +1109,20 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should generate simple request with urlPath for client side"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - urlPath "boxes" - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + urlPath "boxes" + } + response { + status OK() + } + } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -1004,7 +1166,8 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie method 'GET' url("abc") { queryParameters { - parameter 'age': $(consumer(notMatching("^\\w*\$")), producer(regex(".*"))) + parameter 'age': $(consumer(notMatching("^\\w*\$")), + producer(regex(".*"))) } } } @@ -1076,7 +1239,8 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie method 'GET' urlPath("users") { queryParameters { - parameter 'name': $(consumer(absent()), producer(matching("abc"))) + parameter 'name': $(consumer(absent()), + producer(matching("abc"))) } } } @@ -1089,24 +1253,28 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should generate request with url and queryParameters for client side"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - url($(consumer(regex(/users\/[0-9]*/)), producer("users/123"))) { - queryParameters { - parameter 'age': $(consumer(notMatching("^\\w*\$")), producer(10)) - parameter 'name': $(consumer(matching("Denis.*")), producer("Denis")) + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + url($(consumer(regex(/users\/[0-9]*/)), + producer("users/123"))) { + queryParameters { + parameter 'age': $(consumer(notMatching("^\\w*\$")), + producer(10)) + parameter 'name': $(consumer(matching("Denis.*")), + producer("Denis")) + } + } + } + response { + status OK() } } - } - response { - status OK() - } - } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "GET", @@ -1132,46 +1300,58 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should convert groovy dsl stub with rich tree Body as String to wireMock stub for the client side'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('GET') - url $(consumer(~/\/[0-9]{2}/), producer('/12')) - body """\ + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('GET') + url $(consumer(~/\/[0-9]{2}/), producer('/12')) + body """\ { - "personalId": "${value(consumer(regex('[0-9]{11}')), producer('57593728525'))}", + "personalId": "${ + value(consumer(regex('[0-9]{11}')), + producer('57593728525')) + }", "firstName": "${value(consumer(regex('.*')), producer('Bruce'))}", "lastName": "${value(consumer(regex('.*')), producer('Lee'))}", - "birthDate": "${value(consumer(regex('[0-9]{4}-[0-9]{2}-[0-9]{2}')), producer('1985-12-12'))}", + "birthDate": "${ + value(consumer(regex('[0-9]{4}-[0-9]{2}-[0-9]{2}')), + producer('1985-12-12')) + }", "errors": [ { - "propertyName": "${value(consumer(regex('[0-9]{2}')), producer('04'))}", + "propertyName": "${ + value(consumer(regex('[0-9]{2}')), producer('04')) + }", "providerValue": "Test" }, { - "propertyName": "${value(consumer(regex('[0-9]{2}')), producer('08'))}", + "propertyName": "${ + value(consumer(regex('[0-9]{2}')), producer('08')) + }", "providerValue": "Test" } ] } """ - } - response { - status OK() - body("""\ + } + response { + status OK() + body("""\ { "name": "Jan" } """ - ) - headers { - header 'Content-Type': 'application/json' + ) + headers { + header 'Content-Type': 'application/json' + } + } } - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "urlPattern" : "/[0-9]{2}", @@ -1204,35 +1384,40 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should use regexp matches when request body match is defined using a map with a pattern'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'POST' - url '/reissue-payment-order' - body( - loanNumber: "999997001", - amount: value(consumer(regex('[0-9.]+')), producer('100.00')), - currency: "DKK", - applicationName: value(consumer(regex('.*')), producer("Auto-Repayments")), - username: value(consumer(regex('.*')), producer("scheduler")), - cardId: 1 - ) - } - response { - status OK() - body ''' + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'POST' + url '/reissue-payment-order' + body( + loanNumber: "999997001", + amount: value( + consumer(regex('[0-9.]+')), + producer('100.00')), + currency: "DKK", + applicationName: value(consumer(regex('.*')), + producer("Auto-Repayments")), + username: value(consumer(regex('.*')), + producer("scheduler")), + cardId: 1 + ) + } + response { + status OK() + body ''' { "status": "OK" } ''' - headers { - header 'Content-Type': 'application/json' + headers { + header 'Content-Type': 'application/json' + } + } } - } - } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "url" : "/reissue-payment-order", @@ -1265,20 +1450,21 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should generate stub for empty body"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('POST') - url("test") - body("") - } - response { - status 406 - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('POST') + url("test") + body("") + } + response { + status 406 + } + } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "POST", @@ -1299,20 +1485,21 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should generate stub with priority"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - priority 9 - request { - method('POST') - url("test") - } - response { - status 406 - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + priority 9 + request { + method('POST') + url("test") + } + response { + status 406 + } + } when: def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "priority": 9, "request": { @@ -1330,22 +1517,24 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue("#127") def 'should use "test" as an alias for "server"'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('POST') - url("foo") - body( - property: value(consumer("value"), producer("value")) - ) - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('POST') + url("foo") + body( + property: value(consumer("value"), producer("value")) + ) + } + response { + status OK() + } + } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "method" : "POST", @@ -1366,22 +1555,24 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue("#121") def 'should generate stub with empty list as a value of a field'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method('POST') - url("foo") - body( - values: [] - ) - } - response { - status OK() - } - } + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method('POST') + url("foo") + body( + values: [] + ) + } + response { + status OK() + } + } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request": { "method": "POST", @@ -1403,34 +1594,39 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should generate stub properly resolving GString with regular expression'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - priority 1 - request { - method 'POST' - url '/users/password' - headers { - header 'Content-Type': 'application/json' + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + priority 1 + request { + method 'POST' + url '/users/password' + headers { + header 'Content-Type': 'application/json' + } + body( + email: $( + consumer(regex(email())), + producer('not.existing@user.com')), + callback_url: $(consumer(regex(hostname())), + producer('https://partners.com')) + ) + } + response { + status 404 + headers { + header 'Content-Type': 'application/json' + } + body( + code: 4, + message: "User not found by email = [${value(producer(regex(email())), consumer('not.existing@user.com'))}]" + ) + } } - body( - email: $(consumer(regex(email())), producer('not.existing@user.com')), - callback_url: $(consumer(regex(hostname())), producer('https://partners.com')) - ) - } - response { - status 404 - headers { - header 'Content-Type': 'application/json' - } - body( - code: 4, - message: "User not found by email = [${value(producer(regex(email())), consumer('not.existing@user.com'))}]" - ) - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "url" : "/users/password", @@ -1463,26 +1659,28 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def 'should generate stub properly resolving GString with regular expression in url'() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { - request { - method 'PUT' - url "/partners/${value(consumer(regex('^[0-9]*$')), producer('11'))}/agents/11/customers/09665703Z" - headers { - header 'Content-Type': 'application/json' + request { + method 'PUT' + url "/partners/${value(consumer(regex('^[0-9]*$')), producer('11'))}/agents/11/customers/09665703Z" + headers { + header 'Content-Type': 'application/json' + } + body( + first_name: 'Josef', + ) + } + response { + status 422 + } } - body( - first_name: 'Josef', - ) - } - response { - status 422 - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "urlPattern" : "/partners/^[0-9]*$/agents/11/customers/09665703Z", @@ -1509,9 +1707,10 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue('42') def 'should generate stub without optional parameters'() { when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, contractDsl), contractDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, contractDsl), contractDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "url" : "/users/password", @@ -1541,62 +1740,78 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie and: stubMappingIsValidWireMockStub(wireMockStub) where: - contractDsl << [ - org.springframework.cloud.contract.spec.Contract.make { - priority 1 - request { - method 'POST' - url '/users/password' - headers { - header 'Content-Type': 'application/json' + contractDsl << [ + org.springframework.cloud.contract.spec.Contract.make { + priority 1 + request { + method 'POST' + url '/users/password' + headers { + header 'Content-Type': 'application/json' + } + body( + email: $( + consumer(optional(regex(email()))), + producer('abc@abc.com')), + callback_url: $(consumer(regex(hostname())), + producer('https://partners.com')) + ) } - body( - email: $(consumer(optional(regex(email()))), producer('abc@abc.com')), - callback_url: $(consumer(regex(hostname())), producer('https://partners.com')) - ) - } - response { - status 404 - headers { - header 'Content-Type': 'application/json' + response { + status 404 + headers { + header 'Content-Type': 'application/json' + } + body( + code: $(consumer("123123"), + producer(optional("123123"))), + message: "User not found by email = [${value(producer(regex(email())), consumer('not.existing@user.com'))}]" + ) } - body( - code: $(consumer("123123"), producer(optional("123123"))), - message: "User not found by email = [${value(producer(regex(email())), consumer('not.existing@user.com'))}]" - ) - } - }, - org.springframework.cloud.contract.spec.Contract.make { - priority 1 - request { - method 'POST' - url '/users/password' - headers { - header 'Content-Type': 'application/json' - } - body( - """ { - "email" : "${value(consumer(optional(regex(email()))), producer('abc@abc.com'))}", - "callback_url" : "${value(consumer(regex(hostname())), producer('https://partners.com'))}" + }, + org.springframework.cloud.contract.spec.Contract.make { + priority 1 + request { + method 'POST' + url '/users/password' + headers { + header 'Content-Type': 'application/json' + } + body( + """ { + "email" : "${ + value(consumer(optional(regex(email()))), + producer('abc@abc.com')) + }", + "callback_url" : "${ + value(consumer(regex(hostname())), + producer('https://partners.com')) + }" } """ - ) - } - response { - status 404 - headers { - header 'Content-Type': 'application/json' + ) } - body( - """ { - "code" : "${value(consumer(123123), producer(optional(123123)))}", - "message" : "User not found by email = [${value(producer(regex(email())), consumer('not.existing@user.com'))}]" + response { + status 404 + headers { + header 'Content-Type': 'application/json' + } + body( + """ { + "code" : "${ + value(consumer(123123), + producer(optional(123123))) + }", + "message" : "User not found by email = [${ + value(producer(regex(email())), + consumer('not.existing@user.com')) + }]" } """ - ) + ) + } } - } - ] + ] } String toJsonString(value) { @@ -1608,39 +1823,46 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } String toWireMockClientJsonStub(Contract groovyDsl) { - return new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + return new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() } @Issue('180') def 'should generate stub with multipart parameters'() { given: - // tag::multipartdsl[] - org.springframework.cloud.contract.spec.Contract contractDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method "PUT" - url "/multipart" - headers { - contentType('multipart/form-data;boundary=AaB03x') + // tag::multipartdsl[] + org.springframework.cloud.contract.spec.Contract contractDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method "PUT" + url "/multipart" + headers { + contentType('multipart/form-data;boundary=AaB03x') + } + multipart( + // key (parameter name), value (parameter value) pair + formParameter: $(c(regex('".+"')), + p('"formParameterValue"')), + someBooleanParameter: + $(c(regex(anyBoolean())), p('true')), + // a parameter name (e.g. file) + file: named( + // name of the file + name: $(c(regex(nonEmpty())), + p('filename.csv')), + // content of the file + content: $(c(regex(nonEmpty())), + p('file content'))) + ) + } + response { + status OK() + } } - multipart( - // key (parameter name), value (parameter value) pair - formParameter: $(c(regex('".+"')), p('"formParameterValue"')), - someBooleanParameter: $(c(regex(anyBoolean())), p('true')), - // a parameter name (e.g. file) - file: named( - // name of the file - name: $(c(regex(nonEmpty())), p('filename.csv')), - // content of the file - content: $(c(regex(nonEmpty())), p('file content'))) - ) - } - response { - status OK() - } - } - // end::multipartdsl[] + // end::multipartdsl[] when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, contractDsl), contractDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, contractDsl), contractDsl). + toWireMockClientStub() then: println wireMockStub AssertionUtil.assertThatJsonsAreEqual(( @@ -1678,55 +1900,56 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue('#219') def "should generate request with an optional queryParameter for client side"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - urlPath ('/some/api') { - queryParameters { - parameter 'size': value( - consumer(regex('[0-9]+')), - producer(1) - ) - parameter 'page': value( - consumer(regex('[0-9]+')), - producer(0) - ) - parameter sort: value( - consumer(optional(regex('^[a-z]+$'))), - producer('id') - ) + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + urlPath('/some/api') { + queryParameters { + parameter 'size': value( + consumer(regex('[0-9]+')), + producer(1) + ) + parameter 'page': value( + consumer(regex('[0-9]+')), + producer(0) + ) + parameter sort: value( + consumer(optional(regex('^[a-z]+$'))), + producer('id') + ) + } + } + } + response { + status OK() + 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 + ) + } } - } - } - response { - status OK() - 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) + def json = toWireMockClientJsonStub(groovyDsl) then: - AssertionUtil.assertThatJsonsAreEqual((''' + AssertionUtil.assertThatJsonsAreEqual((''' { "request" : { "urlPath" : "/some/api", @@ -1751,30 +1974,31 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } '''), json) and: - stubMappingIsValidWireMockStub(json) + stubMappingIsValidWireMockStub(json) } @Issue('#30') def "should not create a stub for a skipped contract"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - ignored() - method 'GET' - urlPath ('/some/api') { - queryParameters { - parameter 'size': value( - consumer(regex('[0-9]+')), - producer(1) - ) + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + ignored() + method 'GET' + urlPath('/some/api') { + queryParameters { + parameter 'size': value( + consumer(regex('[0-9]+')), + producer(1) + ) + } + } + } + response { + status OK() + body('') + } } - } - } - response { - status OK() - body('') - } - } when: def json = toWireMockClientJsonStub(groovyDsl) then: @@ -1784,25 +2008,27 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue('#30') def "should not create a stub for a contract matching ignored pattern"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'GET' - urlPath ('/some/api') { - queryParameters { - parameter 'size': value( - consumer(regex('[0-9]+')), - producer(1) - ) + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'GET' + urlPath('/some/api') { + queryParameters { + parameter 'size': value( + consumer(regex('[0-9]+')), + producer(1) + ) + } + } + } + response { + status OK() + body('') + } } - } - } - response { - status OK() - body('') - } - } when: - def json = new WireMockStubStrategy("Test", new ContractMetadata(null, true, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + def json = new WireMockStubStrategy("Test", new ContractMetadata(null, true, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: json == '' } @@ -1855,7 +2081,8 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie when: ResponseEntity entity = call(port) then: - entity.headers.find { it.key == "Authorization" && it.value.contains("secret;foo") } + entity.headers. + find { it.key == "Authorization" && it.value.contains("secret;foo") } AssertionUtil.assertThatJsonsAreEqual((''' { "url" : "/api/v1/xxxx?foo=bar&foo=bar2", @@ -1876,10 +2103,13 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue("#664") def "should work with byte arrays"() { given: - File request = new File(WireMockGroovyDslSpec.getResource("/body_builder/request.pdf").file) - File response = new File(WireMockGroovyDslSpec.getResource("/body_builder/response.pdf").file) + File request = new File(WireMockGroovyDslSpec. + getResource("/body_builder/request.pdf").file) + File response = new File(WireMockGroovyDslSpec. + getResource("/body_builder/response.pdf").file) and: - File file = new File(WireMockGroovyDslSpec.getResource("/body_builder/worksWithPdf.groovy").file) + File file = new File(WireMockGroovyDslSpec. + getResource("/body_builder/worksWithPdf.groovy").file) Contract contract = ContractVerifierDslConverter.convertAsCollection( file.parentFile, file ).first() @@ -1903,54 +2133,58 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue('#540') def "should generate a stub with standard WireMock request template"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'POST' - url('/api/v1/xxxx') { - queryParameters { - parameter("foo", "bar") - parameter("foo", "bar2") + org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract. + make { + request { + method 'POST' + url('/api/v1/xxxx') { + queryParameters { + parameter("foo", "bar") + parameter("foo", "bar2") + } + } + headers { + header(authorization(), "secret") + header(authorization(), "secret2") + } + cookies { + cookie("foo", "bar") + } + body(foo: "bar", baz: 5) + } + response { + status OK() + headers { + header(authorization(), "${fromRequest().header(authorization())};foo") + } + body( + url: fromRequest().url(), + path: fromRequest().path(), + pathIndex: fromRequest().path(1), + param: fromRequest().query("foo"), + paramIndex: fromRequest().query("foo", 1), + authorization: fromRequest().header("Authorization"), + authorization2: + fromRequest().header("Authorization", 1), + fullBody: fromRequest().body(), + responseFoo: fromRequest().body('$.foo'), + responseBaz: fromRequest().body('$.baz'), + responseBaz2: "Bla bla ${fromRequest().body('$.foo')} bla bla", + rawUrl: fromRequest().rawUrl(), + rawPath: fromRequest().rawPath(), + rawPathIndex: fromRequest().rawPath(1), + rawParam: fromRequest().rawQuery("foo"), + rawParamIndex: fromRequest().rawQuery("foo", 1), + rawAuthorization: + fromRequest().rawHeader("Authorization"), + rawAuthorization2: + fromRequest().rawHeader("Authorization", 1), + rawResponseFoo: fromRequest().rawBody('$.foo'), + rawResponseBaz: fromRequest().rawBody('$.baz'), + rawResponseBaz2: "Bla bla ${fromRequest().rawBody('$.foo')} bla bla" + ) + } } - } - headers { - header(authorization(), "secret") - header(authorization(), "secret2") - } - cookies { - cookie("foo", "bar") - } - body(foo: "bar", baz: 5) - } - response { - status OK() - headers { - header(authorization(), "${fromRequest().header(authorization())};foo") - } - body( - url: fromRequest().url(), - path: fromRequest().path(), - pathIndex: fromRequest().path(1), - param: fromRequest().query("foo"), - paramIndex: fromRequest().query("foo", 1), - authorization: fromRequest().header("Authorization"), - authorization2: fromRequest().header("Authorization", 1), - fullBody: fromRequest().body(), - responseFoo: fromRequest().body('$.foo'), - responseBaz: fromRequest().body('$.baz'), - responseBaz2: "Bla bla ${fromRequest().body('$.foo')} bla bla", - rawUrl: fromRequest().rawUrl(), - rawPath: fromRequest().rawPath(), - rawPathIndex: fromRequest().rawPath(1), - rawParam: fromRequest().rawQuery("foo"), - rawParamIndex: fromRequest().rawQuery("foo", 1), - rawAuthorization: fromRequest().rawHeader("Authorization"), - rawAuthorization2: fromRequest().rawHeader("Authorization", 1), - rawResponseFoo: fromRequest().rawBody('$.foo'), - rawResponseBaz: fromRequest().rawBody('$.baz'), - rawResponseBaz2: "Bla bla ${fromRequest().rawBody('$.foo')} bla bla" - ) - } - } when: def json = toWireMockClientJsonStub(groovyDsl) then: @@ -1999,7 +2233,8 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie server.addStubMapping(WireMockStubMapping.buildFrom(json)) then: ResponseEntity entity = call(port) - entity.headers.find { it.key == "Authorization" && it.value.contains("secret;foo") } + entity.headers. + find { it.key == "Authorization" && it.value.contains("secret;foo") } and: AssertionUtil.assertThatJsonsAreEqual((''' { @@ -2107,23 +2342,23 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue('#266') def "should work with array of arrays"() { given: - org.springframework.cloud.contract.spec.Contract groovyDsl = Contract.make { - request { - method 'POST' - urlPath '/api/categories' - body([["Programming", "Java"], ["Programming", "Java", "Spring", "Boot"]]) - headers { - header('Content-Type': 'application/json;charset=UTF-8') + org.springframework.cloud.contract.spec.Contract groovyDsl = Contract.make { + request { + method 'POST' + urlPath '/api/categories' + body([["Programming", "Java"], ["Programming", "Java", "Spring", "Boot"]]) + headers { + header('Content-Type': 'application/json;charset=UTF-8') + } + } + response { + status 200 + body([["Programming", "Java"], ["Programming", "Java", "Spring", "Boot"]]) + headers { + header('Content-Type': 'application/json;charset=UTF-8') + } } } - response { - status 200 - body([["Programming", "Java"], ["Programming", "Java", "Spring", "Boot"]]) - headers { - header('Content-Type': 'application/json;charset=UTF-8') - } - } - } when: def json = toWireMockClientJsonStub(groovyDsl) then: @@ -2178,23 +2413,24 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should create a stub for dot separated keys"() { given: Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - request { - method 'PUT' - url '/fraudcheck' - body([ // (4) - "client.id": $(regex('[0-9]{10}')), - loanAmount: 99999 - ]) - headers { - contentType('application/vnd.fraud.v1+json') + request { + method 'PUT' + url '/fraudcheck' + body([ // (4) + "client.id": $(regex('[0-9]{10}')), + loanAmount : 99999 + ]) + headers { + contentType('application/vnd.fraud.v1+json') + } + } + response { + status OK() } } - response { - status OK() - } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: AssertionUtil.assertThatJsonsAreEqual(''' { @@ -2258,7 +2494,8 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: AssertionUtil.assertThatJsonsAreEqual(''' { @@ -2288,20 +2525,20 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie def "should not escape unicode characters"() { given: Contract groovyDsl = Contract.make { - request { - method 'PUT' - url '/fraudcheck' - body([ // (4) - "client.id": $(regex('[0-9]{10}')), - loanAmount: 99999 - ]) - headers { - contentType('application/vnd.fraud.v1+json') + request { + method 'PUT' + url '/fraudcheck' + body([ // (4) + "client.id": $(regex('[0-9]{10}')), + loanAmount : 99999 + ]) + headers { + contentType('application/vnd.fraud.v1+json') + } } - } - response { - status OK() - body(""" + response { + status OK() + body(""" { "code": 91015, "description": "订单已失效", @@ -2309,10 +2546,11 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie "payload": null } """) + } } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: AssertionUtil.assertThatJsonsAreEqual(''' { @@ -2376,7 +2614,8 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: !wireMockStub.contains("&") and: @@ -2418,27 +2657,29 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie @Issue('#667') def "should not double escape backslashes"() { given: - Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { - priority 1 - request { - method 'GET' - urlPath(value(consumer(regex(/\/data\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)), - producer('/data/444d57b2-e309-4576-83cb-5530ee03106a'))) - } - response { - status 200 - headers { - header("Content-Type", "application/json;charset=UTF-8") + Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.make { + priority 1 + request { + method 'GET' + urlPath(value(consumer( + regex(/\/data\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)), + producer('/data/444d57b2-e309-4576-83cb-5530ee03106a'))) + } + response { + status 200 + headers { + header("Content-Type", "application/json;charset=UTF-8") + } + body([ + jsonString: '{\\"attribute\\": \\"value\\"}' + ]) } - body([ - jsonString: '{\\"attribute\\": \\"value\\"}' - ]) } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual(''' + AssertionUtil.assertThatJsonsAreEqual(''' { "request" : { "urlPathPattern" : "/data/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", @@ -2456,29 +2697,30 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } ''', wireMockStub) and: - stubMappingIsValidWireMockStub(wireMockStub) + stubMappingIsValidWireMockStub(wireMockStub) } @Issue('#708') def "should work with boolean body"() { given: - Contract groovyDsl = Contract.make { - request { - method('DELETE') - urlPath("/item/1") - } - response { - status(200) - headers { - contentType(applicationJsonUtf8()) + Contract groovyDsl = Contract.make { + request { + method('DELETE') + urlPath("/item/1") + } + response { + status(200) + headers { + contentType(applicationJsonUtf8()) + } + body("true") } - body("true") } - } when: - String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).toWireMockClientStub() + String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl). + toWireMockClientStub() then: - AssertionUtil.assertThatJsonsAreEqual(''' + AssertionUtil.assertThatJsonsAreEqual(''' { "request" : { "urlPath" : "/item/1", @@ -2495,7 +2737,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } ''', wireMockStub) and: - stubMappingIsValidWireMockStub(wireMockStub) + stubMappingIsValidWireMockStub(wireMockStub) } def should_generate_stubs_with_request_body_matchers() { @@ -2558,12 +2800,13 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie private ResponseTemplateTransformer responseTemplateTransformer() { return new ResponseTemplateTransformer(false, [(HandlebarsJsonPathHelper.NAME): new HandlebarsJsonPathHelper(), - (HandlebarsEscapeHelper.NAME): new HandlebarsEscapeHelper()]) + (HandlebarsEscapeHelper.NAME) : new HandlebarsEscapeHelper()]) } ResponseEntity call(int port) { return new TestRestTemplate().exchange( - RequestEntity.post(URI.create("http://localhost:" + port + "/api/v1/xxxx?foo=bar&foo=bar2")) + RequestEntity.post(URI. + create("http://localhost:" + port + "/api/v1/xxxx?foo=bar&foo=bar2")) .header("Authorization", "secret") .header("Authorization", "secret2") .header("Cookie", "foo=bar") @@ -2579,8 +2822,10 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie String callApiCategories(int port) { return new TestRestTemplate().exchange( - RequestEntity.post(URI.create("http://localhost:" + port + "/api/categories")) + RequestEntity. + post(URI.create("http://localhost:" + port + "/api/categories")) .header("Content-Type", "application/json;charset=UTF-8") - .body(JsonOutput.toJson([["Programming", "Java"], ["Programming", "Java", "Spring", "Boot"]])), String.class).body + .body(JsonOutput. + toJson([["Programming", "Java"], ["Programming", "Java", "Spring", "Boot"]])), String.class).body } }