Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Forked from pmlopes/HelloSpec.groovy
Last active March 8, 2016 16:06
Show Gist options
  • Save crazy4groovy/bc0cb0fc3fef28c01dd1 to your computer and use it in GitHub Desktop.
Save crazy4groovy/bc0cb0fc3fef28c01dd1 to your computer and use it in GitHub Desktop.
Spock and Vertx3
import io.vertx.core.Vertx
import spock.lang.Shared
import spock.lang.Specification
import spock.util.concurrent.BlockingVariable
class HelloSpockSpec extends Specification {
@Shared
def Vertx vertx = Vertx.vertx()
def "test async vert.x"() {
setup:
def result = new BlockingVariable()
when:
vertx.setTimer(1l) { timerId ->
result.set('OK')
}
println "wait 1s"
then:
result.get() == 'OK'
}
}
package proxy
import spock.lang.*
import spock.util.concurrent.*
import org.vertx.groovy.core.*
class VertxProxySpec extends Specification {
@Shared Vertx vertx = Vertx.newVertx()
def proxy = new VertxProxy(vertx)
def responseBody = new BlockingVariable<String>(5)
def responseHeaders = new BlockingVariable<Map>(5)
def responseHandler = { resp ->
println "Got response ${resp.statusCode}..."
responseHeaders.set(resp.headers)
def responseBuffer = new StringBuilder()
resp.bodyHandler { body ->
responseBuffer << body.toString()
}
resp.endHandler {
responseBody.set(responseBuffer.toString())
}
}
void setup() {
proxy.start()
}
void cleanup() {
proxy.stop()
}
void 'can proxy an http request'() {
when:
def request = vertx.createHttpClient(port: VertxProxy.HTTP_PORT).get('/betamax/', responseHandler)
request.headers['Host'] = 'freeside.co'
request.end()
then:
responseBody.get().startsWith('<!DOCTYPE html>')
and:
responseHeaders.get()['Via'] == 'Vertx Proxy'
}
void 'can proxy an https request'() {
when:
def request = vertx.createHttpClient(port: VertxProxy.HTTPS_PORT, SSL: true, trustAll: true).get('/robfletcher/betamax/master/readme.md', responseHandler)
request.headers['Host'] = 'raw.github.com'
request.end()
then:
responseHeaders.get()['Via'] == 'Vertx Proxy'
and:
responseBody.get().startsWith('# Betamax')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment