From c05f19cdb722491c491dd05cdfa90be91c252c9b Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 26 Oct 2018 19:47:42 +0200 Subject: [PATCH] Fixed invalid cookie and URL YAML parsing; fixes gh-770 --- .../verifier/converter/YamlToContracts.groovy | 34 ++++++++++--------- .../YamlContractConverterSpec.groovy | 2 ++ .../test/resources/yml/contract_matchers.yml | 13 ++++++- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/converter/YamlToContracts.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/converter/YamlToContracts.groovy index 802a50c637..5852e901e0 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/converter/YamlToContracts.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/converter/YamlToContracts.groovy @@ -103,13 +103,11 @@ class YamlToContracts { matchers.each { YamlContract.KeyValueMatcher matcher -> if (value instanceof List) { ((List) value).each { - Object clientValue = clientValue(it, matcher, key) - header(key, new DslProperty(clientValue, - it instanceof DslProperty ? it.serverValue : it)) + header(key, clientValue(it, matcher, key).clientValue) } } else { - Object clientValue = clientValue(value, matcher, key) - header(key, new DslProperty(clientValue, serverValue(value, matcher))) + header(key, new DslProperty(clientValue(value, matcher, key).clientValue, + serverValue(value, matcher))) } } if (!matchers) { @@ -122,8 +120,7 @@ class YamlToContracts { cookies { yamlContract.request?.cookies?.each { String key, Object value -> YamlContract.KeyValueMatcher matcher = yamlContract.request.matchers.cookies.find { it.key == key } - Object clientValue = clientValue(value, matcher, key) - cookie(key, new DslProperty(clientValue, value)) + cookie(key, clientValue(value, matcher, key)) } } } @@ -233,8 +230,8 @@ class YamlToContracts { cookies { yamlContract.response?.cookies?.each { String key, Object value -> YamlContract.TestCookieMatcher matcher = yamlContract.response.matchers.cookies.find { it.key == key } - Object serverValue = serverCookieValue(value, matcher, key) - cookie(key, new DslProperty(value, serverValue)) + DslProperty cookieValue = serverCookieValue(value, matcher, key) + cookie(key, cookieValue) } } } @@ -306,8 +303,7 @@ class YamlToContracts { messageHeaders { yamlContract.input?.messageHeaders?.each { String key, Object value -> YamlContract.KeyValueMatcher matcher = yamlContract.input.matchers?.headers?.find { it.key == key } - Object clientValue = clientValue(value, matcher, key) - header(key, new DslProperty(clientValue, value)) + header(key, clientValue(value, matcher, key)) } } if (yamlContract.input.messageBody) messageBody(yamlContract.input.messageBody) @@ -409,8 +405,10 @@ class YamlToContracts { protected DslProperty urlValue(String url, YamlContract.KeyValueMatcher urlMatcher) { if (urlMatcher) { + if (urlMatcher.command) { + return new DslProperty(url, new ExecutionProperty(urlMatcher.command)) + } return new DslProperty(urlMatcher.regex ? Pattern.compile(urlMatcher.regex) : - urlMatcher.command ? new ExecutionProperty(urlMatcher.command) : urlMatcher.predefined ? predefinedToPattern(urlMatcher.predefined) : url, url) } return new DslProperty(url) @@ -440,7 +438,7 @@ class YamlToContracts { return serverValue } - protected Object serverCookieValue(Object value, YamlContract.TestCookieMatcher matcher, String key) { + protected DslProperty serverCookieValue(Object value, YamlContract.TestCookieMatcher matcher, String key) { Object serverValue = value if (matcher?.regex) { serverValue = Pattern.compile(matcher.regex) @@ -450,11 +448,13 @@ class YamlToContracts { Pattern pattern = predefinedToPattern(matcher.predefined) serverValue = pattern assertPatternMatched(pattern, value, key) + } else if (matcher?.command) { + return new DslProperty(new ExecutionProperty(matcher.command), value) } - return serverValue + return new DslProperty(value, serverValue) } - protected Object clientValue(Object value, YamlContract.KeyValueMatcher matcher, String key) { + protected DslProperty clientValue(Object value, YamlContract.KeyValueMatcher matcher, String key) { Object clientValue = value instanceof DslProperty ? value.clientValue : value if (matcher?.regex) { clientValue = Pattern.compile(matcher.regex) @@ -464,8 +464,10 @@ class YamlToContracts { Pattern pattern = predefinedToPattern(matcher.predefined) clientValue = pattern assertPatternMatched(pattern, value, key) + } else if (matcher?.command) { + return new DslProperty(value, new ExecutionProperty(matcher.command)) } - return clientValue + return new DslProperty(clientValue, value) } protected Object queryParamValue(YamlContract yamlContract, String key, Object value) { diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy index f6936bbe82..8c2e4114da 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy @@ -234,6 +234,8 @@ class YamlContractConverterSpec extends Specification { contract.request.bodyMatchers.jsonPathRegexMatchers[8].value() == patterns.isoTime() contract.request.bodyMatchers.jsonPathRegexMatchers[9].path() == "\$.['key'].['complex.key']" contract.request.bodyMatchers.jsonPathRegexMatchers[9].matchingType() == MatchingType.EQUALITY + contract.request.cookies.entries.find { it.key == "foo" }.clientValue instanceof Pattern + contract.request.cookies.entries.find { it.key == "bar" }.serverValue == new ExecutionProperty('equals($it)') and: contract.response.status.clientValue == 200 contract.response.bodyMatchers.jsonPathRegexMatchers[0].path() == '$.duck' diff --git a/spring-cloud-contract-verifier/src/test/resources/yml/contract_matchers.yml b/spring-cloud-contract-verifier/src/test/resources/yml/contract_matchers.yml index 43c9ff6e20..41d0d540ac 100644 --- a/spring-cloud-contract-verifier/src/test/resources/yml/contract_matchers.yml +++ b/spring-cloud-contract-verifier/src/test/resources/yml/contract_matchers.yml @@ -5,6 +5,7 @@ request: Content-Type: application/json cookies: foo: 2 + bar: 3 queryParameters: limit: 10 offset: 20 @@ -32,7 +33,7 @@ request: regex: /get/[0-9] # predefined: # execute a method - # command: + #command: 'equals($it)' queryParameters: - key: limit type: equal_to @@ -57,6 +58,8 @@ request: cookies: - key: foo regex: '[0-9]' + - key: bar + command: 'equals($it)' headers: - key: Content-Type regex: "application/json.*" @@ -89,6 +92,9 @@ request: type: by_null response: status: 200 + cookies: + foo: 1 + bar: 2 body: duck: 123 alpha: "abc" @@ -120,6 +126,11 @@ response: headers: - key: Content-Type regex: "application/json.*" + cookies: + - key: foo + regex: '[0-9]' + - key: bar + command: 'equals($it)' body: - path: $.duck type: by_regex