Skip to content

Commit

Permalink
Preempt all filters
Browse files Browse the repository at this point in the history
- is_plumber_running can fail if there is a filter that requires auth. __hotwater__ now preempts all filters
  • Loading branch information
ElianHugh committed Jun 7, 2024
1 parent ee9251f commit 4d35ef8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion R/middleware.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
42 changes: 39 additions & 3 deletions tests/testthat/test-middleware.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
},
Expand All @@ -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() |>
Expand Down

0 comments on commit 4d35ef8

Please sign in to comment.