1

我一直在尝试配置 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)
        }
    }
}
4

1 回答 1

2

您需要将证书导入 JRE 以使其工作。运行脚本后,将出现两个文件(如注释中所示): - littleproxy_cert - littleproxy_keystore.jks

运行以下命令导入证书:

keytool -import -file littleproxy_cert -alias littleproxy -keystore $JAVA_HOME/jre/lib/security/cacerts

默认密码是changeit。你可能还没有改变它;)

PS Upvoted 准备运行示例-在这里仍然很少见。

于 2016-03-21T08:38:58.883 回答