Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved HTTPS coverage into relevant compatibility specs

  • Loading branch information...
commit 6d7371e7c668a07c9f92dc04220cf04e7d3f270a 1 parent 3059164
Rob Fletcher authored
32 src/test/groovy/co/freeside/betamax/compatibility/HttpClientSpec.groovy
View
@@ -2,9 +2,12 @@ package co.freeside.betamax.compatibility
import co.freeside.betamax.Betamax
import co.freeside.betamax.Recorder
+import co.freeside.betamax.httpclient.BetamaxHttpsSupport
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler
+import co.freeside.betamax.util.server.HelloHandler
+import co.freeside.betamax.util.server.SimpleSecureServer
import org.apache.http.HttpHost
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
@@ -14,19 +17,23 @@ import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Timeout
+import static co.freeside.betamax.TapeMode.WRITE_ONLY
import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA
+import static org.apache.http.HttpStatus.SC_OK
import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY
class HttpClientSpec extends Specification {
@AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
- @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
+ @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot, defaultMode: WRITE_ONLY, sslSupport: true)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
+ @Shared @AutoCleanup('stop') SimpleServer httpsEndpoint = new SimpleSecureServer(5001)
void setupSpec() {
endpoint.start(EchoHandler)
+ httpsEndpoint.start(HelloHandler)
}
@Timeout(10)
@@ -76,4 +83,27 @@ class HttpClientSpec extends Specification {
response.getFirstHeader(VIA)?.value == 'Betamax'
}
+ @Betamax(tape = 'http client spec')
+ void 'proxy can intercept HTTPS requests'() {
+ given:
+ def http = new DefaultHttpClient()
+ BetamaxRoutePlanner.configure(http)
+ BetamaxHttpsSupport.configure(http)
+
+ when: 'an HTTPS request is made'
+ def request = new HttpGet(httpsEndpoint.url)
+ def response = http.execute(request)
+
+ and: 'we read the response body'
+ def responseBytes = new ByteArrayOutputStream()
+ response.entity.writeTo(responseBytes)
+ def responseString = responseBytes.toString('UTF-8')
+
+ then: 'the request is intercepted by the proxy'
+ response.statusLine.statusCode == SC_OK
+ response.getFirstHeader(VIA)?.value == 'Betamax'
+
+ and: 'the response is decoded'
+ responseString == 'Hello World!'
+ }
}
18 src/test/groovy/co/freeside/betamax/compatibility/HttpURLConnectionSpec.groovy
View
@@ -4,23 +4,29 @@ import co.freeside.betamax.Betamax
import co.freeside.betamax.Recorder
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler
+import co.freeside.betamax.util.server.HelloHandler
+import co.freeside.betamax.util.server.SimpleSecureServer
import org.junit.Rule
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Timeout
+import static co.freeside.betamax.TapeMode.WRITE_ONLY
import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA
+import static org.apache.http.HttpStatus.SC_OK
class HttpURLConnectionSpec extends Specification {
@AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
- @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
+ @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot, defaultMode: WRITE_ONLY, sslSupport: true)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
+ @Shared @AutoCleanup('stop') SimpleServer httpsEndpoint = new SimpleSecureServer(5001)
void setupSpec() {
endpoint.start(EchoHandler)
+ httpsEndpoint.start(HelloHandler)
}
@Timeout(10)
@@ -38,4 +44,14 @@ class HttpURLConnectionSpec extends Specification {
connection.disconnect()
}
+ @Betamax(tape = 'http url connection spec', mode = WRITE_ONLY)
+ void 'proxy intercepts HTTPS requests'() {
+ when:
+ HttpURLConnection connection = httpsEndpoint.url.toURL().openConnection()
+
+ then:
+ connection.responseCode == SC_OK
+ connection.getHeaderField(VIA) == 'Betamax'
+ connection.inputStream.text == 'Hello World!'
+ }
}
40 src/test/groovy/co/freeside/betamax/compatibility/WsLiteSpec.groovy
View
@@ -4,11 +4,14 @@ import co.freeside.betamax.Betamax
import co.freeside.betamax.Recorder
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler
+import co.freeside.betamax.util.server.HelloHandler
+import co.freeside.betamax.util.server.SimpleSecureServer
import org.junit.Rule
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
import wslite.rest.RESTClient
+import static co.freeside.betamax.TapeMode.WRITE_ONLY
import static co.freeside.betamax.proxy.jetty.BetamaxProxy.X_BETAMAX
import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK
@@ -17,23 +20,46 @@ import static org.apache.http.HttpHeaders.VIA
class WsLiteSpec extends Specification {
@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
- @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
- @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
- RESTClient http = new RESTClient(endpoint.url)
+ @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot, defaultMode: WRITE_ONLY, sslSupport: true)
+ @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
+ @Shared @AutoCleanup('stop') SimpleServer httpsEndpoint = new SimpleSecureServer(5001)
+
+ void setupSpec() {
+ endpoint.start(EchoHandler)
+ httpsEndpoint.start(HelloHandler)
+ }
@Betamax(tape = 'wslite spec')
void 'can record a connection made with WsLite'() {
- given:
- endpoint.start(EchoHandler)
+ given: 'a properly configured wslite instance'
+ def http = new RESTClient(endpoint.url)
def proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress('localhost', recorder.proxyPort))
- when:
+ when: 'a request is made'
def response = http.get(path: '/', proxy: proxy)
- then:
+ then: 'the request is intercepted'
response.statusCode == HTTP_OK
response.headers[VIA] == 'Betamax'
response.headers[X_BETAMAX] == 'REC'
}
+ @Betamax(tape = 'wslite spec')
+ void 'proxy intercepts HTTPS requests'() {
+ given: 'a properly configured wslite instance'
+ def http = new RESTClient(httpsEndpoint.url)
+ def proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress('localhost', recorder.proxyPort))
+
+ when: 'a request is made'
+ def response = http.get(path: '/', proxy: proxy)
+
+ then: 'the request is intercepted'
+ response.statusCode == HTTP_OK
+ response.headers[VIA] == 'Betamax'
+ response.headers[X_BETAMAX] == 'REC'
+
+ and: 'the response body is decoded'
+ response.contentAsString == 'Hello World!'
+ }
+
}
42 src/test/groovy/co/freeside/betamax/proxy/HttpsSpec.groovy
View
@@ -1,15 +1,19 @@
package co.freeside.betamax.proxy
-import co.freeside.betamax.*
+import co.freeside.betamax.Betamax
+import co.freeside.betamax.Recorder
+import co.freeside.betamax.TapeMode
import co.freeside.betamax.httpclient.BetamaxHttpsSupport
import co.freeside.betamax.proxy.jetty.SimpleServer
-import co.freeside.betamax.util.server.*
+import co.freeside.betamax.util.server.HelloHandler
+import co.freeside.betamax.util.server.SimpleSecureServer
import org.apache.http.client.HttpClient
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.FileUtils.newTempDir
+import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD
import static org.apache.http.HttpHeaders.VIA
import static org.apache.http.HttpStatus.SC_OK
@@ -28,7 +32,7 @@ class HttpsSpec extends Specification {
HttpClient http
void setupSpec() {
- httpEndpoint.start(EchoHandler)
+ httpEndpoint.start(HelloHandler)
httpsEndpoint.start(HelloHandler)
httpUri = httpEndpoint.url.toURI()
@@ -60,39 +64,23 @@ class HttpsSpec extends Specification {
}
@Betamax(tape = 'https spec', mode = TapeMode.WRITE_ONLY)
- void 'proxy can intercept HTTP requests'() {
+ void 'proxy can intercept #scheme requests'() {
when: 'an HTTPS request is made'
- def response = http.execute(new HttpGet(httpEndpoint.url))
+ def request = new HttpGet(httpEndpoint.url)
+ def response = http.execute(request)
then: 'it is intercepted by the proxy'
response.statusLine.statusCode == SC_OK
response.getFirstHeader(VIA)?.value == 'Betamax'
- }
- @Betamax(tape = 'https spec', mode = TapeMode.WRITE_ONLY)
- void 'proxy can intercept HTTPS requests made with HttpClient'() {
- when: 'an HTTPS request is made'
- def response = http.execute(new HttpGet(httpsEndpoint.url))
- def responseBytes = new ByteArrayOutputStream()
- response.entity.writeTo(responseBytes)
- def responseString = responseBytes.toString('UTF-8')
+ and: 'the response body is readable'
+ response.entity.content.text == HELLO_WORLD
- then: 'it is intercepted by the proxy'
- response.statusLine.statusCode == SC_OK
- response.getFirstHeader(VIA)?.value == 'Betamax'
- responseString == 'Hello World!'
+ where:
+ url << [httpEndpoint.url, httpsEndpoint.url]
+ scheme = url.toURI().scheme
}
- @Betamax(tape = 'https spec', mode = TapeMode.WRITE_ONLY)
- void 'proxy can intercept HTTPS requests made with UrlConnection'() {
- when:
- def connection = httpsEndpoint.url.toURL().openConnection()
-
- then:
- connection.responseCode == SC_OK
- connection.getHeaderField(VIA) == 'Betamax'
- connection.inputStream.text == 'Hello World!'
- }
}
Please sign in to comment.
Something went wrong with that request. Please try again.