Skip to content

Commit

Permalink
chore: add some tests around handling IO errors from Pact Broker #1322
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Mar 13, 2021
1 parent b94e5d6 commit 32d22e7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.github.michaelbull.result.unwrap
import com.google.common.net.UrlEscapers.urlPathSegmentEscaper
import mu.KLogging
import java.io.File
import java.io.IOException
import java.net.URLDecoder
import java.util.function.Consumer

Expand Down Expand Up @@ -114,6 +115,7 @@ interface IPactBrokerClient {
/**
* Fetches all consumers for the given provider and selectors
*/
@Throws(IOException::class)
fun fetchConsumersWithSelectors(
providerName: String,
selectors: List<ConsumerVersionSelector>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll

import javax.net.ssl.SSLHandshakeException

@SuppressWarnings('UnnecessaryGetter')
class PactBrokerClientSpec extends Specification {

Expand Down Expand Up @@ -587,4 +589,27 @@ class PactBrokerClientSpec extends Specification {
1 * client.fetchConsumersWithTag('provider', 'MASTER') >> []
result instanceof Ok
}

@Issue('#1322')
def 'when fetching pacts fails with a certificate error'() {
given:
def halClient = Mock(IHalClient)
PactBrokerClient client = Spy(PactBrokerClient, constructorArgs: ['baseUrl']) {
newHalClient() >> halClient
}
def selectors = [
new ConsumerVersionSelector('DEV', true, null, 'MASTER')
]

when:
def result = client.fetchConsumersWithSelectors('provider', selectors, [], false, '')

then:
1 * halClient.navigate() >> {
throw new InvalidNavigationRequest('PKIX path building failed', new SSLHandshakeException('PKIX path building failed'))
}
notThrown(SSLHandshakeException)
result instanceof Err
result.component2() instanceof InvalidNavigationRequest
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import au.com.dius.pact.core.model.PactReader
import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector
import au.com.dius.pact.core.pactbroker.IPactBrokerClient
import au.com.dius.pact.core.pactbroker.InvalidHalResponse
import au.com.dius.pact.core.pactbroker.InvalidNavigationRequest
import au.com.dius.pact.core.pactbroker.PactBrokerResult
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
import au.com.dius.pact.core.support.expressions.ValueResolver
Expand All @@ -21,6 +22,7 @@ import spock.lang.Specification
import spock.lang.Unroll
import spock.util.environment.RestoreSystemProperties

import javax.net.ssl.SSLHandshakeException
import java.lang.annotation.Annotation

import static au.com.dius.pact.core.support.expressions.ExpressionParser.VALUES_SEPARATOR
Expand Down Expand Up @@ -1247,6 +1249,18 @@ class PactBrokerLoaderSpec extends Specification {
'localhost' | '1234' | 'https' | null | 'https://localhost:1234'
}

@Issue('#1322')
def 'Throws an Exception if there is a certificate error'() {
when:
pactBrokerLoader().load('test')

then:
1 * brokerClient.fetchConsumersWithSelectors('test', [], [], false, '') >> {
throw new InvalidNavigationRequest('PKIX path building failed', new SSLHandshakeException('PKIX path building failed'))
}
thrown(InvalidNavigationRequest)
}

private static VersionSelector createVersionSelector(Map args = [:]) {
new VersionSelector() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon
description += "\nSource: ${loader.description()}"
val pacts = handleWith<List<Pact>> { loader.load(serviceName) }.getOrElse {
val ignoreAnnotation = AnnotationSupport.findAnnotation(context.requiredTestClass, IgnoreNoPactsToVerify::class.java)
if (ignoreAnnotation.isPresent && ignoreAnnotation.get().ignoreIoErrors == "true") {
if (ignoreAnnotation.isPresent &&
(valueResolver != null && valueResolver.resolveValue(ignoreAnnotation.get().ignoreIoErrors) == "true") ||
ignoreAnnotation.get().ignoreIoErrors == "true") {
emptyList()
} else {
throw it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ open class PactBrokerLoader(
overriddenConsumer = consumer
}

@Throws(IOException::class)
override fun load(providerName: String): List<Pact> {
val resolver = setupValueResolver()
return when {
Expand Down

0 comments on commit 32d22e7

Please sign in to comment.