File: test-req-error.R

package info (click to toggle)
r-cran-httr2 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,604 kB
  • sloc: sh: 21; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
test_that("can customise what statuses are errors", {
  req <- request_test()
  expect_equal(error_is_error(req, response(404)), TRUE)
  expect_equal(error_is_error(req, response(200)), FALSE)

  req <- req %>% req_error(is_error = ~ !resp_is_error(.x))
  expect_equal(error_is_error(req, response(404)), FALSE)
  expect_equal(error_is_error(req, response(200)), TRUE)
})

test_that("can customise error info", {
  req <- request_test()
  expect_equal(error_body(req, response(404)), NULL)

  req <- req %>% req_error(body = ~"Hi!")
  expect_equal(error_body(req, response(404)), "Hi!")
})

test_that("failing callback still generates useful body", {
  req <- request_test() %>% req_error(body = ~ abort("This is an error!"))
  expect_snapshot_error(error_body(req, response(404)))

  out <- expect_snapshot(error = TRUE, {
    req <- request_test("/status/404")
    req <- req %>% req_error(body = ~ resp_body_json(.x)$error)
    req %>% req_perform()
  })
})