From 7e8d7fc78277db7abd0c2a9bdaedb5829b0a57f6 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 30 May 2023 12:57:20 +1000 Subject: [PATCH] fix: MockServerURLGenerator was not combining URL fragments correctly --- .../com/dius/pact/core/model/generators/Generator.kt | 7 ++----- .../model/generators/MockServerURLGeneratorSpec.groovy | 10 +++++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/generators/Generator.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/generators/Generator.kt index d944c28711..7207bb3612 100755 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/generators/Generator.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/generators/Generator.kt @@ -2,6 +2,7 @@ package au.com.dius.pact.core.model.generators import au.com.dius.pact.core.model.PactSpecVersion import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory +import au.com.dius.pact.core.support.HttpClientUtils.buildUrl import au.com.dius.pact.core.support.Json import au.com.dius.pact.core.support.Result import au.com.dius.pact.core.support.expressions.DataType @@ -563,11 +564,7 @@ data class MockServerURLGenerator( val regex = Regex(regex) val match = regex.matchEntire(example) if (match != null) { - if (href.endsWith('/')) { - href + match.groupValues[1] - } else { - href + "/" + match.groupValues[1] - } + buildUrl(href, match.groupValues[1]).toString() } else { logger.error { "MockServerURL: can not generate a value as the regex did not match the example, " + diff --git a/core/model/src/test/groovy/au/com/dius/pact/core/model/generators/MockServerURLGeneratorSpec.groovy b/core/model/src/test/groovy/au/com/dius/pact/core/model/generators/MockServerURLGeneratorSpec.groovy index 939e7bef9a..6a359aec1f 100644 --- a/core/model/src/test/groovy/au/com/dius/pact/core/model/generators/MockServerURLGeneratorSpec.groovy +++ b/core/model/src/test/groovy/au/com/dius/pact/core/model/generators/MockServerURLGeneratorSpec.groovy @@ -3,9 +3,9 @@ package au.com.dius.pact.core.model.generators import spock.lang.Specification import spock.lang.Unroll +@SuppressWarnings('LineLength') class MockServerURLGeneratorSpec extends Specification { - @SuppressWarnings('LineLength') @Unroll def 'generate returns null when #desc'() { expect: @@ -34,4 +34,12 @@ class MockServerURLGeneratorSpec extends Specification { [mockServer: [href: 'http://mockserver/']] ] } + + def 'examples from Pact Compatability Suite'() { + expect: + new MockServerURLGenerator('http://localhost:9876/pacts/provider/{provider}/for-verification', + '.*(\\/\\Qpacts\\E\\/\\Qprovider\\E\\/\\Q{provider}\\E\\/\\Qfor-verification\\E)$') + .generate([mockServer: [href: 'http://localhost:40955']], null) == + 'http://localhost:40955/pacts/provider/%7Bprovider%7D/for-verification' + } }