diff --git a/R/response.R b/R/response.R index 5a3c190..fba9ecb 100644 --- a/R/response.R +++ b/R/response.R @@ -135,6 +135,7 @@ Response <- R6::R6Class( deprecated_headers(headers) deprecated_status(status) headers <- private$.get_headers(headers) + headers[["Content-Type"]] <- content_plain() response(status = private$.get_status(status), headers = headers, body = convert_body(body)) }, #' @details Send a file. diff --git a/tests/testthat/_snaps/response.md b/tests/testthat/_snaps/response.md index 5f17a48..42ae723 100644 --- a/tests/testthat/_snaps/response.md +++ b/tests/testthat/_snaps/response.md @@ -1,4 +1,4 @@ -# Response +# response Code res diff --git a/tests/testthat/file.html b/tests/testthat/file.html new file mode 100644 index 0000000..b8ab859 --- /dev/null +++ b/tests/testthat/file.html @@ -0,0 +1,5 @@ + + +

Hello

+ + \ No newline at end of file diff --git a/tests/testthat/partial.html b/tests/testthat/partial.html new file mode 100644 index 0000000..90519fa --- /dev/null +++ b/tests/testthat/partial.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/testthat/render.html b/tests/testthat/render.html new file mode 100644 index 0000000..8512f83 --- /dev/null +++ b/tests/testthat/render.html @@ -0,0 +1,6 @@ + + [! partial.html !] + +

[% title %]

+ + diff --git a/tests/testthat/render.md b/tests/testthat/render.md new file mode 100644 index 0000000..9e2ea94 --- /dev/null +++ b/tests/testthat/render.md @@ -0,0 +1,3 @@ +[! partial.html !] + +# [% title %] diff --git a/tests/testthat/test-response.R b/tests/testthat/test-response.R index c7eb08d..05546fc 100644 --- a/tests/testthat/test-response.R +++ b/tests/testthat/test-response.R @@ -1,4 +1,4 @@ -test_that("Response", { +test_that("response", { # errors expect_error(response()) @@ -10,9 +10,9 @@ test_that("Response", { expect_equal(res$headers, list()) # 404 - res <- response_404("404") + res <- response_404(I("404")) expect_equal(res$status, 404L) - expect_equal(res$body, "404") + expect_equal(res$body, I("404")) expect_equal( res$headers, list( @@ -30,4 +30,59 @@ test_that("Response", { `Content-Type` = content_html() ) ) + expect_true(is_response(res)) +}) + +test_that("Response", { + res <- Response$new() + expect_s3_class(res, "Response") + + # htmltools + resp <- res$send( + htmltools::p("hello") + ) + expect_equal(resp$body, "

hello

") + + # factor + resp <- res$send(as.factor("hello")) + expect_equal(resp$body, "hello") + + # status + res$set_status(404L) + expect_equal(res$status, 404L) + + res$status <- 200L + expect_equal(res$status, 200L) + + # sendf + resp <- res$sendf("hello %s", "world") + expect_equal(resp$body, "hello world") + + # text + resp <- res$text("hello") + expect_equal(resp$body, "hello") + expect_equal( + resp$headers[["Content-Type"]], + content_plain() + ) + + # file + resp <- res$send_file("file.html") + expect_equal(nchar(resp$body), 48) + + # redirect + resp <- res$redirect("/") + expect_equal(resp$headers$Location, "/") + + # render + resp <- res$render("render.html", list(title = "hello")) + expect_equal( + resp$body, + "

hello

" + ) + resp <- res$render("render.md", list(title = "hello")) + expect_equal( + resp$body, + "\n

hello

" + ) })