Skip to content

Commit

Permalink
trailong slash and add Rook
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Aug 21, 2024
1 parent b4db48e commit 69292a8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Suggests:
fs,
lintr (>= 3.1.2),
readr,
Rook,
testthat,
withr
11 changes: 6 additions & 5 deletions R/router.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build_routes <- function() {
plumber::options_plumber(trailingSlash = TRUE)
pr <- porcelain::porcelain$new(validate = TRUE)
pr$registerHook(stage = "preserialize", function(data, req, res, value) {
res$setHeader("Access-Control-Allow-Origin", "http://localhost:3000")
Expand All @@ -7,7 +8,7 @@ build_routes <- function() {

pr$handle(get_root())
pr$handle(get_version())
pr$handle("POST", "/dataset",
pr$handle("POST", "/dataset/",
function(req, res) target_post_dataset(req, res),
serializer = plumber::serializer_unboxed_json(null = "null"))
pr$handle(get_dataset())
Expand All @@ -27,30 +28,30 @@ get_root <- function() {
get_version <- function() {
porcelain::porcelain_endpoint$new(
"GET",
"/version",
"/version/",
target_get_version,
returning = porcelain::porcelain_returning_json("Version"))
}

get_dataset <- function() {
porcelain::porcelain_endpoint$new(
"GET", "/dataset/<name>",
"GET", "/dataset/<name>/",
target_get_dataset,
returning = porcelain::porcelain_returning_json("DatasetMetadata"))
}

get_datasets <- function() {
porcelain::porcelain_endpoint$new(
"GET",
"/datasets",
"/datasets/",
target_get_datasets,
returning = porcelain::porcelain_returning_json("DatasetNames"))
}

get_trace <- function() {
porcelain::porcelain_endpoint$new(
"GET",
"/dataset/<name>/trace/<biomarker>",
"/dataset/<name>/trace/<biomarker>/",
target_get_trace,
porcelain::porcelain_input_query(disaggregate = "string",
filter = "string"),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ local_POST_dataset_request <- function(dat, filename, xcol = "day",
}
}, envir = env)

make_req("POST", "/dataset",
make_req("POST", "/dataset/",
body = request_body,
CONTENT_LENGTH = nchar(request_body),
CONTENT_TYPE = "multipart/form-data; boundary=----WebKitFormBoundaryvbfCGA1r00d8B0Vv")
Expand All @@ -67,7 +67,7 @@ local_POST_dataset_request_bad_file <- function(env = parent.frame()) {
}
}, envir = env)

make_req("POST", "/dataset",
make_req("POST", "/dataset/",
body = request_body,
CONTENT_LENGTH = nchar(request_body),
CONTENT_TYPE = "multipart/form-data; boundary=----WebKitFormBoundaryvbfCGA1r00d8B0Vv")
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-read.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("GET /dataset<name> returns 404 if dataset not found", {
router <- build_routes()
res <- router$request("GET", "/dataset/testdataset")
res <- router$request("GET", "/dataset/testdataset/")
expect_equal(res$status, 404)
validate_failure_schema(res$body)
body <- jsonlite::fromJSON(res$body)
Expand All @@ -10,7 +10,7 @@ test_that("GET /dataset<name> returns 404 if dataset not found", {

test_that("GET /trace/<biomarker> returns 404 if dataset not found", {
router <- build_routes()
res <- router$request("GET", "/dataset/testdataset/trace/ab")
res <- router$request("GET", "/dataset/testdataset/trace/ab/")
expect_equal(res$status, 404)
validate_failure_schema(res$body)
body <- jsonlite::fromJSON(res$body)
Expand All @@ -30,7 +30,7 @@ test_that("can get trace for uploaded dataset with xcol", {
router <- build_routes()
res <- router$call(request)
expect_equal(res$status, 200)
res <- router$request("GET", "/dataset/testdata/trace/ab")
res <- router$request("GET", "/dataset/testdata/trace/ab/")
expect_equal(res$status, 200)
expected_warnings <- list("span too small. fewer data values than degrees of freedom.",
"pseudoinverse used at 0.96",
Expand Down Expand Up @@ -62,7 +62,7 @@ test_that("can get disgagregated traces", {
local_add_dataset(dat,
"testdataset")
router <- build_routes()
res <- router$request("GET", "/dataset/testdataset/trace/ab",
res <- router$request("GET", "/dataset/testdataset/trace/ab/",
query = list(disaggregate = "sex"))
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
Expand All @@ -89,7 +89,7 @@ test_that("can get filtered traces", {
local_add_dataset(dat,
"testdataset")
router <- build_routes()
res <- router$request("GET", "/dataset/testdataset/trace/ab",
res <- router$request("GET", "/dataset/testdataset/trace/ab/",
query = list(filter = "sex:M"))
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
Expand All @@ -109,7 +109,7 @@ test_that("can get disgagregated and filtered traces", {
local_add_dataset(dat,
"testdataset")
router <- build_routes()
res <- router$request("GET", "/dataset/testdataset/trace/ab",
res <- router$request("GET", "/dataset/testdataset/trace/ab/",
query = list(disaggregate = "age", filter = "sex:M"))
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
Expand Down
14 changes: 10 additions & 4 deletions tests/testthat/test-router.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test_that("GET /version", {
expect_equal(res, jsonlite::unbox(as.character(packageVersion("serovizr"))))

router <- build_routes()
res_api <- router$request("GET", "/version")
res_api <- router$request("GET", "/version/")
expect_equal(res_api$status, 200)
body <- jsonlite::fromJSON(res_api$body)
expect_equal(unclass(res), unclass(body$data))
Expand Down Expand Up @@ -51,7 +51,7 @@ test_that("GET /datasets", {
local_add_dataset(data.frame(biomarker = "ab", value = 1, day = 1:10),
"anotherdataset")
router <- build_routes()
res <- router$request("GET", "/datasets")
res <- router$request("GET", "/datasets/")
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$data, c("anotherdataset", "testdataset"))
Expand All @@ -65,7 +65,7 @@ test_that("GET /dataset<name>", {
sex = c("M", "F")),
"testdataset")
router <- build_routes()
res <- router$request("GET", "/dataset/testdataset")
res <- router$request("GET", "/dataset/testdataset/")
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$data$variables$name, c("age", "sex"))
Expand All @@ -89,7 +89,7 @@ test_that("GET /dataset/<name>/trace/<biomarker>", {
})
router <- build_routes()
set.seed(1)
res <- router$request("GET", "/dataset/testdataset/trace/ab")
res <- router$request("GET", "/dataset/testdataset/trace/ab/")
expect_equal(res$status, 200)
expected_warnings <- list("span too small. fewer data values than degrees of freedom.",
"pseudoinverse used at 0.96",
Expand All @@ -107,3 +107,9 @@ test_that("GET /dataset/<name>/trace/<biomarker>", {
body <- jsonlite::fromJSON(res$body)
expect_equal(body$data, jsonlite::fromJSON(expected))
})

test_that("requests without trailing slash are redirected", {
router <- build_routes()
res_api <- router$request("GET", "/version")
expect_equal(res_api$status, 307)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test_that("can get uploaded dataset metadata with default xcol", {
res <- router$call(request)
expect_equal(res$status, 200)

res <- router$request("GET", "/dataset/testdata")
res <- router$request("GET", "/dataset/testdata/")
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$data$variables$name, c("age", "sex"))
Expand All @@ -112,7 +112,7 @@ test_that("can get uploaded dataset metadata with xcol", {
res <- router$call(request)
expect_equal(res$status, 200)

res <- router$request("GET", "/dataset/testdata")
res <- router$request("GET", "/dataset/testdata/")
expect_equal(res$status, 200)
body <- jsonlite::fromJSON(res$body)
expect_equal(body$data$variables$name, c("age", "sex"))
Expand Down

0 comments on commit 69292a8

Please sign in to comment.