-
-
Save crazy4groovy/bc0cb0fc3fef28c01dd1 to your computer and use it in GitHub Desktop.
Spock and Vertx3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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