Skip to content

Commit

Permalink
Make some snapshot tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Apr 8, 2024
1 parent e1e9a0f commit 1db6a87
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"args": {},
"headers": {
"Host": "127.0.0.1:<port>",
"Accept-Encoding": "deflate, gzip",
"Accept-Encoding": "<encodings>",
"accept": "application/json",
"content-type": "application/json",
"user-agent": "R-hub client"
Expand Down Expand Up @@ -55,7 +55,7 @@
"form": {},
"headers": {
"Host": "127.0.0.1:<port>",
"Accept-Encoding": "deflate, gzip",
"Accept-Encoding": "<encodings>",
"accept": "application/json",
"content-type": "application/json",
"user-agent": "R-hub client",
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ http <- webfakes::new_app_process(http_app())
redact_port <- function(x) {
gsub(":[0-9]+", ":<port>", x)
}

redact_ae_header <- function(x) {
gsub(
"\"Accept[-]Encoding\": \"[^\"]*\"",
"\"Accept-Encoding\": \"<encodings>\"",
x
)
}
4 changes: 2 additions & 2 deletions tests/testthat/test-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("query GET", {
withr::local_envvar(RHUB_SERVER = http$url())
expect_snapshot({
cat(rawToChar(query("/get")$content))
}, transform = redact_port)
}, transform = function(x) redact_port(redact_ae_header(x)))
})

test_that("query HTTP errors", {
Expand All @@ -23,7 +23,7 @@ test_that("query POST", {
data <- charToRaw(jsonlite::toJSON(list(foo = "bar", foobar = 1:3)))
expect_snapshot({
cat(rawToChar(query("/post", method = "POST", data = data)$content))
}, transform = redact_port)
}, transform = function(x) redact_port(redact_ae_header(x)))
})

test_that("query, unknown verb", {
Expand Down

0 comments on commit 1db6a87

Please sign in to comment.