nealrichardson / httptest

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

NULL response getting passed around #16

Closed jameslamb closed 6 years ago

jameslamb commented 6 years ago

Hello! Thank you for this excellent package. I use it all the time.

I encountered a strange error today. Doing this:

start_capturing()
httr::GET("dafasdfasdflkm;aslkdfmasdf.com")

Returned:

 Error in if (method != "GET") { : argument is of length zero 

Based on the docs, I would have expected httptest to write out a .R file mocking the failed request. I spent about half an hour trying to walk my way back through the code interactively tonight, but I couldn't track it down.

Hope you are able to reproduce it!

nealrichardson commented 6 years ago

Thanks! The error message you were seeing was this line, which happened when the function got NULL instead of a response object when trying to write it to a file, as it does when the request function from httr exited with an error. In the commit referenced above, I've caught that case and issued a warning instead. There's no response to record, so no file to write.

I'm assuming here that the main reason you've gotten an error from httr::GET is either something transient (e.g. your network is down, libcurl is configured wrong, etc.) or something accidental (e.g. you typed a bogus URL for a host that does not exist), so you don't want to record an error mock file for this, and the warning is more helpful than leaving behind some junk mock files for you to delete. If you do want to test how your code handles the case where GET or some other request method errors, you can make a .R mock file yourself with stop("Some message!") in it, and when with_mock_api() tries to load it, the error will be thrown.

Please let me know if this does not address your issue. Thanks again for reporting!

jameslamb commented 6 years ago

Thanks for the prompt reply @nealrichardson !

That all makes sense to me. Can you point to some documentation on how to set up that .R mock file? I haven't found an example of it in your vignettes or roxygen stuff. I was using capture_requests because I wanted to try to mock out some 4xx and 5xx responses.

Mainly I'm wondering how to figure out what the expected file path is. I've been able to do this with 2xx stuff without issue (thanks again for making it so user-friendly!) but not sure where to start on non-successful requests.

Cheers,

-James

nealrichardson commented 6 years ago

With httr, 4xx or 5xx responses don't error like that. They return a response object that has a status_code of 400 or whatever. To get a feel for how httr does this, or even to set up your tests, try making requests at httpbin.org: you can get any response status you want from them, e.g. http://httpbin.org/status/418 returns 418 status. You could record that and then rename it to whatever path you need it to be for your tests.

For httptest, the expected file path is whatever it would be for a successful response--the response status doesn't affect the file path (other than perhaps the extension because error responses (currently) are always written as verbose .R files), only the contents of the file.

The best reference I have for how to test >400 status responses is probably https://enpiar.com/2017/06/21/7-hard-testing-problems-made-easy-by-httptest/, which links to some real examples as well.