nealrichardson / httptest

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

Testing GET Rest API with http://{domain}/ #1

Closed byapparov closed 7 years ago

byapparov commented 7 years ago

Hi,

I am not sure how to use with_mock_API against the full URL that contains http(s)://{domain}. Where the json files should be located in this case?

For example if I want to do something like this in my package:

    response <- GET(url  = "https://mydomain.zendesk.com",
                    path = "api/v2/incremental/tickets.json?update_date=3423423",
                    add_headers("Content-Type" = "application/json"),
                    authenticate(user = Sys.getenv("ZENDESK_USER"),
                                 password = Sys.getenv("ZENDESK_PASSWORD"))

Can I mock the response?

nealrichardson commented 7 years ago

Thanks! I see that with_mock_API did not work with real URLs (those starting with http:// or any other protocol), only mock files paths. The commit linked above addresses that. Can you pull that branch and see if it meets your needs? If not, could you please add a failing test or two to test-mock-api.R and make a pull request back?

With the new commit, your mock for that request would go in tests/testthat/mydomain.zendesk.com/api/v2/incremental/tickets.json plus some hash of the querystring, I think. If you write the test, the with_mock_API context will tell you where it looked for the file if it doesn't find one.

byapparov commented 7 years ago

Thank you @nealrichardson, it works fine. Very useful package!

nealrichardson commented 7 years ago

Ok, merged that branch in and tagged a release: https://github.com/nealrichardson/httptest/releases/tag/1.1.2

Can't put it on CRAN just yet because the previous release was only a few days ago, but you can devtools::install_github that tag. Thanks for your feedback!