Skip to content
This repository
tree: 76e2e14a19
Fetching contributors…

Cannot retrieve contributors at this time

file 123 lines (100 sloc) 2.914 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
package co.freeside.betamax.proxy

import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.*
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import org.yaml.snakeyaml.Yaml
import spock.lang.*
import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK

@Stepwise
class ProxyRecordAndPlaybackSpec extends Specification {

@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Shared @AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
RESTClient http

void setupSpec() {
recorder.insertTape('proxy record and playback spec')
proxy.start()
}

void setup() {
http = new RESTClient(endpoint.url)
BetamaxRoutePlanner.configure(http.client)
}

@Timeout(10)
void 'proxy makes a real HTTP request the first time it gets a request for a URI'() {
given:
endpoint.start(EchoHandler)

when:
http.get(path: '/')

then:
recorder.tape.size() == 1
}

@Timeout(10)
void 'subsequent requests for the same URI are played back from tape'() {
when:
http.get(path: '/')

then:
recorder.tape.size() == 1
}

@Timeout(10)
void 'subsequent requests with a different HTTP method are recorded separately'() {
given:
endpoint.start(EchoHandler)

when:
http.head(path: '/')

then:
recorder.tape.size() == old(recorder.tape.size()) + 1
recorder.tape.interactions[-1].request.method == 'HEAD'
}

void 'when the tape is ejected the data is written to a file'() {
given:
proxy.stop()

when:
recorder.ejectTape()

then:
def file = new File(recorder.tapeRoot, 'proxy_record_and_playback_spec.yaml')
file.isFile()

and:
def yaml = file.withReader { reader ->
new Yaml().loadAs(reader, Map)
}
yaml.name == 'proxy record and playback spec'
yaml.size() == 2
}

void 'can load an existing tape from a file'() {
given:
def file = new File(recorder.tapeRoot, 'existing_tape.yaml')
file.parentFile.mkdirs()
file.text = '''\
!tape
name: existing_tape
interactions:
- recorded: 2011-08-19T11:45:33.000Z
request:
method: GET
uri: http://icanhascheezburger.com/
headers: {Accept-Language: 'en-GB,en', If-None-Match: b00b135}
response:
status: 200
headers: {Content-Type: text/plain, Content-Language: en-GB}
body: O HAI!
'''

when:
recorder.insertTape('existing_tape')
proxy.start()

then:
recorder.tape.name == 'existing_tape'
recorder.tape.size() == 1
}

@Timeout(10)
void 'can play back a loaded tape'() {
when:
def response = http.get(uri: 'http://icanhascheezburger.com/')

then:
response.statusLine.statusCode == HTTP_OK
response.data.text == 'O HAI!'
}

}
Something went wrong with that request. Please try again.