Skip to content

Commit

Permalink
Add application/octet-stream serializer (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored Jun 8, 2022
1 parent 6dfc0ab commit ca42a30
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export(serializer_html)
export(serializer_htmlwidget)
export(serializer_jpeg)
export(serializer_json)
export(serializer_octet)
export(serializer_parquet)
export(serializer_pdf)
export(serializer_png)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
## New features

* Static file handler now serves HEAD requests. (#798)

* Introduces new GeoJSON serializer and parser. GeoJSON objects are parsed into `sf` objects and `sf` or `sfc` objects will be serialized into GeoJSON. (@josiahparry, #830)

* Add new Octet-Stream serializer. This is a wrapper around the Content Type serializer with type `application/octet-stream`. (#864)

* Update feather serializer to use the arrow package. The new default feather MIME type is `application/vnd.apache.arrow.file`. (@pachadotdev #849)

* Add parquet serializer and parser by using the arrow package (@pachadotdev #849)

* Updated example `14-future` to use `promises::future_promise()` and added an endpoint that uses `{coro}` to write _simpler_ async / `{promises}` code (#785)

* Add `path` argument to `pr_cookie()` allowing Secure cookies to define where they are served (@jtlandis #850)

## Bug fixes
Expand All @@ -27,6 +34,7 @@

* Decode path URI before attempting to serve static assets (@meztez #754).


# plumber 1.1.0

## Breaking changes
Expand Down
16 changes: 16 additions & 0 deletions R/serializer.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ serializer_content_type <- function(type, serialize_fn = identity) {
)
}

#' @describeIn serializers Octet serializer. If content is received that does
#' not have a `"raw"` type, then an error will be thrown.
#' @export
serializer_octet <- function(..., type = "application/octet-stream") {
serializer_content_type(type, function(val) {
if (!is.raw(val)) {
stop("The Octet-Stream serializer received non-raw data")
}
val
})
}


#' @describeIn serializers CSV serializer. See also: [readr::format_csv()]
#' @export
serializer_csv <- function(..., type = "text/csv; charset=UTF-8") {
Expand Down Expand Up @@ -619,6 +632,9 @@ add_serializers_onLoad <- function() {
register_serializer("null", serializer_identity)
register_serializer("contentType", serializer_content_type)

# octet-stream
register_serializer("octet", serializer_octet)

# html
register_serializer("html", serializer_html)

Expand Down
6 changes: 6 additions & 0 deletions man/serializers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/testthat/test-serializer-octet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test_that("octet serializes raw objects properly", {

content <- charToRaw("Beware of bugs in the above code; I have only proved it correct, not tried it.")

val <- serializer_octet()(content, list(), PlumberResponse$new(), stop)
expect_equal(val$status, 200L)
expect_equal(val$headers$`Content-Type`, "application/octet-stream")
expect_equal(val$body, content)
expect_equal(rawToChar(val$body), rawToChar(content))
})

test_that("octet throws on non-raw objects", {

content <- "Beware of bugs in the above code; I have only proved it correct, not tried it."

expect_error(
serializer_octet()(
content,
list(),
PlumberResponse$new(),
function(req, res, err) stop(err)
),
"received non-raw data",
)
})

0 comments on commit ca42a30

Please sign in to comment.