Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Mar 4, 2024
1 parent 8a2f6f3 commit 2f95592
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions tests/testthat/test-outpack-http-client.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("client sends well formed requests", {
skip_if_not_installed("mockery")
verb <- mockery::mock(mock_response(json_string("[1,2,3]")))

res <- http_client_request(verb, "http://example.com", "/path")
res <- http_client_request(verb, "http://example.com/path")
expect_mapequal(
res,
list(status = "success", errors = NULL, data = list(1, 2, 3)))
Expand All @@ -18,7 +18,7 @@ test_that("client can return json verbatim as text", {
## any json processor
verb <- mockery::mock(mock_response(json_string("[1,2, 3]"), wrap = FALSE))

res <- http_client_request(verb, "http://example.com", "/path",
res <- http_client_request(verb, "http://example.com/path",
parse_json = FALSE)
expect_equal(res, "[1,2, 3]")
})
Expand All @@ -34,7 +34,7 @@ test_that("client can download files", {
mockery::stub(http_client_request, "http_client_download_options",
mock_download_options)

res <- http_client_request(verb, "http://example.com", "/path",
res <- http_client_request(verb, "http://example.com/path",
download = dest)

expect_identical(res, dest)
Expand Down Expand Up @@ -63,6 +63,21 @@ test_that("handle errors", {
})


test_that("handle errors from packit", {
str <- paste0(
'{"status":"failure",',
'"error":{"error":"NOT_FOUND","detail":"Resource not found"},',
'"data":null}')
r <- mock_response(json_string(str), status = 404, wrap = FALSE)
err <- expect_error(http_client_handle_error(r),
"Resource not found")
expect_s3_class(err, "outpack_http_client_error")
expect_equal(err$code, 404)
expect_equal(err$errors, list(list(error = "NOT_FOUND",
detail = "Resource not found")))
})


test_that("can construct sensible download options", {
path <- temp_file()
res <- http_client_download_options(path)
Expand All @@ -74,7 +89,7 @@ test_that("can construct sensible download options", {

test_that("can use the client to make requests", {
skip_if_not_installed("mockery")
cl <- outpack_http_client$new("http://example.com")
cl <- outpack_http_client$new("http://example.com", NULL)
mock_get <- mockery::mock(mock_response(json_string("[1,2,3]")))
mockery::stub(cl$get, "httr::GET", mock_get)
res <- cl$get("/path")
Expand All @@ -83,5 +98,5 @@ test_that("can use the client to make requests", {
list(status = "success", errors = NULL, data = list(1, 2, 3)))
mockery::expect_called(mock_get, 1)
expect_equal(mockery::mock_args(mock_get)[[1]],
list("http://example.com/path"))
list("http://example.com/path", NULL))
})

0 comments on commit 2f95592

Please sign in to comment.