-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest-ause_cassette_record_modes.R
181 lines (155 loc) · 6.24 KB
/
test-ause_cassette_record_modes.R
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
context("use_cassette: record modes work as expected")
library(crul, quietly = TRUE)
mydir <- file.path(tempdir(), "use_cassette_record_mode")
invisible(vcr_configure(dir = mydir))
conn <- crul::HttpClient$new(hb())
test_that("use_cassette record mode: once", {
# record mode `once`:
# - Replay previously recorded interactions.
# - Record new interactions if there is no cassette file.
# - Cause an error to be raised for new requests if there is a cassette file.
skip_on_cran()
unlink(file.path(vcr_c$dir, "once.yml"))
# record interaction
one <- use_cassette("once", (res <- conn$get("get")), record = "once")
expect_is(one, "Cassette")
expect_is(res, "HttpResponse")
# interaction should replay
# - we know it replayed if it doesn't timeout as timeout only
# used in real request
two <- use_cassette("once", {
res2 <- conn$get("get", timeout_ms = 10)
}, record = "once")
expect_is(two, "Cassette")
expect_equal(length(one$http_interactions_$used_interactions), 0)
expect_equal(length(two$http_interactions_$used_interactions), 1)
expect_equal(length(one$new_recorded_interactions), 1)
expect_equal(length(two$new_recorded_interactions), 0)
expect_identical(res$content, res2$content)
# delete cassette file, new interaction should be recorded successfully
unlink(file.path(vcr_c$dir, "once.yml"))
expect_false(file.exists(file.path(vcr_c$dir, "once.yml")))
three <- use_cassette("once", (res3 <- conn$get("get")),
record = "once")
expect_is(three, "Cassette")
# raise error on attempted NEW INTERACTION on existing cassette file
expect_error(
use_cassette("once", conn$get("get", query = list(foo = "bar")),
record = "once"),
"An HTTP request has been made that vcr does not know how to handle"
)
})
test_that("use_cassette record mode: none", {
# record mode `none`:
# - Replay previously recorded interactions.
# - Cause an error to be raised for any new requests.
skip_on_cran()
unlink(file.path(vcr_c$dir, "none.yml"))
# record first with another record mode to make the cassette
invisible(use_cassette("none", (res <- conn$get("get"))))
# previously recorded interaction should replay
one <- use_cassette("none", (res <- conn$get("get")), record = "none")
expect_is(one, "Cassette")
expect_is(res, "HttpResponse")
# raise error if any NEW INTERACTIONS attempted
# FIXME:
expect_error(
use_cassette("none", conn$get("get", query = list(foo = "bar")),
record = "none"),
"vcr does not know how to handle"
# "The current record mode \\('none'\\) does not"
)
})
test_that("use_cassette record mode: new_episodes", {
# record mode `new_episodes`:
# - Record new interactions.
# - Replay previously recorded interactions.
skip_on_cran()
unlink(file.path(vcr_c$dir, "new_episodes.yml"))
# record first interaction
one <- use_cassette("new_episodes", {
res <- conn$get("get")
}, record = "new_episodes")
expect_is(one, "Cassette")
expect_is(res, "HttpResponse")
one_yml <- yaml::yaml.load_file(file.path(vcr_c$dir, "new_episodes.yml"))
expect_equal(length(one_yml$http_interactions), 1)
# first interaction again, should be played back
one_again <- use_cassette("new_episodes", {
res2 <- conn$get("get")
}, record = "new_episodes")
expect_is(one_again, "Cassette")
expect_is(res2, "HttpResponse")
one_again_yml <- yaml::yaml.load_file(file.path(vcr_c$dir, "new_episodes.yml"))
expect_equal(length(one_again_yml$http_interactions), 1)
# record new interaction, is recorded below first one above
two <- use_cassette("new_episodes", {
res3 <- conn$get("get", query = list(project = "mars-explorer"))
}, record = "new_episodes")
expect_is(two, "Cassette")
expect_is(res3, "HttpResponse")
two_yml <- yaml::yaml.load_file(file.path(vcr_c$dir, "new_episodes.yml"))
expect_equal(length(two_yml$http_interactions), 2)
# first and second interaction again together, both should be played back
yolo <- use_cassette("new_episodes", {
res2_played_back <- conn$get("get")
res3_played_back <- conn$get("get", query = list(project = "mars-explorer"))
}, record = "new_episodes")
expect_is(two, "Cassette")
expect_is(res3, "HttpResponse")
two_yml <- yaml::yaml.load_file(file.path(vcr_c$dir, "new_episodes.yml"))
expect_equal(length(two_yml$http_interactions), 2)
})
test_that("use_cassette record mode: all", {
# record mode `all`:
# - Record new interactions.
# - Never replay previously recorded interactions.
skip_on_cran()
unlink(file.path(vcr_c$dir, "all.yml"))
# record first interaction
one <- use_cassette("all", (res <- conn$get("get")), record = "all")
one_yml <- yaml::yaml.load_file(file.path(vcr_c$dir, "all.yml"))
expect_equal(length(one_yml$http_interactions), 1)
# sleep for a bit to make sure times are at least a second apart
Sys.sleep(1)
# previously recorded interactions do not playback
# - recorded time and response header time have changed
two <- use_cassette("all", (res2 <- conn$get("get")), record = "all")
two_yml <- yaml::yaml.load_file(file.path(vcr_c$dir, "all.yml"))
expect_equal(length(two_yml$http_interactions), 1)
### recorded_at
expect_false(
identical(one_yml$http_interactions[[1]]$recorded_at,
two_yml$http_interactions[[1]]$recorded_at
)
)
expect_true(
two_yml$http_interactions[[1]]$recorded_at >
one_yml$http_interactions[[1]]$recorded_at
)
### response headers date
expect_false(
identical(one_yml$http_interactions[[1]]$response$headers$date,
two_yml$http_interactions[[1]]$response$headers$date
)
)
expect_true(
two_yml$http_interactions[[1]]$response$headers$date >
one_yml$http_interactions[[1]]$response$headers$date
)
# new interactions are recorded
three <- use_cassette("all", {
res3 <- conn$get("get", query = list(cheese = "pepperjack"))
}, record = "all")
three_yml <- yaml::yaml.load_file(file.path(vcr_c$dir, "all.yml"))
expect_equal(length(three_yml$http_interactions), 2)
expect_match(three_yml$http_interactions[[2]]$response$body$string,
"pepperjack")
})
# cleanup
unlink(file.path(vcr_c$dir, "once.yml"))
unlink(file.path(vcr_c$dir, "none.yml"))
unlink(file.path(vcr_c$dir, "new_episodes.yml"))
unlink(file.path(vcr_c$dir, "all.yml"))
# reset configuration
vcr_configure_reset()