/
PostBodySpec.groovy
80 lines (64 loc) · 2.25 KB
/
PostBodySpec.groovy
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
package co.freeside.betamax.tape
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
import org.junit.Rule
import org.yaml.snakeyaml.Yaml
import spock.lang.*
import static co.freeside.betamax.TapeMode.WRITE_ONLY
import static co.freeside.betamax.util.FileUtils.newTempDir
@Issue('https://github.com/robfletcher/betamax/issues/50')
class PostBodySpec extends Specification {
@Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
private DefaultHttpClient httpClient = new DefaultHttpClient()
void setup() {
BetamaxRoutePlanner.configure(httpClient)
}
void 'post body is stored on tape when using UrlConnection'() {
given:
def postBody = '{"foo":"bar"}'
HttpURLConnection connection = 'http://httpbin.org/post'.toURL().openConnection()
connection.doOutput = true
connection.requestMethod = 'POST'
connection.addRequestProperty('Content-Type', 'application/json')
and:
recorder.startProxy('post_body_with_url_connection', [mode: WRITE_ONLY])
when:
connection.outputStream.withStream { stream ->
stream << postBody.getBytes('UTF-8')
}
println connection.inputStream.text
and:
recorder.stopProxy()
then:
def file = new File(tapeRoot, 'post_body_with_url_connection.yaml')
def tapeData = file.withReader {
new Yaml().loadAs(it, Map)
}
tapeData.interactions[0].request.body == postBody
}
void 'post body is stored on tape when using HttpClient'() {
given:
def postBody = '{"foo":"bar"}'
def httpPost = new HttpPost('http://httpbin.org/post')
httpPost.setHeader('Content-Type', 'application/json')
def reqEntity = new StringEntity(postBody, 'UTF-8')
reqEntity.setContentType('application/json')
httpPost.entity = reqEntity
and:
recorder.startProxy('post_body_with_http_client', [mode: WRITE_ONLY])
when:
httpClient.execute(httpPost)
and:
recorder.stopProxy()
then:
def file = new File(tapeRoot, 'post_body_with_http_client.yaml')
def tapeData = file.withReader {
new Yaml().loadAs(it, Map)
}
tapeData.interactions[0].request.body == postBody
}
}