Skip to content
This repository
Browse code

Broke client compatibility tests out into individual specs

  • Loading branch information...
commit 3059164710356e7c9055bc1f1cbb86d4136dce7c 1 parent d4fbdef
authored October 12, 2012
3  build.gradle
@@ -38,7 +38,8 @@ dependencies {
38 38
 		exclude module: 'groovy-all'
39 39
 		exclude module: 'junit-dep'
40 40
 	}
41  
-	testCompile 'commons-httpclient:commons-httpclient:3.1'
  41
+	testCompile 'commons-httpclient:commons-httpclient:3.1',
  42
+				'com.github.groovy-wslite:groovy-wslite:0.7.1'
42 43
 
43 44
 	testRuntime 'cglib:cglib-nodep:2.2.2'
44 45
 }
90  src/test/groovy/co/freeside/betamax/compatibility/HttpBuilderSpec.groovy
... ...
@@ -0,0 +1,90 @@
  1
+package co.freeside.betamax.compatibility
  2
+
  3
+import co.freeside.betamax.Betamax
  4
+import co.freeside.betamax.Recorder
  5
+import co.freeside.betamax.httpclient.BetamaxRoutePlanner
  6
+import co.freeside.betamax.proxy.jetty.SimpleServer
  7
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
  8
+import co.freeside.betamax.util.server.EchoHandler
  9
+import groovyx.net.http.HttpURLClient
  10
+import groovyx.net.http.RESTClient
  11
+import org.apache.http.HttpHost
  12
+import org.junit.Rule
  13
+import spock.lang.AutoCleanup
  14
+import spock.lang.Shared
  15
+import spock.lang.Specification
  16
+import spock.lang.Timeout
  17
+import static co.freeside.betamax.util.FileUtils.newTempDir
  18
+import static java.net.HttpURLConnection.HTTP_OK
  19
+import static org.apache.http.HttpHeaders.VIA
  20
+import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY
  21
+
  22
+class HttpBuilderSpec extends Specification {
  23
+
  24
+	@AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
  25
+	@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
  26
+	@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
  27
+
  28
+	void setupSpec() {
  29
+		endpoint.start(EchoHandler)
  30
+	}
  31
+
  32
+	@Timeout(10)
  33
+	@Betamax(tape = 'http builder spec')
  34
+	void 'proxy intercepts HTTPClient connections when using ProxySelectorRoutePlanner'() {
  35
+		given:
  36
+		def http = new RESTClient(endpoint.url)
  37
+		BetamaxRoutePlanner.configure(http.client)
  38
+
  39
+		when:
  40
+		def response = http.get(path: '/')
  41
+
  42
+		then:
  43
+		response.status == HTTP_OK
  44
+		response.getFirstHeader(VIA)?.value == 'Betamax'
  45
+	}
  46
+
  47
+	@Timeout(10)
  48
+	@Betamax(tape = 'http builder spec')
  49
+	void 'proxy intercepts HTTPClient connections when explicitly told to'() {
  50
+		given:
  51
+		def http = new RESTClient(endpoint.url)
  52
+		http.client.params.setParameter(DEFAULT_PROXY, new HttpHost('localhost', recorder.proxyPort, 'http'))
  53
+
  54
+		when:
  55
+		def response = http.get(path: '/')
  56
+
  57
+		then:
  58
+		response.status == HTTP_OK
  59
+		response.getFirstHeader(VIA)?.value == 'Betamax'
  60
+	}
  61
+
  62
+	@Timeout(10)
  63
+	@Betamax(tape = 'http builder spec')
  64
+	void 'proxy intercepts HttpURLClient connections'() {
  65
+		given:
  66
+		def http = new HttpURLClient(url: endpoint.url)
  67
+
  68
+		when:
  69
+		def response = http.request(path: '/')
  70
+
  71
+		then:
  72
+		response.status == HTTP_OK
  73
+		response.getFirstHeader(VIA)?.value == 'Betamax'
  74
+	}
  75
+
  76
+	@Timeout(10)
  77
+	@Betamax(tape = 'http builder spec')
  78
+	void 'proxy automatically intercepts connections when the underlying client is a SystemDefaultHttpClient'() {
  79
+		given:
  80
+		def http = new BetamaxRESTClient(endpoint.url)
  81
+
  82
+		when:
  83
+		def response = http.get(path: '/')
  84
+
  85
+		then:
  86
+		response.status == HTTP_OK
  87
+		response.getFirstHeader(VIA)?.value == 'Betamax'
  88
+	}
  89
+
  90
+}
47  src/test/groovy/co/freeside/betamax/compatibility/HttpClient3Spec.groovy
... ...
@@ -0,0 +1,47 @@
  1
+package co.freeside.betamax.compatibility
  2
+
  3
+import co.freeside.betamax.Betamax
  4
+import co.freeside.betamax.Recorder
  5
+import co.freeside.betamax.proxy.jetty.SimpleServer
  6
+import co.freeside.betamax.util.server.EchoHandler
  7
+import org.apache.commons.httpclient.HttpClient
  8
+import org.apache.commons.httpclient.ProxyHost
  9
+import org.apache.commons.httpclient.methods.GetMethod
  10
+import org.junit.Rule
  11
+import spock.lang.AutoCleanup
  12
+import spock.lang.Shared
  13
+import spock.lang.Specification
  14
+import spock.lang.Timeout
  15
+import static co.freeside.betamax.util.FileUtils.newTempDir
  16
+import static java.net.HttpURLConnection.HTTP_OK
  17
+import static org.apache.http.HttpHeaders.VIA
  18
+
  19
+class HttpClient3Spec extends Specification {
  20
+
  21
+	@AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
  22
+	@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
  23
+	@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
  24
+
  25
+	void setupSpec() {
  26
+		endpoint.start(EchoHandler)
  27
+	}
  28
+
  29
+	@Timeout(10)
  30
+	@Betamax(tape = 'http client 3 spec')
  31
+	void 'proxy intercepts HTTPClient 3.x connections'() {
  32
+		given:
  33
+		def client = new HttpClient()
  34
+		client.hostConfiguration.proxyHost = new ProxyHost('localhost', 5555)
  35
+
  36
+		and:
  37
+		def request = new GetMethod(endpoint.url)
  38
+
  39
+		when:
  40
+		def status = client.executeMethod(request)
  41
+
  42
+		then:
  43
+		status == HTTP_OK
  44
+		request.getResponseHeader(VIA)?.value == 'Betamax'
  45
+	}
  46
+
  47
+}
79  src/test/groovy/co/freeside/betamax/compatibility/HttpClientSpec.groovy
... ...
@@ -0,0 +1,79 @@
  1
+package co.freeside.betamax.compatibility
  2
+
  3
+import co.freeside.betamax.Betamax
  4
+import co.freeside.betamax.Recorder
  5
+import co.freeside.betamax.httpclient.BetamaxRoutePlanner
  6
+import co.freeside.betamax.proxy.jetty.SimpleServer
  7
+import co.freeside.betamax.util.server.EchoHandler
  8
+import org.apache.http.HttpHost
  9
+import org.apache.http.client.methods.HttpGet
  10
+import org.apache.http.impl.client.DefaultHttpClient
  11
+import org.apache.http.impl.client.SystemDefaultHttpClient
  12
+import org.junit.Rule
  13
+import spock.lang.AutoCleanup
  14
+import spock.lang.Shared
  15
+import spock.lang.Specification
  16
+import spock.lang.Timeout
  17
+import static co.freeside.betamax.util.FileUtils.newTempDir
  18
+import static java.net.HttpURLConnection.HTTP_OK
  19
+import static org.apache.http.HttpHeaders.VIA
  20
+import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY
  21
+
  22
+class HttpClientSpec extends Specification {
  23
+
  24
+	@AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
  25
+	@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
  26
+	@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
  27
+
  28
+	void setupSpec() {
  29
+		endpoint.start(EchoHandler)
  30
+	}
  31
+
  32
+	@Timeout(10)
  33
+	@Betamax(tape = 'http client spec')
  34
+	void 'proxy intercepts HTTPClient connections when using ProxySelectorRoutePlanner'() {
  35
+		given:
  36
+		def http = new DefaultHttpClient()
  37
+		BetamaxRoutePlanner.configure(http)
  38
+
  39
+		when:
  40
+		def request = new HttpGet(endpoint.url)
  41
+		def response = http.execute(request)
  42
+
  43
+		then:
  44
+		response.statusLine.statusCode == HTTP_OK
  45
+		response.getFirstHeader(VIA)?.value == 'Betamax'
  46
+	}
  47
+
  48
+	@Timeout(10)
  49
+	@Betamax(tape = 'http client spec')
  50
+	void 'proxy intercepts HTTPClient connections when explicitly told to'() {
  51
+		given:
  52
+		def http = new DefaultHttpClient()
  53
+		http.params.setParameter(DEFAULT_PROXY, new HttpHost('localhost', recorder.proxyPort, 'http'))
  54
+
  55
+		when:
  56
+		def request = new HttpGet(endpoint.url)
  57
+		def response = http.execute(request)
  58
+
  59
+		then:
  60
+		response.statusLine.statusCode == HTTP_OK
  61
+		response.getFirstHeader(VIA)?.value == 'Betamax'
  62
+	}
  63
+
  64
+	@Timeout(10)
  65
+	@Betamax(tape = 'http client spec')
  66
+	void 'proxy automatically intercepts SystemDefaultHttpClient connections'() {
  67
+		given:
  68
+		def http = new SystemDefaultHttpClient()
  69
+
  70
+		when:
  71
+		def request = new HttpGet(endpoint.url)
  72
+		def response = http.execute(request)
  73
+
  74
+		then:
  75
+		response.statusLine.statusCode == HTTP_OK
  76
+		response.getFirstHeader(VIA)?.value == 'Betamax'
  77
+	}
  78
+
  79
+}
41  src/test/groovy/co/freeside/betamax/compatibility/HttpURLConnectionSpec.groovy
... ...
@@ -0,0 +1,41 @@
  1
+package co.freeside.betamax.compatibility
  2
+
  3
+import co.freeside.betamax.Betamax
  4
+import co.freeside.betamax.Recorder
  5
+import co.freeside.betamax.proxy.jetty.SimpleServer
  6
+import co.freeside.betamax.util.server.EchoHandler
  7
+import org.junit.Rule
  8
+import spock.lang.AutoCleanup
  9
+import spock.lang.Shared
  10
+import spock.lang.Specification
  11
+import spock.lang.Timeout
  12
+import static co.freeside.betamax.util.FileUtils.newTempDir
  13
+import static java.net.HttpURLConnection.HTTP_OK
  14
+import static org.apache.http.HttpHeaders.VIA
  15
+
  16
+class HttpURLConnectionSpec extends Specification {
  17
+
  18
+	@AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
  19
+	@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
  20
+	@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
  21
+
  22
+	void setupSpec() {
  23
+		endpoint.start(EchoHandler)
  24
+	}
  25
+
  26
+	@Timeout(10)
  27
+	@Betamax(tape = 'http url connection spec')
  28
+	void 'proxy intercepts URL connections'() {
  29
+		given:
  30
+		HttpURLConnection connection = new URL(endpoint.url).openConnection()
  31
+		connection.connect()
  32
+
  33
+		expect:
  34
+		connection.responseCode == HTTP_OK
  35
+		connection.getHeaderField(VIA) == 'Betamax'
  36
+
  37
+		cleanup:
  38
+		connection.disconnect()
  39
+	}
  40
+
  41
+}
39  src/test/groovy/co/freeside/betamax/compatibility/WsLiteSpec.groovy
... ...
@@ -0,0 +1,39 @@
  1
+package co.freeside.betamax.compatibility
  2
+
  3
+import co.freeside.betamax.Betamax
  4
+import co.freeside.betamax.Recorder
  5
+import co.freeside.betamax.proxy.jetty.SimpleServer
  6
+import co.freeside.betamax.util.server.EchoHandler
  7
+import org.junit.Rule
  8
+import spock.lang.AutoCleanup
  9
+import spock.lang.Shared
  10
+import spock.lang.Specification
  11
+import wslite.rest.RESTClient
  12
+import static co.freeside.betamax.proxy.jetty.BetamaxProxy.X_BETAMAX
  13
+import static co.freeside.betamax.util.FileUtils.newTempDir
  14
+import static java.net.HttpURLConnection.HTTP_OK
  15
+import static org.apache.http.HttpHeaders.VIA
  16
+
  17
+class WsLiteSpec extends Specification {
  18
+
  19
+	@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
  20
+	@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
  21
+	@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
  22
+	RESTClient http = new RESTClient(endpoint.url)
  23
+
  24
+	@Betamax(tape = 'wslite spec')
  25
+	void 'can record a connection made with WsLite'() {
  26
+		given:
  27
+		endpoint.start(EchoHandler)
  28
+		def proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress('localhost', recorder.proxyPort))
  29
+
  30
+		when:
  31
+		def response = http.get(path: '/', proxy: proxy)
  32
+
  33
+		then:
  34
+		response.statusCode == HTTP_OK
  35
+		response.headers[VIA] == 'Betamax'
  36
+		response.headers[X_BETAMAX] == 'REC'
  37
+	}
  38
+
  39
+}
125  src/test/groovy/co/freeside/betamax/proxy/ProxyNetworkCommsSpec.groovy
... ...
@@ -1,125 +0,0 @@
1  
-package co.freeside.betamax.proxy
2  
-
3  
-import co.freeside.betamax.*
4  
-import co.freeside.betamax.proxy.jetty.SimpleServer
5  
-import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
6  
-import co.freeside.betamax.util.server.EchoHandler
7  
-import groovyx.net.http.*
8  
-import org.apache.commons.httpclient.*
9  
-import org.apache.commons.httpclient.methods.GetMethod
10  
-import org.apache.http.HttpHost
11  
-import org.junit.Rule
12  
-import spock.lang.*
13  
-import static co.freeside.betamax.util.FileUtils.newTempDir
14  
-import static java.net.HttpURLConnection.HTTP_OK
15  
-import static org.apache.http.HttpHeaders.VIA
16  
-import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY
17  
-
18  
-@Unroll
19  
-class ProxyNetworkCommsSpec extends Specification {
20  
-
21  
-    @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
22  
-    @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
23  
-    @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
24  
-
25  
-    void setupSpec() {
26  
-        endpoint.start(EchoHandler)
27  
-    }
28  
-
29  
-    @Timeout(10)
30  
-	@Betamax(tape = 'proxy network comms spec')
31  
-    void 'proxy intercepts URL connections'() {
32  
-        given:
33  
-        HttpURLConnection connection = new URL(endpoint.url).openConnection()
34  
-        connection.connect()
35  
-
36  
-        expect:
37  
-        connection.responseCode == HTTP_OK
38  
-        connection.getHeaderField(VIA) == 'Betamax'
39  
-
40  
-        cleanup:
41  
-        connection.disconnect()
42  
-    }
43  
-
44  
-    @Timeout(10)
45  
-	@Betamax(tape = 'proxy network comms spec')
46  
-    void 'proxy intercepts HTTPClient connections when using ProxySelectorRoutePlanner'() {
47  
-        given:
48  
-        def http = new BetamaxRESTClient(endpoint.url)
49  
-
50  
-        when:
51  
-        def response = http.get(path: '/')
52  
-
53  
-        then:
54  
-        response.status == HTTP_OK
55  
-        response.getFirstHeader(VIA)?.value == 'Betamax'
56  
-    }
57  
-
58  
-    @Timeout(10)
59  
-	@Betamax(tape = 'proxy network comms spec')
60  
-    void 'proxy intercepts HTTPClient connections when explicitly told to'() {
61  
-        given:
62  
-        def http = new RESTClient(endpoint.url)
63  
-        http.client.params.setParameter(DEFAULT_PROXY, new HttpHost('localhost', recorder.proxyPort, 'http'))
64  
-
65  
-        when:
66  
-        def response = http.get(path: '/')
67  
-
68  
-        then:
69  
-        response.status == HTTP_OK
70  
-        response.getFirstHeader(VIA)?.value == 'Betamax'
71  
-    }
72  
-
73  
-    @Timeout(10)
74  
-	@Betamax(tape = 'proxy network comms spec')
75  
-    void 'proxy intercepts HttpURLClient connections'() {
76  
-        given:
77  
-        def http = new HttpURLClient(url: endpoint.url)
78  
-
79  
-        when:
80  
-        def response = http.request(path: '/')
81  
-
82  
-        then:
83  
-        response.status == HTTP_OK
84  
-        response.getFirstHeader(VIA)?.value == 'Betamax'
85  
-    }
86  
-
87  
-    @Timeout(10)
88  
-	@Betamax(tape = 'proxy network comms spec')
89  
-    void 'proxy intercepts HTTPClient 3.x connections'() {
90  
-        given:
91  
-        def client = new HttpClient()
92  
-		client.hostConfiguration.proxyHost = new ProxyHost('localhost', 5555)
93  
-		
94  
-		and:
95  
-        def request = new GetMethod(endpoint.url)
96  
-
97  
-        when:
98  
-        def status = client.executeMethod(request)
99  
-
100  
-        then:
101  
-        status == HTTP_OK
102  
-        request.getResponseHeader(VIA)?.value == 'Betamax'
103  
-    }
104  
-
105  
-    @Timeout(10)
106  
-	@Betamax(tape = 'proxy network comms spec')
107  
-    void 'proxy handles #method requests'() {
108  
-        given:
109  
-        def http = new BetamaxRESTClient(endpoint.url)
110  
-
111  
-        when:
112  
-        def response = http."$method"(path: '/')
113  
-
114  
-        then:
115  
-        response.status == HTTP_OK
116  
-        response.getFirstHeader(VIA)?.value == 'Betamax'
117  
-
118  
-        cleanup:
119  
-        http.shutdown()
120  
-
121  
-        where:
122  
-        method << ['get', 'post', 'put', 'head', 'delete', 'options']
123  
-    }
124  
-
125  
-}
49  src/test/groovy/co/freeside/betamax/proxy/RequestMethodsSpec.groovy
... ...
@@ -0,0 +1,49 @@
  1
