From 2f955924aa81261bce6a3d6d3b94a9df96f99308 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Mon, 4 Mar 2024 14:57:23 +0000 Subject: [PATCH] Fix tests --- tests/testthat/test-outpack-http-client.R | 25 ++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-outpack-http-client.R b/tests/testthat/test-outpack-http-client.R index 7017eeaf..75112203 100644 --- a/tests/testthat/test-outpack-http-client.R +++ b/tests/testthat/test-outpack-http-client.R @@ -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))) @@ -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]") }) @@ -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) @@ -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) @@ -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") @@ -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)) })