diff --git a/R/middleware.R b/R/middleware.R index 0a5a563..f71f940 100644 --- a/R/middleware.R +++ b/R/middleware.R @@ -17,7 +17,8 @@ middleware <- function(engine) { # the dummy path is needed for pinging the server from hotwater plumber::pr_get( "/__hotwater__", function() "running", - serializer = plumber::serializer_text() + serializer = plumber::serializer_text(), + preempt = "__first__" ) |> plumber::pr_hook("postserialize", postserialise_hotwater(js)) } diff --git a/tests/testthat/test-middleware.R b/tests/testthat/test-middleware.R index 8d89f29..d0bd2f9 100644 --- a/tests/testthat/test-middleware.R +++ b/tests/testthat/test-middleware.R @@ -16,6 +16,42 @@ test_that("middleware injection works", { ) }) +test_that("middleware injection works with filters", { + engine <- new_engine( + path = system.file("examples", "plumber.R", package = "hotwater"), + port = httpuv::randomPort(), + dirs = NULL, + host = NULL, + ignore = NULL + ) + runner <- callr::r_bg( + function(port, middleware) { + plumber::pr(system.file("examples", "plumber.R", package = "hotwater")) |> + plumber::pr_filter("foo", function(req, res) { + stop("I break things") + }) |> + middleware() |> + plumber::pr_run(port = port) + }, + args = list( + port = engine$config$port, + middleware = middleware(engine) + ) + ) + + i <- 1L + while (i < 20L && !is_plumber_running(engine)) { + i <- i + 1L + Sys.sleep(0.5) + } + + resp <- httr2::request(sprintf("localhost:%s/__hotwater__", engine$config$port)) |> + httr2::req_perform() |> + httr2::resp_status() + + expect_identical(resp, 200L) +}) + test_that("is_plumber_running works", { dummy_engine <- list( config = list( @@ -28,8 +64,9 @@ test_that("is_plumber_running works", { system.file("examples", "plumber.R", package = "hotwater") ) |> plumber::pr_get( - "/__hotwater__", function() "running", - serializer = plumber::serializer_text() + "/__hotwater__", + function() "running", + serializer = plumber::serializer_text() ) |> plumber::pr_run(port = port) }, @@ -55,7 +92,6 @@ test_that("autoreloader is attached", { host = NULL, ignore = NULL ) - middleware_filter <- middleware(engine) new_runner(engine) resp <- httr2::request(sprintf("localhost:%s", engine$config$port)) |> httr2::req_perform() |>