Skip to content

Commit

Permalink
dont enc2utf8 anymore; just set encoding in cassettes to empty string
Browse files Browse the repository at this point in the history
- also not using body_from in response or request classes - may need to change this
  • Loading branch information
sckott committed Jul 31, 2022
1 parent 6ee2f55 commit cf5ee5b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 40 deletions.
2 changes: 1 addition & 1 deletion R/cassette_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ Cassette <- R6::R6Class(
response <- VcrResponse$new(
z$response$status,
z$response$headers,
z$response$body$string,
z$response$body$string %||% z$response$body$base64_string,
opts = self$cassette_opts,
disk = z$response$body$file
)
Expand Down
4 changes: 3 additions & 1 deletion R/http_interaction_list.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
me <- new.env()

# Null list, an empty HTTPInteractionList object
NullList <- R6::R6Class(
'NullList',
Expand Down Expand Up @@ -115,7 +117,7 @@ HTTPInteractionList <- R6::R6Class(
self$allow_playback_repeats <- allow_playback_repeats
self$parent_list <- parent_list
self$used_interactions <- used_interactions

me$httpil <- self
interaction_summaries <- vapply(interactions, function(x) {
sprintf("%s => %s",
request_summary(Request$new()$from_hash(x$request)),
Expand Down
27 changes: 18 additions & 9 deletions R/request_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Request <- R6::R6Class(
Request$new(
method = hash[['method']],
uri = hash[['uri']],
# body = hash[['body']],
body = body_from(hash[['body']]),
headers = hash[['headers']],
disk = hash[['disk']]
Expand Down Expand Up @@ -155,7 +156,8 @@ serializable_body <- function(x, preserve_exact_body_bytes = FALSE) {
body_from <- function(x) {
if (is.null(x)) x <- ""
if (
(!is.null(attr(x, "base64")) && attr(x, "base64")) || all(is_base64(x))
(!is.null(attr(x, "base64")) && attr(x, "base64"))
# (!is.null(attr(x, "base64")) && attr(x, "base64")) || all(is_base64(x))
) {
b64dec <- base64enc::base64decode(x)
b64dec_r2c <- tryCatch(rawToChar(b64dec), error = function(e) e)
Expand All @@ -168,7 +170,8 @@ body_from <- function(x) {
b64dec_r2c
}
} else {
try_encode_string(x, Encoding_safe(x))
x
# try_encode_string(x, Encoding_safe(x))
}
}

Expand All @@ -179,13 +182,19 @@ try_encoding <- function(x) {
}

is_base64 <- function(x) {
if (inherits(x, "form_file")) return(FALSE)
as_num <- tryCatch(as.numeric(x), warning = function(w) w)
if (!inherits(as_num, "warning")) return(FALSE)
# split string by newlines b/c base64 w/ newlines won't be
# recognized as valid base64
x <- strsplit(x, "\r|\n", useBytes = TRUE)[[1]]
all(grepl(b64_pattern, x))
if (!is.list(x)) {
if ("base64" %in% names(attributes(x))) {
return(attr(x, 'base64'))
}
return(FALSE)
}
return("base64_string" %in% names(x))
# as_num <- tryCatch(as.numeric(x), warning = function(w) w)
# if (!inherits(as_num, "warning")) return(FALSE)
# # split string by newlines b/c base64 w/ newlines won't be
# # recognized as valid base64
# x <- strsplit(x, "\r|\n", useBytes = TRUE)[[1]]
# all(grepl(b64_pattern, x))
}

Encoding_safe <- function(x) {
Expand Down
11 changes: 9 additions & 2 deletions R/response_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ VcrResponse <- R6::R6Class(
if (inherits(body, "list")) {
body <- paste(names(body), body, sep = "=", collapse = ",")
}
self$body <- if (is.character(body)) enc2utf8(body) else body
# self$body <- if (is.character(body)) enc2utf8(body) else body
self$body <- body
}
if (!missing(http_version)) {
self$http_version <- extract_http_version(http_version)
Expand All @@ -119,6 +120,11 @@ VcrResponse <- R6::R6Class(
if (!missing(disk)) self$disk <- disk
},

#' @description print method for the `VcrResponse` class
#' @param x self
#' @param ... ignored
print = function(x, ...) cat("<VcrResponse> ", sep = "\n"),

#' @description Create a hash
#' @return a list
to_hash = function() {
Expand All @@ -141,7 +147,8 @@ VcrResponse <- R6::R6Class(
VcrResponse$new(
hash[["status"]],
hash[["headers"]],
body_from(hash[["body"]]),
hash[["body"]],
# body_from(hash[["body"]]),
hash[["http_version"]],
hash[["adapater_metadata"]],
hash[["disk"]]
Expand Down
12 changes: 6 additions & 6 deletions R/serializer.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ Serializer <- R6::R6Class("Serializer",
} else {
# check for base64 encoding
x$http_interactions <- lapply(x$http_interactions, function(z) {
if (is_base64(z$response$body$string)) {
if (is_base64(z$response$body)) {
# if character and newlines detected, remove newlines
z$response$body$string <- private$strip_newlines(z$response$body$string)
b64dec <- base64enc::base64decode(z$response$body$string)
z$response$body$base64_string <- private$strip_newlines(z$response$body$base64_string)
b64dec <- base64enc::base64decode(z$response$body$base64_string)
b64dec_r2c <- tryCatch(rawToChar(b64dec), error = function(e) e)
z$response$body$string <- if (inherits(b64dec_r2c, "error")) {
z$response$body$base64_string <- if (inherits(b64dec_r2c, "error")) {
# probably is binary (e.g., pdf), so can't be converted to char.
b64dec
} else {
# probably was originally character data, so
# can convert to character from binary
b64dec_r2c
}
z$response$body$encoding <-
sup_mssg(vcr_c$quiet, encoding_guess(z$response$body$string, TRUE))
z$response$body$encoding <- ""
# sup_mssg(vcr_c$quiet, encoding_guess(z$response$body$base64_string, TRUE))
}
return(z)
})
Expand Down
11 changes: 7 additions & 4 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ prep_interaction <- function(x, file, bytes) {
} else {
get_body(x$response$body)
}
body_nchar <- tryCatch(nchar(body), error = function(e) e)
body <- enc2utf8(body)
if (length(body) == 0 || !nzchar(body)) body <- ""
list(
res = list(
list(
request = list(
method = x$request$method,
Expand All @@ -68,7 +66,7 @@ prep_interaction <- function(x, file, bytes) {
status = x$response$status,
headers = dedup_keys(x$response$headers),
body = list(
encoding = sup_mssg(vcr_c$quiet, encoding_guess(x$response$body, bytes)),
encoding = "",
file = x$response$disk,
string = body
)
Expand All @@ -77,6 +75,11 @@ prep_interaction <- function(x, file, bytes) {
recorded_with = pkg_versions()
)
)
if (bytes) {
str_index <- which(grepl("string", names(res[[1]]$response$body)))
names(res[[1]]$response$body)[str_index] <- "base64_string"
}
return(res)
}

# param x: a list with "request" and "response" slots
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-httr.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test_that("httr use_cassette works", {
# response body
str <- yaml::yaml.load_file(out$manfile)
str <- rawToChar(base64enc::base64decode(
str$http_interactions[[1]]$response$body$string))
str$http_interactions[[1]]$response$body$base64_string))
expect_is(str, "character")
expect_match(str, "404")
expect_match(str, "DOCTYPE HTML")
Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/test-quiet.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ vcr_configure(dir = tmpdir)

test_that("quiet works", {
library(crul)
con <- HttpClient$new("https://eu.httpbin.org")
# default: quiet=TRUE
expect_true(vcr_configuration()$quiet)
expect_message(
use_cassette("foo3", crul::ok("https://eu.httpbin.org/get")),
use_cassette("foo3", con$get("get")),
NA
)
expect_message(
use_cassette("foo1", crul::ok("https://eu.httpbin.org/get")),
use_cassette("foo1", con$get("get")),
NA
)
expect_message(
use_cassette("foo2", crul::ok("https://eu.httpbin.org/get")),
use_cassette("foo2", con$get("get")),
NA
)

Expand All @@ -25,15 +26,15 @@ test_that("quiet works", {
expect_false(vcr_configuration()$quiet)
webmockr::webmockr_disable_net_connect()
expect_message(
use_cassette("foo3", crul::ok("https://eu.httpbin.org/get")),
use_cassette("foo3", con$get("get")),
"allowed"
)
expect_message(
use_cassette("foo1", crul::ok("https://eu.httpbin.org/get")),
use_cassette("foo1", con$get("get")),
"enabled"
)
expect_message(
use_cassette("foo2", crul::ok("https://eu.httpbin.org/get")),
use_cassette("foo2", con$get("get")),
"disabled"
)
})
Expand Down
23 changes: 13 additions & 10 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ test_that("try_encoding", {

test_that("is_base64", {
expect_error(is_base64(), "\"x\" is missing")
# actual base64 strings are base64
expect_true(is_base64(base64enc::base64encode(charToRaw("foo"))))
# regular character strings are not base64
expect_false(is_base64("foo"))
# numbers as strings are not base64
expect_false(is_base64("12345"))
# numbers as numbers are not base64
expect_false(is_base64(12345))
expect_false(is_base64(base64enc::base64encode(charToRaw("foo"))))
expect_true(is_base64(list(base64_string = "adfadsf")))
expect_false(is_base64(list(string = "adfadsf")))
# # actual base64 strings are base64
# expect_true(is_base64(base64enc::base64encode(charToRaw("foo"))))
# # regular character strings are not base64
# expect_false(is_base64("foo"))
# # numbers as strings are not base64
# expect_false(is_base64("12345"))
# # numbers as numbers are not base64
# expect_false(is_base64(12345))
})

test_that("serializable_body", {
Expand All @@ -44,7 +47,7 @@ test_that("serializable_body", {
expect_is(aa, "character")
expect_true(attr(aa, "base64"))
expect_true(is_base64(aa))
expect_true(is_base64(aa[[1]]))
expect_false(is_base64(aa[[1]]))

bb <- serializable_body("foo", FALSE)
expect_is(bb, "character")
Expand All @@ -65,7 +68,7 @@ test_that("body_from", {

cc <- body_from(base64enc::base64encode(charToRaw("foo")))
expect_is(cc, "character")
expect_true(is_base64(base64enc::base64encode(charToRaw("foo"))))
expect_false(is_base64(base64enc::base64encode(charToRaw("foo"))))
expect_false(is_base64(cc))

dd <- body_from(charToRaw("foo"))
Expand Down

0 comments on commit cf5ee5b

Please sign in to comment.