httptest2 icon indicating copy to clipboard operation
httptest2 copied to clipboard

mocking iterative requests

Open asadow opened this issue 1 year ago • 1 comments

This is more for a discussion, and is a copy of an issue I thought might belong in httr2.

My function my_perform() uses httr2::req_perform_iterative() which has a max_reqs argument (default to 20). I'd like to test that my_perform() performs an infinite or very large number of iterative requests.

For now I am testing the argument:

test_that("my_perform() performs Inf requests by default", {
  expect_equal(formals(my_perform)$max_reqs, Inf)
})

Is this ideal or is it instead better to mock a large request? I created a fake response with data removed which allows for

with_mock_api({
  test_that("my_perform() returns API page_count responses", {
    responses<- my_req("my_endpoint") |> my_perform()
    expect_length(responses, 21)
  })
})

(And I don't understand why the above requires 2 mock files, endpoint.R and endpoint-6fdf2d.R.)

asadow avatar Jul 10 '24 12:07 asadow

(Moved this from httptest to httptest2)

I don't think you want to test that it actually makes an infinite number of requests. I would do what I gather you're doing with the fake responses, and test that your function does the iteration as expected, and you get the right results that would only get if it correctly paged through the API as you expect.

(And I don't understand why the above requires 2 mock files, endpoint.R and endpoint-6fdf2d.R.)

I can't say without seeing your code, but my guess is that the iterative next_req function you have is adding a query parameter to fetch the next page of results or whatever, and 6fdf2d is the hash of that added query string.

nealrichardson avatar Jul 23 '24 14:07 nealrichardson

Closing as this is on the right track

asadow avatar Jan 02 '25 19:01 asadow