nealrichardson / httptest

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

expect_verb should allow regexes in the url argument #19

Closed npelikan closed 5 years ago

npelikan commented 5 years ago

Happy to take lead on the PR for this one, would like to start discussing some design first though.

I have a function that writes a file via a PUT request, however the design of the API requires a file name as part of the URL. As the file name is unimportant and I don't want to overwrite past files, I use a UUID for file name, ergo the urls can be something like https://fake.com/<path>/<random_uuid>.

I'd love to test this function using something like

httptest::with_fake_http({
        httptest::expect_PUT(
            object = my_function(file=file),
            url = "https://fake\\.com/path/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
        )
    })

What would make most sense design-wise for implementing this? I was thinking a boolean argument to expect_verb, something like:

expect_PUT(object, url = "", ..., .url_regex=FALSE)

does that work for you?

nealrichardson commented 5 years ago

The issue is that the fixed argument that gets passed to testthat::expect_error et al. is hard-coded to TRUE inside expect_mock_request here: https://github.com/nealrichardson/httptest/blob/master/R/expect-request.R#L70

A better solution would be to have fixed = TRUE as a default argument in L68, and then you should be able to expect_PUT(..., fixed=FALSE) to use regular expression matching. That way, we're using the conventions/arguments that already exist in testthat. Would you be interested in trying that?

npelikan commented 5 years ago

yup, works for me!

nealrichardson commented 5 years ago

Ok, just added full regular expression support to the expectations in that commit. Please give that a try and let me know if I missed anything.