Skip to content

Instantly share code, notes, and snippets.

@dedickinson
Created March 21, 2016 01:39
Show Gist options
  • Save dedickinson/6ad96679a15b24b2e3d3 to your computer and use it in GitHub Desktop.
Save dedickinson/6ad96679a15b24b2e3d3 to your computer and use it in GitHub Desktop.
A sample Spock test using Betamax - fails on SSL call
@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)
}
}
}
@dedickinson
Copy link
Author

This will create a file named "littleproxy_cert" that must be imported into the JRE's certs:

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

I found I had to run this as sudo on OS X with Oracle Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment