Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a Custom Matcher spec that uses the proxy

  • Loading branch information...
commit 25dcb20f6cb436e434c5628a46dc1618726e31b6 1 parent 77d0e5a
@dkowis dkowis authored
View
73 betamax-proxy/src/test/groovy/co/freeside/betamax/proxy/CustomMatcherSpec.groovy
@@ -0,0 +1,73 @@
+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 "Doesn't change the tape when replaying an HTTPS connection"() {
+ given:
+ def proxyConfig = ProxyConfiguration.builder()
+ .sslEnabled(true)
+ .tapeRoot(tapeRoot)
+ .defaultMode(TapeMode.READ_WRITE)
+ .defaultMatchRule(customMatchRule)
+ .build()
+
+ def recorder = new Recorder(proxyConfig)
+ recorder.start("httpBinTape")
+
+ when:
+ //TODO simple rest client?
+ 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
27 betamax-proxy/src/test/resources/betamax/tapes/httpBinTape.yaml
@@ -0,0 +1,27 @@
+!tape
+name: httpbinTape
+interactions:
+- recorded: 2013-11-11T17:53:35.524Z
+ request:
+ method: POST
+ uri: https://httpbin.org/post
+ headers:
+ Accept: '*/*'
+ Accept-Charset: UTF-8,*;q=.1
+ Accept-Encoding: gzip
+ Connection: keep-alive
+ Content-Length: '5'
+ Content-Type: text/plain
+ Host: httpbin.org
+ User-Agent: Java/1.7.0_45
+ body: "BUTTS"
+ response:
+ status: 200
+ headers:
+ Access-Control-Allow-Origin: '*'
+ Connection: keep-alive
+ Content-Length: '425'
+ Content-Type: application/json
+ Date: Mon, 11 Nov 2013 17:53:35 GMT
+ Server: gunicorn/0.17.4
+ body: "Hey look some text: BUTTS"
Please sign in to comment.
Something went wrong with that request. Please try again.