Permalink
Browse files

#40 added `useProxy` property to Recorder to allow proxy to be disabled

  • Loading branch information...
Rob Fletcher
Rob Fletcher committed Oct 23, 2012
1 parent f54310e commit 001236d12bf842c38122df85541cf669bcda4ccb
@@ -26,6 +26,7 @@ import org.junit.runners.model.*
import static TapeMode.READ_WRITE
import static co.freeside.betamax.MatchRule.*
import static java.util.Collections.EMPTY_MAP
+
/**
* This is the main interface to the Betamax proxy. It allows control of Betamax configuration and inserting and
* ejecting `Tape` instances. The class can also be used as a _JUnit @Rule_ allowing tests annotated with `@Betamax` to
@@ -98,18 +99,14 @@ class Recorder implements MethodRule {
*/
boolean sslSupport = false
+ /**
+ * If set to true the proxy is started before and stopped after each annotated test method.
+ */
+ boolean useProxy = true
- String getProxyHost() {
- proxy.url.toURI().host
- }
-
- int getHttpsProxyPort() {
- //proxyPort + 1
- proxyPort
- }
private StorableTape tape
- HttpInterceptor proxy = new ProxyServer(this)
+ private final ProxyServer interceptor = new ProxyServer(this)
/**
* Inserts a tape either creating a new one or loading an existing file from `tapeRoot`.
@@ -170,10 +167,6 @@ class Recorder implements MethodRule {
}
}
- Proxy getProxy() {
- new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.localHost.hostAddress, proxyPort))
- }
-
@Override
Statement apply(Statement statement, FrameworkMethod method, Object target) {
def annotation = method.getAnnotation(Betamax)
@@ -192,15 +185,31 @@ class Recorder implements MethodRule {
}
}
+ /**
+ * @return the hostname or address where the proxy will run.
+ */
+ String getProxyHost() {
+ interceptor.host
+ }
+
+ /**
+ * @return a `java.net.Proxy` instance configured to point to the Betamax proxy.
+ */
+ Proxy getProxy() {
+ new Proxy(Proxy.Type.HTTP, new InetSocketAddress(interceptor.host, interceptor.port))
+ }
+
private void startProxy(String tapeName, Map arguments) {
- if (!proxy.running) {
- proxy.start()
+ if (useProxy && !interceptor.running) {
+ interceptor.start()
}
insertTape(tapeName, arguments)
}
private void stopProxy() {
- proxy.stop()
+ if (useProxy) {
+ interceptor.stop()
+ }
ejectTape()
}
@@ -97,7 +97,7 @@ class ProxyServer extends SimpleServer implements HttpInterceptor {
)
}
- void overrideProxySettings() {
+ private void overrideProxySettings() {
def proxyHost = InetAddress.localHost.hostAddress
def nonProxyHosts = recorder.ignoreHosts as Set
if (recorder.ignoreLocalhost) {
@@ -106,17 +106,17 @@ class ProxyServer extends SimpleServer implements HttpInterceptor {
proxyOverrider.activate proxyHost, port, nonProxyHosts
}
- void restoreOriginalProxySettings() {
+ private void restoreOriginalProxySettings() {
proxyOverrider.deactivateAll()
}
- void overrideSSLSettings() {
+ private void overrideSSLSettings() {
if (recorder.sslSupport) {
sslOverrider.activate()
}
}
- void restoreOriginalSSLSettings() {
+ private void restoreOriginalSSLSettings() {
if (recorder.sslSupport) {
sslOverrider.deactivate()
}
@@ -26,7 +26,7 @@ class SimpleServer extends AbstractLifeCycleListener {
static final int DEFAULT_PORT = 5000
- protected final String host
+ final String host
protected int port
private Server server
private CountDownLatch startedLatch
@@ -20,7 +20,7 @@ import static org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED
class BetamaxHttpClientSpec extends Specification {
@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
- @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot, proxy: Mock(HttpInterceptor))
+ @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
def http = new BetamaxHttpClient(recorder)
@@ -53,7 +53,7 @@ class HttpsSpec extends Specification {
and:
def proxyURI = "${scheme}://${proxy.address()}".toURI()
InetAddress.getByName(proxyURI.host) == InetAddress.getByName(recorder.proxyHost)
- proxyURI.port == scheme == 'https' ? recorder.httpsProxyPort : recorder.proxyPort
+ proxyURI.port == recorder.proxyPort
where:
uri << [httpUri, httpsUri]
@@ -0,0 +1,44 @@
+package co.freeside.betamax.proxy
+
+import co.freeside.betamax.*
+import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.util.server.HelloHandler
+import org.apache.http.client.methods.HttpGet
+import org.apache.http.impl.client.SystemDefaultHttpClient
+import org.junit.Rule
+import spock.lang.*
+import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD
+import static java.net.HttpURLConnection.HTTP_OK
+import static org.apache.http.HttpHeaders.VIA
+
+@Issue('https://github.com/robfletcher/betamax/issues/40')
+class ProxyOptionalitySpec extends Specification {
+
+ @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
+ @Rule Recorder recorder = new Recorder(useProxy: false)
+
+ void setupSpec() {
+ endpoint.start(HelloHandler)
+ }
+
+ @Betamax(tape = 'proxy optionality')
+ void 'the Betamax proxy does not start if not used by the recorder'() {
+ given:
+ def http = new SystemDefaultHttpClient()
+
+ and:
+ def request = new HttpGet(endpoint.url)
+
+ when:
+ def response = http.execute(request)
+
+ then:
+ response.statusLine.statusCode == HTTP_OK
+ response.entity.content.text == HELLO_WORLD
+
+ and:
+ !response.getFirstHeader(VIA)
+ !response.getFirstHeader('X-Betamax')
+ }
+
+}

0 comments on commit 001236d

Please sign in to comment.