-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: pachamaltese <pachamaltese@users.noreply.github.com> Co-authored-by: Barret Schloerke <barret@rstudio.com>
- Loading branch information
1 parent
03a34d1
commit e535922
Showing
7 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#' @rdname serializers | ||
#' @export | ||
serializer_csv <- function(...) { | ||
if (!requireNamespace("readr", quietly = TRUE)) { | ||
stop("`readr` must be installed for `serializer_csv` to work") | ||
} | ||
|
||
function(val, req, res, errorHandler) { | ||
tryCatch({ | ||
res$setHeader("Content-Type", "text/plain; charset=UTF-8") | ||
res$body <- readr::format_csv(val, ...) | ||
return(res$toResponse()) | ||
}, error = function(e){ | ||
errorHandler(req, res, e) | ||
}) | ||
} | ||
} | ||
|
||
#' @include globals.R | ||
.globals$serializers[["csv"]] <- serializer_csv |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
context("CSV serializer") | ||
|
||
test_that("CSV serializes properly", { | ||
skip_if_not_installed("readr") | ||
|
||
d <- data.frame(a=1, b=2, c="hi") | ||
val <- serializer_csv()(d, data.frame(), PlumberResponse$new(), stop) | ||
expect_equal(val$status, 200L) | ||
expect_equal(val$headers$`Content-Type`, "text/plain; charset=UTF-8") | ||
expect_equal(val$body, readr::format_csv(d)) | ||
|
||
d <- data.frame(a=1, b=2, c="hi", na=NA) | ||
val <- serializer_csv()(d, data.frame(), PlumberResponse$new(), stop) | ||
expect_equal(val$status, 200L) | ||
expect_equal(val$headers$`Content-Type`, "text/plain; charset=UTF-8") | ||
expect_equal(val$body, readr::format_csv(d, na = "NA")) | ||
|
||
d <- data.frame(a=1, b=2, c="hi", na=NA) | ||
val <- serializer_csv(na = 'string')(d, data.frame(), PlumberResponse$new(), stop) | ||
expect_equal(val$status, 200L) | ||
expect_equal(val$headers$`Content-Type`, "text/plain; charset=UTF-8") | ||
expect_equal(val$body, readr::format_csv(d, na = 'string')) | ||
}) | ||
|
||
test_that("Errors call error handler", { | ||
skip_if_not_installed("readr") | ||
|
||
errors <- 0 | ||
errHandler <- function(req, res, err){ | ||
errors <<- errors + 1 | ||
} | ||
|
||
expect_equal(errors, 0) | ||
serializer_csv()(parse(text="hi"), data.frame(), PlumberResponse$new("csv"), err = errHandler) | ||
expect_equal(errors, 1) | ||
}) |