Skip to content

Commit

Permalink
test: response #48
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Mar 27, 2022
1 parent 5eb0190 commit b587e26
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
1 change: 1 addition & 0 deletions R/response.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/response.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Response
# response

Code
res
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>Hello</h1>
</body>
</html>
1 change: 1 addition & 0 deletions tests/testthat/partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script>console.log('tests');</script>
6 changes: 6 additions & 0 deletions tests/testthat/render.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
[! partial.html !]
<body>
<h1>[% title %]</h1>
</body>
</html>
3 changes: 3 additions & 0 deletions tests/testthat/render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[! partial.html !]

# [% title %]
61 changes: 58 additions & 3 deletions tests/testthat/test-response.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("Response", {
test_that("response", {
# errors
expect_error(response())

Expand All @@ -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(
Expand All @@ -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, "<p>hello</p>")

# 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,
"<html><script>console.log('tests');</script> <body> <h1>hello</h1> </body></html>"
)
resp <- res$render("render.md", list(title = "hello"))
expect_equal(
resp$body,
"<script>console.log('tests');</script>\n<h1>hello</h1>"
)
})

0 comments on commit b587e26

Please sign in to comment.