nealrichardson / httptest

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

How to test GET with write_disk() #86

Open teotenn opened 1 year ago

teotenn commented 1 year ago

Hi, I am trying to test a function that uses GET but we are mainly focused on its side effect of write_disk(). In short, the following piece of code should write the file to a particular location and then process it further

GET(
        datasource_link,
        write_disk(tmp_file, overwrite = TRUE)
      )

If I execute the code above the file will be written to getwd() under the name tmp_file. But when I execute it inside with_mock_api I cannot find the tmp_file anywere and thus, my function cannot continue.

Is there a way to work around that?

I've been reading the code and searching in the issue but I cannot figure it out.

nealrichardson commented 9 months ago

There is handling for write_disk: https://github.com/nealrichardson/httptest/blob/main/R/capture-requests.R#L168-L180

Here is where it is tested: https://github.com/nealrichardson/httptest/blob/main/tests/testthat/test-capture-requests.R#L1-L76

The tests check that you can get the content() of a disk-backed response, but I don't recall whether it moves the mock file into the file path given to write_disk(), so it's possible that's not covered. I'm not sure whether that's the intended way to access responses in httr, or whether you should always use content(). Happy to consider a change if one is needed.