-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a Custom Matcher spec that uses the proxy
- Loading branch information
David Kowis
committed
Nov 15, 2013
1 parent
77d0e5a
commit 25dcb20
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
betamax-proxy/src/test/groovy/co/freeside/betamax/proxy/CustomMatcherSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
betamax-proxy/src/test/resources/betamax/tapes/httpBinTape.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |