nealrichardson / httptest

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

Support httr::RETRY() in capture_requests() #13

Closed MarkEdmondson1234 closed 6 years ago

MarkEdmondson1234 commented 6 years ago

Hi Neal!

I'm updating some tests and noticed that none of the mock files are being written, so giving failures when using with_mock_API()

My example:

library(googleLanguageR)
# auto auth

library(httptest)

test_text <- "Translate this"

capture_requests(
  gl_translate(test_text), path = ".", verbose = TRUE
)

with_mock_api(gl_translate(test_text))

sessionInfo()

dir.exists("translation.googleapis.com")

which gives:

> library(googleLanguageR)
> # auto auth
> 
> library(httptest)
> 
> test_text <- "Translate this"
> 
> capture_requests(
+   gl_translate(test_text), path = ".", verbose = TRUE
+ )
2018-06-15 10:28:30 -- Translating text: 14 characters - 
# A tibble: 1 x 3
  translatedText detectedSourceLanguage text          
* <chr>          <chr>                  <chr>         
1 Translate this en                     Translate this
> 
> with_mock_api(gl_translate(test_text))
2018-06-15 10:28:30 -- Translating text: 14 characters - 
Error: POST https://translation.googleapis.com/language/translate/v2/ target=en&format=text&source=&q=Translate%20this (translation.googleapis.com/language/translate/v2-b2807c-POST.json)
Request failed [ERROR]. Retrying in 1 seconds...
Error: POST https://translation.googleapis.com/language/translate/v2/ target=en&format=text&source=&q=Translate%20this (translation.googleapis.com/language/translate/v2-b2807c-POST.json)
Request failed [ERROR]. Retrying in 1 seconds...
Error : POST https://translation.googleapis.com/language/translate/v2/ target=en&format=text&source=&q=Translate%20this (translation.googleapis.com/language/translate/v2-b2807c-POST.json)
 Show Traceback

 Rerun with Debug
 Error: Request failed before finding status code: POST https://translation.googleapis.com/language/translate/v2/ target=en&format=text&source=&q=Translate%20this (translation.googleapis.com/language/translate/v2-b2807c-POST.json) > 
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] httptest_3.0.1             testthat_2.0.0             googleLanguageR_0.1.1.9000

loaded via a namespace (and not attached):
 [1] utf8_1.1.4        crayon_1.3.4      digest_0.6.15     assertthat_0.2.0  R6_2.2.2         
 [6] jsonlite_1.5      magrittr_1.5      pillar_1.2.3      httr_1.3.1        cli_1.0.0        
[11] rlang_0.2.1       curl_3.2          rstudioapi_0.7    googleAuthR_0.6.2 tools_3.5.0      
[16] purrr_0.2.5       yaml_2.1.19       compiler_3.5.0    base64enc_0.1-3   memoise_1.1.0    
[21] openssl_1.0.1     tibble_1.4.2     
> 
> dir.exists("translation.googleapis.com")
[1] FALSE
nealrichardson commented 6 years ago

Thanks for the report, Mark. capture_requests works using the base::trace() function, which is something of a dark art. I suspect the problem is that the httr::RETRY() method isn't being traced, and that's how your function is making its requests. I'll start there and add support for that (and your timing is good as I'm looking to make a new httptest release shortly).

If you want an immediate workaround, I suspect if you changed https://github.com/MarkEdmondson1234/googleAuthR/blob/af4102c2f1518f6a7e413bf727f85be8af5e69ec/R/generator.R#L330-L332 to

req <- retryRequest(do.call(arg_list$verb
                              args = arg_list[names(arg_list) != "verb"],
                              envir = asNamespace("httr")))

it would directly call POST, the capture_requests tracing would kick in, and you'd get your mocks recorded. (If so, that would also confirm the lack of RETRY() support as the only issue in httptest to fix.)

nealrichardson commented 6 years ago

Ok, I believe this issue is resolved with that change to support RETRY(). Please let me know if it does not address everything for you.

MarkEdmondson1234 commented 6 years ago

Thanks, all working now!