Permalink
Browse files

Okay, this is pretty solid now

  • Loading branch information...
1 parent fd8893f commit f6a258a2a286641d619212c92c8ba5164b231bbb @dkowis dkowis committed Nov 19, 2013
@@ -3,6 +3,8 @@ package co.freeside.betamax.proxy.matchRules
import co.freeside.betamax.ProxyConfiguration
import co.freeside.betamax.Recorder
import co.freeside.betamax.TapeMode
+import groovyx.net.http.ContentType
+import groovyx.net.http.RESTClient
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
@@ -14,37 +16,52 @@ import javax.net.ssl.HttpsURLConnection
*/
@Unroll
class CustomMatcherSpec extends Specification {
- @Shared def tapeRoot = new File("src/test/resources/betamax/tapes")
+ @Shared
+ def tapeRoot = new File("src/test/resources/betamax/tapes")
- void "Using a custom matcher, can replay the tape"() {
+ def simplePost(String url, String payload) {
+ def output = null
+ HttpsURLConnection conn = new URL(url).openConnection()
+ conn.setDoOutput(true)
+ conn.setRequestMethod("POST")
+ conn.setFixedLengthStreamingMode(payload.getBytes().length)
+ def out = new PrintWriter(conn.getOutputStream())
+ out.print(payload)
+ out.close()
+
+ output = conn.getInputStream().getText()
+ conn.disconnect()
+
+ return output
+ }
+
+ void "Using a custom matcher it should replay"() {
given:
def imr = new InstrumentedMatchRule()
def proxyConfig = ProxyConfiguration.builder()
.sslEnabled(true)
.tapeRoot(tapeRoot)
- .defaultMode(TapeMode.READ_WRITE)
+ .defaultMode(TapeMode.READ_ONLY)
.defaultMatchRule(imr)
.build()
def recorder = new Recorder(proxyConfig)
recorder.start("httpBinTape")
+ imr.requestValidations << { r ->
+ //Will run this request validation on both requests being matched
+ //No matter what, either recorded, or sent, I should have a payload of "BUTTS"
+ if(r.bodyAsText.input.text != "BUTTS" ){
+ println("REQUEST BODY WASNT THERE!!!")
+ }
+ }
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()
-
+ def response = simplePost("https://httpbin.org/post", "BUTTS")
then:
- def response = conn.getInputStream().getText()
- conn.disconnect()
+ def content = response.toString()
recorder.stop()
- response == "Hey look some text: BUTTS"
+ content == "Hey look some text: BUTTS"
}
void "When the tape only contains a single entry in #mode, it should only match once"() {
@@ -61,19 +78,10 @@ class CustomMatcherSpec extends Specification {
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()
+ assert (imr.counter.get() == 0)
+ def response = simplePost("https://httpbin.org/post", "BUTTS")
then:
- def response = conn.getInputStream().getText()
- conn.disconnect()
recorder.stop()
imr.counter.get() == 1
@@ -84,5 +92,4 @@ class CustomMatcherSpec extends Specification {
}
-
}
@@ -9,12 +9,47 @@ import java.util.concurrent.atomic.AtomicInteger
class InstrumentedMatchRule implements MatchRule {
def counter = new AtomicInteger(0)
+
+ def requestValidations = []
+
+ def readAll(Reader r) {
+ def output = ""
+ def line = null
+ while((line = r.readLine()) != null) {
+ output << line << "\n"
+ }
+ return output.trim()
+ }
+
+ def dumpRequest(Request r) {
+ println("Request:")
+ def lines = [
+ "URI: ${r.uri}",
+ "METHOD: ${r.method}",
+ "HEADERS: ${r.headers.collect { k,v -> "$k: $v"} join ", "}",
+ "BODY: ${readAll(r.bodyAsText.input)}"
+ ]
+ lines.each { l ->
+ println("\t" + l)
+ }
+ }
@Override
boolean isMatch(Request a, Request b) {
+
+ requestValidations.each { rv ->
+ rv.call(a)
+ rv.call(b)
+ }
+
+ //Does doing this eat all the text and ruin it?
+ println("REQUEST A:")
+ dumpRequest(a)
+ println("REQUEST B:")
+ dumpRequest(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()}")
+ println("Matching attempt: ${current}")
+ println("A request class: ${a.getClass()}")
+ println("B request class: ${b.getClass()}")
if(a.getUri() == b.getUri() && a.getMethod() == b.getMethod()) {
//Same method and URI, lets do a body comparison
@@ -23,8 +58,8 @@ class InstrumentedMatchRule implements MatchRule {
//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)
+ println("aBody: " + aBody)
+ println("bBody: " + bBody)
//Right now, lets just compare the bodies also
return aBody.equals(bBody)

0 comments on commit f6a258a

Please sign in to comment.