Skip to content

Commit

Permalink
Adding a Custom Matcher spec that uses the proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kowis committed Nov 15, 2013
1 parent 77d0e5a commit 25dcb20
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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"

}



}
27 changes: 27 additions & 0 deletions betamax-proxy/src/test/resources/betamax/tapes/httpBinTape.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 25dcb20

Please sign in to comment.