Skip to content
This repository
tree: d673f5cb79
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 96 lines (85 sloc) 2.463 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
package co.freeside.betamax.recorder

import co.freeside.betamax.Recorder
import co.freeside.betamax.util.httpbuilder.BetamaxRESTClient
import groovyx.net.http.RESTClient
import spock.lang.*
import static co.freeside.betamax.MatchRule.host
import static co.freeside.betamax.util.FileUtils.newTempDir
import static org.apache.http.HttpHeaders.VIA

@Issue('https://github.com/robfletcher/betamax/issues/9')
class RequestMatchingSpec extends Specification {

@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Shared Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared RESTClient http = new BetamaxRESTClient()

void setupSpec() {
tapeRoot.mkdirs()
}

@Unroll('#method request for #uri returns "#responseText"')
void 'default match is method and uri'() {
given:
new File(tapeRoot, 'method_and_uri_tape.yaml').text = '''\
!tape
name: method and uri tape
interactions:
- recorded: 2011-08-23T20:24:33.000Z
request:
method: GET
uri: http://xkcd.com/
response:
status: 200
headers: {Content-Type: text/plain}
body: get method response from xkcd.com
- recorded: 2011-08-23T20:24:33.000Z
request:
method: POST
uri: http://xkcd.com/
response:
status: 200
headers: {Content-Type: text/plain}
body: post method response from xkcd.com
- recorded: 2011-08-23T20:24:33.000Z
request:
method: GET
uri: http://qwantz.com/
response:
status: 200
headers: {Content-Type: text/plain}
body: get method response from qwantz.com
'''
when:
def response = recorder.withTape('method and uri tape') {
http."$method"(uri: uri)
}

then:
response.data.text == responseText

where:
method | uri
'get' | 'http://xkcd.com/'
'post' | 'http://xkcd.com/'
'get' | 'http://qwantz.com/'

responseText = "$method method response from ${uri.toURI().host}"
}

void 'can match based on host'() {
given:
new File(tapeRoot, 'host_match_tape.yaml').text = '''\
!tape
name: host match tape
interactions:
- recorded: 2011-08-23T20:24:33.000Z
request:
method: GET
uri: http://xkcd.com/936/
response:
status: 200
headers: {Content-Type: text/plain}
body: get method response from xkcd.com
'''
when:
def response = recorder.withTape('host match tape', [match: [host]]) {
http.get(uri: 'http://xkcd.com/875/')
}

then:
response.getFirstHeader(VIA)?.value == 'Betamax'
response.data.text == 'get method response from xkcd.com'
}
}
Something went wrong with that request. Please try again.