Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Set up custom matcher tests

  • Loading branch information...
commit 447d1e26924d174f9e5a041ef939ac0cad47aa9a 1 parent 1030bb3
@dkowis dkowis authored
View
72 betamax-proxy/src/test/groovy/co/freeside/betamax/proxy/CustomMatcherSpec.groovy
@@ -1,72 +0,0 @@
-package co.freeside.betamax.proxy
-
-import co.freeside.betamax.MatchRule
-import co.freeside.betamax.ProxyConfiguration
-import co.freeside.betamax.Recorder
-import co.freeside.betamax.TapeMode
-import co.freeside.betamax.message.Request
-import spock.lang.Shared
-import spock.lang.Specification
-
-import javax.net.ssl.HttpsURLConnection
-
-/**
- * Created by dkowis on 11/14/13.
- */
-class CustomMatcherSpec extends Specification {
- @Shared def tapeRoot = new File("src/test/resources/betamax/tapes")
- @Shared def customMatchRule = new MatchRule() {
- @Override
- boolean isMatch(Request a, Request b) {
- if(a.getUri() == b.getUri() && a.getMethod() == b.getMethod()) {
- //Same method and URI, lets do a body comparison
- def aBody = a.getBodyAsText().getInput().getText()
- def bBody = b.getBodyAsText().getInput().getText()
-
- //Ideally in the real world, we'd parse the XML or the JSON and compare the ASTs instead
- // of just comparing the body strings, so that meaningless whitespace doesn't mean anything
- System.err.println("aBody: " + aBody)
- System.err.println("bBody: " + bBody)
-
- //Right now, lets just compare the bodies also
- return aBody.equals(bBody)
- } else {
- //URI and method don't match, so we're going to bail
- return false
- }
- }
- }
-
-
- void "Using a custom matcher, can replay the tape"() {
- given:
- def proxyConfig = ProxyConfiguration.builder()
- .sslEnabled(true)
- .tapeRoot(tapeRoot)
- .defaultMode(TapeMode.READ_ONLY)
- .defaultMatchRule(customMatchRule)
- .build()
-
- def recorder = new Recorder(proxyConfig)
- recorder.start("httpBinTape")
-
- when:
- def payload = "BUTTS"
- HttpsURLConnection conn = new URL("https://httpbin.org/post").openConnection()
- conn.setDoOutput(true)
- conn.setRequestMethod("POST")
- conn.setFixedLengthStreamingMode(payload.getBytes().length)
- def out = new PrintWriter(conn.getOutputStream())
- out.print(payload)
- out.close()
-
- then:
- def response = conn.getInputStream().getText()
-
- response == "Hey look some text: BUTTS"
-
- }
-
-
-
-}
View
95 betamax-proxy/src/test/groovy/co/freeside/betamax/proxy/matchRules/CustomMatcherSpec.groovy
@@ -0,0 +1,95 @@
+package co.freeside.betamax.proxy.matchRules
+
+import co.freeside.betamax.MatchRule
+import co.freeside.betamax.ProxyConfiguration
+import co.freeside.betamax.Recorder
+import co.freeside.betamax.TapeMode
+import co.freeside.betamax.message.Request
+import spock.lang.Shared
+import spock.lang.Specification
+import spock.lang.Unroll
+
+import javax.net.ssl.HttpsURLConnection
+import java.util.concurrent.atomic.AtomicInteger
+
+/**
+ * Created by dkowis on 11/14/13.
+ */
+@Unroll
+class CustomMatcherSpec extends Specification {
+ @Shared def tapeRoot = new File("src/test/resources/betamax/tapes")
+
+ /**
+ * I can't run both of these tests, and I don't understand why.
+ * I would expect that when using recorder.stop() it'd
+ */
+ void "Using a custom matcher, can replay the tape"() {
+ given:
+ def imr = new InstrumentedMatchRule()
+ def proxyConfig = ProxyConfiguration.builder()
+ .sslEnabled(true)
+ .tapeRoot(tapeRoot)
+ .defaultMode(TapeMode.READ_WRITE)
+ .defaultMatchRule(imr)
+ .build()
+
+ def recorder = new Recorder(proxyConfig)
+ recorder.start("httpBinTape")
+
+ when:
+ def payload = "BUTTS"
+ HttpsURLConnection conn = new URL("https://httpbin.org/post").openConnection()
+ conn.setDoOutput(true)
+ conn.setRequestMethod("POST")
+ conn.setFixedLengthStreamingMode(payload.getBytes().length)
+ def out = new PrintWriter(conn.getOutputStream())
+ out.print(payload)
+ out.close()
+
+ then:
+ def response = conn.getInputStream().getText()
+ conn.disconnect()
+ recorder.stop()
+
+ response == "Hey look some text: BUTTS"
+ }
+
+ void "When the tape only contains a single entry in #mode, it should only match once"() {
+ given:
+ def imr = new InstrumentedMatchRule()
+ def proxyConfig = ProxyConfiguration.builder()
+ .sslEnabled(true)
+ .tapeRoot(tapeRoot)
+ .defaultMode(mode)
+ .defaultMatchRule(imr)
+ .build()
+
+ def recorder = new Recorder(proxyConfig)
+ recorder.start("httpBinTape")
+
+ when:
+ assert(imr.counter.get() == 0)
+ def payload = "BUTTS"
+ HttpsURLConnection conn = new URL("https://httpbin.org/post").openConnection()
+ conn.setDoOutput(true)
+ conn.setRequestMethod("POST")
+ conn.setFixedLengthStreamingMode(payload.getBytes().length)
+ def out = new PrintWriter(conn.getOutputStream())
+ out.print(payload)
+ out.close()
+
+ then:
+ def response = conn.getInputStream().getText()
+ conn.disconnect()
+ recorder.stop()
+
+ imr.counter.get() == 1
+ response == "Hey look some text: BUTTS"
+
+ where:
+ mode << [TapeMode.READ_ONLY, TapeMode.READ_WRITE]
+ }
+
+
+
+}
View
36 betamax-proxy/src/test/groovy/co/freeside/betamax/proxy/matchRules/InstrumentedMatchRule.groovy
@@ -0,0 +1,36 @@
+package co.freeside.betamax.proxy.matchRules
+
+import co.freeside.betamax.MatchRule
+import co.freeside.betamax.message.Request
+
+import java.util.concurrent.atomic.AtomicInteger
+
+
+class InstrumentedMatchRule implements MatchRule {
+
+ def counter = new AtomicInteger(0)
+ @Override
+ boolean isMatch(Request a, Request b) {
+ def current = counter.incrementAndGet()
+ System.err.println("Matching attempt: ${current}")
+ System.err.println("A request class: ${a.getClass()}")
+ System.err.println("B request class: ${b.getClass()}")
+
+ if(a.getUri() == b.getUri() && a.getMethod() == b.getMethod()) {
+ //Same method and URI, lets do a body comparison
+ def aBody = a.getBodyAsText().getInput().getText()
+ def bBody = b.getBodyAsText().getInput().getText()
+
+ //Ideally in the real world, we'd parse the XML or the JSON and compare the ASTs instead
+ // of just comparing the body strings, so that meaningless whitespace doesn't mean anything
+ System.err.println("aBody: " + aBody)
+ System.err.println("bBody: " + bBody)
+
+ //Right now, lets just compare the bodies also
+ return aBody.equals(bBody)
+ } else {
+ //URI and method don't match, so we're going to bail
+ return false
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.