Skip to content

Commit

Permalink
Middleware unit tests
Browse files Browse the repository at this point in the history
- Add more unit tests for middleware
- Remove cli from `check_suggests` (already a dependency)
  • Loading branch information
ElianHugh committed Jun 2, 2024
1 parent 5a5e49b commit dd95cf7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
18 changes: 10 additions & 8 deletions R/middleware.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ middleware <- function(engine) {
}) |>
# the dummy path is needed for pinging the server from hotwater
plumber::pr_get("/__hotwater__", function() "running") |>
plumber::pr_hook("postserialize", function(value) {
if (grepl("text/html", value$headers[["Content-Type"]])) {
value$headers[["Cache-Control"]] <- "no-cache"
value$body <- c(value$body, js) |>
paste0(collapse = "\n")
}
value
})
plumber::pr_hook("postserialize", postserialise_hotwater)
}
}

postserialise_hotwater <- function(value) {
if (grepl("text/html", value$headers[["Content-Type"]])) {
value$headers[["Cache-Control"]] <- "no-cache"
value$body <- c(value$body, js) |>
paste0(collapse = "\n")
}
value
}

publish_browser_reload <- function(engine) {
# at the moment, the message itself is largely meaningless because we're faking the
# protocol on the javascript side of things
Expand Down
17 changes: 11 additions & 6 deletions R/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ uninstall_hotwater <- function(install_folder = "~/.local/bin/") {
}
}

#' WIP
#' Check suggested packages for CLI usage
#'
#' The {docopt} and {remotes} packages are required to run hotwater from the command line.
#'
#' @noRd
check_suggests <- function() {
suggests <- c("docopt", "cli", "remotes")
suggests <- c("docopt", "remotes")
lapply(
suggests,
function(suggestion) {
Expand All @@ -68,7 +72,8 @@ check_suggests <- function() {
invisible()
}

#' WIP
#' Run hotwater as a bash script
#' @noRd
run_cli <- function() {
doc <- "hotwater
Expand All @@ -83,18 +88,18 @@ run_cli <- function() {
-f FILE --file=FILE plumber path (required)
-d DIRS --dirs=DIRS extra directories
-p PORT --port=PORT plumber port
-s SERVER --host=SERVER plumber host
-s SERVER --server=SERVER plumber host
"

args <- docopt::docopt(
doc,
version = as.character(utils::packageVersion("hotwater"))
)

hotwater::run(
run(
path = args$file,
dirs = args$dirs,
port = if (is.null(args$port)) NULL else as.numeric(args$port),
host = args$host
host = args$server
)
}
28 changes: 28 additions & 0 deletions tests/testthat/test-middleware.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,33 @@ test_that("middleware injection works", {
)
)
expect_snapshot(injection(dummy_engine))
fn <- middleware(dummy_engine)
router <- fn(plumber::pr())
expect_s3_class(router$routes$`__hotwater__`, "PlumberEndpoint")
expect_identical(
router$`.__enclos_env__`$private$hooks$postserialize[[1L]],
postserialise_hotwater
)
})

test_that("is_plumber_running works", {
dummy_engine <- list(
config = list(
port = httpuv::randomPort()
)
)
router <- callr::r_bg(
function(port) {
plumber::pr(
system.file("examples", "plumber.R", package = "hotwater")
) |>
plumber::pr_run(port = port)
},
args = list(
port = dummy_engine$config$port
)
)
Sys.sleep(3L) # seems silly but works...
expect_true(is_plumber_running(dummy_engine))
router$kill_tree()
})

0 comments on commit dd95cf7

Please sign in to comment.