Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' into httpclient

  • Loading branch information...
commit d46b2520ddbba9bbaea3584fcc3b945f12631c5b 2 parents 8cf3fd9 + 837b44d
Rob Fletcher authored
View
4 src/docs/index.md
@@ -204,11 +204,11 @@ Under Java 6 it is not possible to proxy connections to URLs whose host is `loca
### Apache HttpClient
-By default [Apache _HttpClient_][httpclient] takes no notice of Java's HTTP proxy settings. The Betamax proxy can only intercept traffic from HttpClient if the client instance is set up to use a [`ProxySelectorRoutePlanner`][proxyselector]. When Betamax is not active this will mean HttpClient traffic will be routed via the default proxy configured in Java (if any).
+The default implementations of [Apache _HttpClient_][httpclient] takes no notice of Java's HTTP proxy settings. The Betamax proxy can only intercept traffic from HttpClient if the client instance is set up to use a [`ProxySelectorRoutePlanner`][proxyselector]. When Betamax is not active this will mean HttpClient traffic will be routed via the default proxy configured in Java (if any).
In a dependency injection context such as a [Grails][grails] app you can just inject a proxy-configured _HttpClient_ instance into your class-under-test.
-Betamax provides a convenient `HttpRoutePlanner` implementation that you can use to configure your _HttpClient_ instance.
+The _HttpClient_ library provides an implementation called [SystemDefaultHttpClient](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/SystemDefaultHttpClient.html) that _does_ use the JVM proxy settings. Ideally you can use that. In addition, Betamax provides a convenient `HttpRoutePlanner` implementation that you can use to configure instances of other _HttpClient_ types.
#### Configuring HttpClient
View
5 src/test/groovy/co/freeside/betamax/AnnotationSpec.groovy
@@ -1,7 +1,7 @@
package co.freeside.betamax
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import org.junit.Rule
@@ -20,8 +20,7 @@ class AnnotationSpec extends Specification {
RESTClient http
void setup() {
- http = new RESTClient(endpoint.url)
- BetamaxRoutePlanner.configure(http.client)
+ http = new BetamaxRESTClient(endpoint.url)
}
void 'no tape is inserted if there is no annotation on the feature'() {
View
5 src/test/groovy/co/freeside/betamax/AnnotationTest.groovy
@@ -1,7 +1,7 @@
package co.freeside.betamax
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import org.junit.*
@@ -18,8 +18,7 @@ class AnnotationTest {
@Before
void initRestClient() {
- http = new RESTClient(endpoint.url)
- BetamaxRoutePlanner.configure(http.client)
+ http = new BetamaxRESTClient(endpoint.url)
}
@After
View
5 src/test/groovy/co/freeside/betamax/LocalhostSpec.groovy
@@ -1,7 +1,7 @@
package co.freeside.betamax
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.*
import org.junit.Rule
@@ -21,10 +21,9 @@ class LocalhostSpec extends Specification {
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
- @Shared RESTClient http = new RESTClient()
+ @Shared RESTClient http = new BetamaxRESTClient()
void setupSpec() {
- BetamaxRoutePlanner.configure(http.client)
endpoint.start(EchoHandler)
}
View
12 src/test/groovy/co/freeside/betamax/SmokeSpec.groovy
@@ -1,12 +1,10 @@
package co.freeside.betamax
import co.freeside.betamax.httpclient.BetamaxHttpsSupport
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
-import groovyx.net.http.HttpResponseDecorator
-import groovyx.net.http.RESTClient
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
+import groovyx.net.http.*
import org.junit.Rule
import spock.lang.*
-
import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA
@@ -15,11 +13,7 @@ class SmokeSpec extends Specification {
@Rule Recorder recorder = new Recorder(sslSupport: true)
- @Shared RESTClient http = new RESTClient()
-
- void setupSpec() {
- BetamaxRoutePlanner.configure(http.client)
- }
+ @Shared RESTClient http = new BetamaxRESTClient()
@Betamax(tape = 'smoke spec')
void '#type response data'() {
View
7 src/test/groovy/co/freeside/betamax/proxy/HttpsSpec.groovy
@@ -1,12 +1,12 @@
package co.freeside.betamax.proxy
import co.freeside.betamax.*
-import co.freeside.betamax.httpclient.*
+import co.freeside.betamax.httpclient.BetamaxHttpsSupport
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.*
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
-import org.apache.http.impl.client.DefaultHttpClient
+import org.apache.http.impl.client.SystemDefaultHttpClient
import org.junit.Rule
import spock.lang.*
import static co.freeside.betamax.util.FileUtils.newTempDir
@@ -36,9 +36,8 @@ class HttpsSpec extends Specification {
}
void setup() {
- http = new DefaultHttpClient()
+ http = new SystemDefaultHttpClient()
BetamaxHttpsSupport.configure(http)
- BetamaxRoutePlanner.configure(http)
}
@Betamax(tape = 'https spec')
View
5 src/test/groovy/co/freeside/betamax/proxy/IgnoreHostsSpec.groovy
@@ -1,8 +1,8 @@
package co.freeside.betamax.proxy
import co.freeside.betamax.Recorder
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.*
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import spock.lang.*
@@ -24,8 +24,7 @@ class IgnoreHostsSpec extends Specification {
}
void setup() {
- http = new RESTClient()
- BetamaxRoutePlanner.configure(http.client)
+ http = new BetamaxRESTClient()
recorder.insertTape('ignore hosts spec')
}
View
21 src/test/groovy/co/freeside/betamax/proxy/NoTapeSpec.groovy
@@ -1,17 +1,11 @@
package co.freeside.betamax.proxy
import co.freeside.betamax.Recorder
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
-import co.freeside.betamax.proxy.jetty.ProxyServer
-import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.proxy.jetty.*
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
-import groovyx.net.http.HttpResponseException
-import groovyx.net.http.RESTClient
-import spock.lang.AutoCleanup
-import spock.lang.Issue
-import spock.lang.Shared
-import spock.lang.Specification
-
+import groovyx.net.http.*
+import spock.lang.*
import static java.net.HttpURLConnection.HTTP_FORBIDDEN
@Issue('https://github.com/robfletcher/betamax/issues/18')
@@ -20,18 +14,13 @@ class NoTapeSpec extends Specification {
@Shared Recorder recorder = new Recorder()
@Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
- RESTClient http
+ RESTClient http = new BetamaxRESTClient(endpoint.url)
void setupSpec() {
proxy.start()
endpoint.start(EchoHandler)
}
- void setup() {
- http = new RESTClient(endpoint.url)
- BetamaxRoutePlanner.configure(http.client)
- }
-
void 'an error is returned if the proxy intercepts a request when no tape is inserted'() {
when:
http.get(path: '/')
View
8 src/test/groovy/co/freeside/betamax/proxy/ProxyNetworkCommsSpec.groovy
@@ -1,8 +1,8 @@
package co.freeside.betamax.proxy
import co.freeside.betamax.*
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.*
import org.apache.commons.httpclient.*
@@ -45,8 +45,7 @@ class ProxyNetworkCommsSpec extends Specification {
@Betamax(tape = 'proxy network comms spec')
void 'proxy intercepts HTTPClient connections when using ProxySelectorRoutePlanner'() {
given:
- def http = new RESTClient(endpoint.url)
- BetamaxRoutePlanner.configure(http.client)
+ def http = new BetamaxRESTClient(endpoint.url)
when:
def response = http.get(path: '/')
@@ -107,8 +106,7 @@ class ProxyNetworkCommsSpec extends Specification {
@Betamax(tape = 'proxy network comms spec')
void 'proxy handles #method requests'() {
given:
- def http = new RESTClient(endpoint.url)
- BetamaxRoutePlanner.configure(http.client)
+ def http = new BetamaxRESTClient(endpoint.url)
when:
def response = http."$method"(path: '/')
View
9 src/test/groovy/co/freeside/betamax/proxy/ProxyRecordAndPlaybackSpec.groovy
@@ -1,8 +1,8 @@
package co.freeside.betamax.proxy
import co.freeside.betamax.Recorder
-import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.*
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import org.yaml.snakeyaml.Yaml
@@ -17,18 +17,13 @@ class ProxyRecordAndPlaybackSpec extends Specification {
@Shared @AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
- RESTClient http
+ RESTClient http = new BetamaxRESTClient(endpoint.url)
void setupSpec() {
recorder.insertTape('proxy record and playback spec')
proxy.start()
}
- void setup() {
- http = new RESTClient(endpoint.url)
- BetamaxRoutePlanner.configure(http.client)
- }
-
@Timeout(10)
void 'proxy makes a real HTTP request the first time it gets a request for a URI'() {
given:
View
5 src/test/groovy/co/freeside/betamax/proxy/ProxyTimeoutSpec.groovy
@@ -3,6 +3,7 @@ package co.freeside.betamax.proxy
import co.freeside.betamax.*
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.SlowHandler
import groovyx.net.http.*
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler
@@ -18,11 +19,9 @@ class ProxyTimeoutSpec extends Specification {
@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
- RESTClient http
+ RESTClient http = new BetamaxRESTClient(endpoint.url)
void setup() {
- http = new RESTClient(endpoint.url)
- BetamaxRoutePlanner.configure(http.client)
http.client.httpRequestRetryHandler = new DefaultHttpRequestRetryHandler(0, false)
}
View
5 src/test/groovy/co/freeside/betamax/recorder/RequestMatchingSpec.groovy
@@ -2,6 +2,7 @@ package co.freeside.betamax.recorder
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import groovyx.net.http.RESTClient
import spock.lang.*
import static co.freeside.betamax.MatchRule.host
@@ -13,12 +14,10 @@ class RequestMatchingSpec extends Specification {
@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Shared Recorder recorder = new Recorder(tapeRoot: tapeRoot)
- @Shared RESTClient http = new RESTClient()
+ @Shared RESTClient http = new BetamaxRESTClient()
void setupSpec() {
tapeRoot.mkdirs()
-
- BetamaxRoutePlanner.configure(http.client)
}
@Unroll('#method request for #uri returns "#responseText"')
View
8 src/test/groovy/co/freeside/betamax/recorder/TapeModeSpec.groovy
@@ -3,6 +3,7 @@ package co.freeside.betamax.recorder
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.*
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.*
import spock.lang.*
@@ -16,7 +17,7 @@ class TapeModeSpec extends Specification {
@Shared Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
- RESTClient http
+ RESTClient http = new BetamaxRESTClient()
void setupSpec() {
tapeRoot.mkdirs()
@@ -24,11 +25,6 @@ class TapeModeSpec extends Specification {
endpoint.start(EchoHandler)
}
- void setup() {
- http = new RESTClient()
- BetamaxRoutePlanner.configure(http.client)
- }
-
void cleanup() {
recorder.ejectTape()
}
View
7 src/test/groovy/co/freeside/betamax/tape/PostBodySpec.groovy
@@ -5,6 +5,7 @@ import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
+import org.apache.http.impl.client.SystemDefaultHttpClient
import org.junit.Rule
import org.yaml.snakeyaml.Yaml
import spock.lang.*
@@ -17,11 +18,7 @@ class PostBodySpec extends Specification {
@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
- private DefaultHttpClient httpClient = new DefaultHttpClient()
-
- void setup() {
- BetamaxRoutePlanner.configure(httpClient)
- }
+ private DefaultHttpClient httpClient = new SystemDefaultHttpClient()
void 'post body is stored on tape when using UrlConnection'() {
given:
View
15 src/test/groovy/co/freeside/betamax/util/httpbuilder/BetamaxRestClient.groovy
@@ -0,0 +1,15 @@
+package co.freeside.betamax.util.httpbuilder
+
+import groovy.transform.InheritConstructors
+import groovyx.net.http.RESTClient
+import org.apache.http.impl.client.*
+import org.apache.http.params.HttpParams
+
+@InheritConstructors
+class BetamaxRESTClient extends RESTClient {
+
+ @Override
+ protected AbstractHttpClient createClient(HttpParams params) {
+ new SystemDefaultHttpClient(params)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.