+package co.freeside.betamax.proxy
  2
+
  3
+import co.freeside.betamax.*
  4
+import co.freeside.betamax.proxy.jetty.SimpleServer
  5
+import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
  6
+import co.freeside.betamax.util.server.EchoHandler
  7
+import groovyx.net.http.*
  8
+import org.apache.commons.httpclient.*
  9
+import org.apache.commons.httpclient.methods.GetMethod
  10
+import org.apache.http.HttpHost
  11
+import org.junit.Rule
  12
+import spock.lang.*
  13
+import static co.freeside.betamax.util.FileUtils.newTempDir
  14
+import static java.net.HttpURLConnection.HTTP_OK
  15
+import static org.apache.http.HttpHeaders.VIA
  16
+import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY
  17
+
  18
+@Unroll
  19
+class RequestMethodsSpec extends Specification {
  20
+
  21
+    @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
  22
+    @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
  23
+    @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
  24
+
  25
+    void setupSpec() {
  26
+        endpoint.start(EchoHandler)
  27
+    }
  28
+
  29
+    @Timeout(10)
  30
+	@Betamax(tape = 'proxy network comms spec')
  31
+    void 'proxy handles #method requests'() {
  32
+        given:
  33
+        def http = new BetamaxRESTClient(endpoint.url)
  34
+
  35
+        when:
  36
+        def response = http."$method"(path: '/')
  37
+
  38
+        then:
  39
+        response.status == HTTP_OK
  40
+        response.getFirstHeader(VIA)?.value == 'Betamax'
  41
+
  42
+        cleanup:
  43
+        http.shutdown()
  44
+
  45
+        where:
  46
+        method << ['get', 'post', 'put', 'head', 'delete', 'options']
  47
+    }
  48
+
  49
+}

0 notes on commit 3059164

Please sign in to comment.
Something went wrong with that request. Please try again.