nealrichardson / httptest

A Test Environment for HTTP Requests in R
https://enpiar.com/r/httptest/
Other
79 stars 10 forks source link

Integration tests with captured requests #62

Closed robertdj closed 3 years ago

robertdj commented 3 years ago

Hi

First: I'm really enjoying this package!

Second: I have question about how I can use the captured requests as part of integration tests. My situation is that I use {httptest} to run fast unit tests with mocked HTTP calls. But (sometimes) I also want to actually make the call and compare the result with the captured request. This has saved at least one regression in the API.

I can see something like this working:

response <- <function to make the call>
old_response_file <- file.path("capture folder", paste0(httptest::build_mock_url(response$request), ".json"))
# Compare httr::content(response, as = "text") with content of "old_response_file"

This would some extra lines of code in each integration test, but it seems to me that save_response does most of this already. Have I overlooked a feature in {httptest} to do this? If not, would you be interested in such a function?

nealrichardson commented 3 years ago

Just so I understand: you want to make real API requests and then compare the responses with your previously recorded response mocks?

You could do capture_requests() with the real requests and compare the directory contents to your mock dir. with_mock_dir() might be convenient here as it uses mocks if they exist and records them if they do not. Something like:

with_mock_dir("existing_api", {
  do_stuff()
})

new_api <- tempfile()
with_mock_dir("new_api", {
  skip_on_cran() # Since this will make actual HTTP requests
  do_stuff()
})

test_that("the API has not changed", {
  for (f in dir(test_path("existing_api")) {
    # compare `f` with `file.path(new_api, f)`
  }
})
robertdj commented 3 years ago

Yes, you are spot on :-)

That looks like a good solution -- thanks!