nealrichardson / httptest

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

Works in RStudio (and Travis) but not at command line #27

Closed datawookie closed 4 years ago

datawookie commented 4 years ago

Hi!

I've been writing tests for an API wrapper package and {httptest} has been a complete game changer. Thanks for your work on this.

I have run into a snag though. My tests work 100% fine in RStudio and also under Travis. The serialised results from the API calls are stored in a folder called mainnet.infura.io under tests/testthat. This is the contents of that folder:

$ ls -l mainnet.infura.io/
-rw-rw-r--   60 Jan 25 13:13 9BvO7Cvbe3p5FpinlbXv-14a098-POST.json
-rw-rw-r--  768 Jan 25 13:13 9BvO7Cvbe3p5FpinlbXv-23dd28-POST.json
-rw-rw-r--  768 Jan 25 13:13 9BvO7Cvbe3p5FpinlbXv-5c5555-POST.json

But when I build a source package and then run R CMD check the tests fail. Here is the error message:

── 1. Error: valid protocol version (@test-eth.R#11)  ──────────────────────────
POST https://mainnet.infura.io/ {"jsonrpc":"2.0","method":"eth_protocolVersion","id":1,"params":[]} (mainnet.infura.io-14a098-POST.json)
Backtrace:
  1. testthat::expect_type(eth_protocolVersion(), "integer")
  4. ether::eth_protocolVersion()
  8. ether:::get_post_response("eth_protocolVersion")
 12. httr::POST(...)
 13. httr:::request_perform(req, hu$handle$handle)
 14. httr:::request_fetch(req$output, req$url, handle)
 16. httptest:::stop_request(req)

── 2. Error: can retrieve transactions (@test-eth.R#17)  ───────────────────────
POST https://mainnet.infura.io/ {"jsonrpc":"2.0","method":"eth_getTransactionByBlockHashAndIndex","id":1,"params":["0xd3ca9f0659473d93a00dc2076c2c9e8d7805243c59c8cb0d3501b262f98a15f1","0x0"]} (mainnet.infura.io-23dd28-POST.json)
Backtrace:
  1. ether::eth_getTransactionByBlockHashAndIndex(...)
  2. ether:::get_post_response(...)
  6. httr::POST(...)
  7. httr:::request_perform(req, hu$handle$handle)
  8. httr:::request_fetch(req$output, req$url, handle)
 10. httptest:::stop_request(req)

My understanding is that {httptest} is now looking for the API results in mainnet.infura.io-14a098-POST.json and mainnet.infura.io-23dd28-POST.json.

I'm not sure why the names and location of these files differ under these different scenarios. I've hit my head against this for a bit and I'd really appreciate some help.

Thanks, Andrew.

nealrichardson commented 4 years ago

Hmm, hard to say without seeing the code. My first guess when things work interactively but not in R CMD check is that file paths are involved (since it will install into a tmp dir), but it's not obvious to me where that might be here.

The odd thing is that mainnet.infura.io/9BvO7Cvbe3p5FpinlbXv-14a098-POST.json exists but it's looking for mainnet.infura.io-14a098-POST.json. The payload hash is the same, so the difference is that /9BvO7Cvbe3p5FpinlbXv URI segment is missing. Does that string mean anything to you? A user id that is set in an environment variable, maybe? If so, consider Sys.setenv("THAT_ENV_VAR", "test-user") or something in your test suite so that it's a fixed value (or similar, if this isn't exactly the case).

datawookie commented 4 years ago

Hi @nealrichardson, yes! That's precisely what the problem was. I should have realised the significance. Thanks for pointing me in the right direction. Best regards, Andrew.