我一直在尝试配置 Betamax v2.0.0-alpha-1 来模拟我的 spock 测试中的 HTTP(S) 调用。对非 SSL 站点的调用有效,但对 HTTPS 站点的调用会导致以下异常:
javax.ws.rs.ProcessingException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
我已将代码归结为如下所示,调用groovy BetamaxTestSpec.groovy
应该会给您异常。正如您在代码中看到的,我使用的是 jersey-client 库。
要点:https ://gist.github.com/dedickinson/6ad96679a15b24b2e3d3
代码:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('software.betamax:betamax-junit:2.0.0-alpha-1')
@Grab('org.glassfish.jersey.core:jersey-client:2.22.1')
import org.junit.Rule
import software.betamax.ProxyConfiguration
import software.betamax.TapeMode
import software.betamax.junit.Betamax
import software.betamax.junit.RecorderRule
import spock.lang.Specification
import javax.ws.rs.client.ClientBuilder
import javax.ws.rs.core.MediaType
import groovy.json.JsonSlurper
class BetamaxTestSpec extends Specification {
@Rule
RecorderRule recorderRule = new RecorderRule(ProxyConfiguration.builder()
.sslEnabled(true)
.build())
@Betamax(tape = 'jCenterKeywordQuery.tape', mode = TapeMode.WRITE_ONLY)
def "Test basic keyword query with JCenter"() {
given:
def searcher = new Searcher()
def result = searcher.searchJCenter('groovy*')
expect:
1 == 1
}
@Betamax(tape = 'mvnKeywordQuery.tape', mode = TapeMode.WRITE_ONLY)
def "Test basic keyword query with Maven Central"() {
given:
def searcher = new Searcher()
def result = searcher.searchMavenCentral('groovy')
expect:
1 == 1
}
class Searcher {
def searchJCenter(qry) {
new JsonSlurper().parseText ClientBuilder.newClient().
target('https://api.bintray.com/search/packages/maven/'.toURI()).
queryParam('q', qry).
request(MediaType.APPLICATION_JSON_TYPE).get(String)
}
def searchMavenCentral(qry) {
new JsonSlurper().parseText ClientBuilder.newClient().
target('http://search.maven.org/solrsearch/select'.toURI()).
queryParam('q', qry).
queryParam('rows', 20).
queryParam('wt', 'json').
request().
get(String)
}
}
}