Skip to content

Commit

Permalink
#111 more work on handling redirects - a few new helper fxns, not wor…
Browse files Browse the repository at this point in the history
…king yet
  • Loading branch information
sckott committed Mar 2, 2021
1 parent e518fa1 commit 408bb73
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions R/adapter.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ Adapter <- R6::R6Class("Adapter",
wi_th(tmp, .list = list(query = urip$parameter, headers = req$headers))
}

# check if new request/response from redirects in vcr
req <- redirects_request(req)
resp <- redirects_response(resp)

} else {
private$mock(on = FALSE)
resp <- private$fetch_request(req)
Expand Down
31 changes: 24 additions & 7 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,23 @@ vcr_loaded <- function() {
"package:vcr" %in% search()
}

handle_separate_redirects <- function(req) {
# check whether a cassette is inserted without assuming vcr is installed
vcr_cassette_inserted <- function() {
if (vcr_loaded()) {
return(length(vcr::current_cassette()) > 0)
}
return(FALSE)
}

check_redirect_setting <- function() {
cs <- vcr::current_cassette()
stopifnot("record_separate_redirects must be logical" =
is.logical(cs$record_separate_redirects))
return(cs)
}

handle_separate_redirects <- function(req) {
cs <- check_redirect_setting()
if (cs$record_separate_redirects) {
req$options$followlocation <- 0L
if (is.list(req$url))
Expand All @@ -209,10 +222,14 @@ handle_separate_redirects <- function(req) {
return(req)
}

# check whether a cassette is inserted without assuming vcr is installed
vcr_cassette_inserted <- function() {
if (vcr_loaded()) {
return(length(vcr::current_cassette()) > 0)
}
return(FALSE)
redirects_request <- function(x) {
cs <- check_redirect_setting()
if (cs$record_separate_redirects) return(cs$request_handler$request_original)
x
}

redirects_response <- function(x) {
cs <- check_redirect_setting()
if (cs$record_separate_redirects) return(last(cs$redirect_pool)[[1]])
x
}

0 comments on commit 408bb73

Please sign in to comment.