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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
# Null list, an empty HTTPInteractionList object
NullList <- R6::R6Class(
'NullList',
public = list(
response_for = function() NULL,
has_interaction_matching = function() FALSE,
has_used_interaction_matching = function() FALSE,
remaining_unused_interaction_count = function() 0
)
)
#' @title HTTPInteractionList class
#' @description keeps track of all [HTTPInteraction] objects
#' @export
#' @param request The request from an object of class `HTTPInteraction`
#' @details
#' \strong{Private Methods}
#' \describe{
#' \item{\code{has_unused_interactions()}}{
#' Are there any unused interactions? returns boolean
#' }
#' \item{\code{matching_interaction_index_for()}}{
#' asdfadf
#' }
#' \item{\code{matching_used_interaction_for(request)}}{
#' asdfadfs
#' }
#' \item{\code{interaction_matches_request(request, interaction)}}{
#' Check if a request matches an interaction (logical)
#' }
#' \item{\code{from_hash()}}{
#' Get a hash back.
#' }
#' \item{\code{request_summary(z)}}{
#' Get a request summary (character)
#' }
#' \item{\code{response_summary(z)}}{
#' Get a response summary (character)
#' }
#' }
#' @examples \dontrun{
#' vcr_configure(
#' dir = tempdir(),
#' record = "once"
#' )
#'
#' # make interactions
#' ## make the request
#' ### turn off mocking
#' crul::mock(FALSE)
#' url <- "https://eu.httpbin.org/post"
#' cli <- crul::HttpClient$new(url = url)
#' res <- cli$post(body = list(a = 5))
#'
#' ## request
#' (request <- Request$new("POST", url, list(a = 5), res$headers))
#' ## response
#' (response <- VcrResponse$new(
#' res$status_http(),
#' res$response_headers,
#' res$parse("UTF-8"),
#' res$response_headers$status))
#' ## make an interaction
#' (inter <- HTTPInteraction$new(request = request, response = response))
#'
#' # make an interactionlist
#' (x <- HTTPInteractionList$new(
#' interactions = list(inter),
#' request_matchers = vcr_configuration()$match_requests_on
#' ))
#' x$interactions
#' x$request_matchers
#' x$parent_list
#' x$parent_list$response_for()
#' x$parent_list$has_interaction_matching()
#' x$parent_list$has_used_interaction_matching()
#' x$parent_list$remaining_unused_interaction_count()
#' x$used_interactions
#' x$allow_playback_repeats
#' x$interactions
#' x$response_for(request)
#' }
HTTPInteractionList <- R6::R6Class(
'HTTPInteractionList',
public = list(
#' @field interactions (list) list of interaction class objects
interactions = NULL,
#' @field request_matchers (character) vector of request matchers
request_matchers = NULL,
#' @field allow_playback_repeats whether to allow playback repeats
allow_playback_repeats = FALSE,
#' @field parent_list A list for empty objects, see `NullList`
parent_list = NullList$new(),
#' @field used_interactions (list) Interactions that have been used
used_interactions = list(),
#' @description Create a new `HTTPInteractionList` object
#' @param interactions (list) list of interaction class objects
#' @param request_matchers (character) vector of request matchers
#' @param allow_playback_repeats whether to allow playback repeats or not
#' @param parent_list A list for empty objects, see `NullList`
#' @param used_interactions (list) Interactions that have been used. That is,
#' interactions that are on disk in the current cassette, and a
#' request has been made that matches that interaction
#' @return A new `HTTPInteractionList` object
initialize = function(interactions,
request_matchers,
allow_playback_repeats = FALSE,
parent_list = NullList$new(),
used_interactions = list()) {
self$interactions <- interactions
self$request_matchers <- request_matchers
self$allow_playback_repeats <- allow_playback_repeats
self$parent_list <- parent_list
self$used_interactions <- used_interactions
interaction_summaries <- vapply(interactions, function(x) {
sprintf("%s => %s",
request_summary(Request$new()$from_hash(x$request)),
response_summary(VcrResponse$new()$from_hash(x$response)))
}, "")
vcr_log_info(sprintf(
"Init. HTTPInteractionList w/ request matchers [%s] & %s interaction(s): { %s }",
paste0(self$request_matchers, collapse = ", "),
length(interactions),
paste0(interaction_summaries, collapse = ', ')
), vcr_c$log_opts$date)
},
#' @description Check if there's a matching interaction, returns a
#' response object
response_for = function(request) {
index <- private$matching_interaction_index(request)
if (length(index) > 0) {
# index should be length 1 here it seems
# FIXME: for now just get the first one
# if (length(index > 1)) warning("more than 1 found, using first")
index <- index[1]
# delete the http interaction at <index>, and capture it into `interaction`
interaction <- self$interactions[[index]]
self$interactions <- delete_at(self$interactions, index)
# put `interaction` at front of list with `unshift`
self$used_interactions <- unshift(self$used_interactions, list(interaction))
vcr_log_info(sprintf(" Found matching interaction for %s at index %s: %s",
request_summary(Request$new()$from_hash(request)),
index,
response_summary(VcrResponse$new()$from_hash(interaction$response))),
vcr_c$log_opts$date)
interaction$response
} else {
tmp <- private$matching_used_interaction_for(request)
if (tmp) {
tmp$response
} else {
self$parent_list$response_for()
}
}
},
#' @description Check if has a matching interaction
#' @return logical
has_interaction_matching = function(request) {
private$matching_interaction_bool(request) ||
private$matching_used_interaction_for(request) ||
self$parent_list$has_interaction_matching()
},
#' @description check if has used interactions matching a given request
#' @return logical
has_used_interaction_matching = function(request) {
lapply(self$used_interactions, function(i) {
private$interaction_matches_request(request, i)
})
},
#' @description Number of unused interactions
#' @return integer
remaining_unused_interaction_count = function() {
length(self$interactions)
},
#' @description Checks if there are no unused interactions left.
#' @return various
assert_no_unused_interactions = function() {
if (!private$has_unused_interactions()) return(NULL)
descriptions <- lapply(self$interactions, function(x) {
sprintf(" - %s => %s",
request_summary(x$request, self$request_matchers),
response_summary(x$response))
})
vcr_log_info(descriptions, vcr_c$log_opts$date)
stop("There are unused HTTP interactions left in the cassette:\n",
descriptions, call. = FALSE)
}
),
private = list(
# return: logical
has_unused_interactions = function() {
length(self$interactions) > 0
},
gather_match_checks = function(request) {
out <- logical(0)
iter <- 0
while (!any(out) && iter < length(self$interactions)) {
iter <- iter + 1
bool <- private$interaction_matches_request(
request, self$interactions[[iter]])
out <- c(out, bool)
}
return(out)
},
# return: logical
matching_interaction_bool = function(request) {
any(private$gather_match_checks(request))
},
# return: integer
matching_interaction_index = function(request) {
which(private$gather_match_checks(request))
},
# return: interactions list or `FALSE`
matching_used_interaction_for = function(request) {
if (!self$allow_playback_repeats) return(FALSE)
if (length(self$used_interactions) == 0) return(FALSE)
tmp <- FALSE
i <- 0
while (!tmp) {
i <- i + 1
tmp <- private$interaction_matches_request(request,
self$used_interactions[[i]])
}
if (tmp) self$used_interactions[[i]] else FALSE
},
# return: interactions list
interaction_matches_request = function(req, interaction) {
bod <- interaction$request$body
if (length(names(bod)) > 0) {
if ("string" %in% names(bod)) bod <- bod$string
}
intreq <- Request$new(
interaction$request$method,
interaction$request$uri,
bod,
interaction$request$headers
)
vcr_log_info(sprintf(" Checking if {%s} matches {%s} using matchers: [%s]",
request_summary(req),
request_summary(intreq),
paste0(self$request_matchers, collapse = ", ")),
vcr_c$log_opts$date)
all(unlist(lapply(self$request_matchers, function(y) {
matcher <- RequestMatcherRegistry$new()$registry[[y]]
res <- matcher$matches(req, intreq)
msg <- if (res) "matched" else "did not match"
# cat(paste0("method: ", req$method), sep = "\n ")
# cat(paste0("body: ", req$body), sep = "\n ")
vcr_log_info(sprintf(" %s %s: current request [%s] vs [%s]",
y, msg,
request_summary(req, self$request_matchers),
request_summary(intreq, self$request_matchers)),
vcr_c$log_opts$date)
return(res)
})))
},
# return: character
request_summary = function(z) {
paste(z$method, z$uri)
},
# return: character
response_summary = function(z) {
paste(
z$status$status_code,
sprintf("['%s ...'", substring(gsub("\n", " ", z$body), 1, 50)),
"]"
)
}
)
)
# makes a copy - does not modify in place
# x: must be a list
# y: must be numeric; ignores values out of range
delete_at <- function(x, y) {
stopifnot(is.list(x))
stopifnot(is.numeric(y))
x[-y]
}
# makes a copy - does not modify in place
# x: a list with objects of class `HTTPInteraction`
# y: a list with an object of class `HTTPInteraction`
unshift <- function(x, y) {
stopifnot(inherits(x, "list"))
stopifnot(inherits(y, "list"))
# stopifnot(inherits(y[[1]], "HTTPInteraction"))
c(y, x)
}
|