Skip to content

Conversation

@hadley
Copy link
Member

@hadley hadley commented Aug 31, 2021

@sckott, @nealrichardson how does this look to you?

Copy link
Contributor

@nealrichardson nealrichardson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally makes sense to me and I ❤️ how small the diff is

if (!is.null(mock)) {
mock <- as_function(mock)
mock_resp <- mock(req)
if (!is.null(mock_resp)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check for NULL specifically? If anything I'd think stopifnot(is_response(mock_resp)) would make sense, or just let the mock function return whatever it wants.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the function can opt out of handling a request

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see you said that in the docs. Is there a use case you had in mind for this? It's not something I had considered; I'd think either you're mocking everything or nothing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno, it just seems weird to completely take over all http requests with no way to opt out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mocking is weird ;) I think it would be problematic if you're mocking responses but some requests silently pass through and call out to the internet, so I can't imagine a use case for this. But I guess here you'd have to explicitly design your mock function to return NULL in those cases, so you'd have control over that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I think this design lets you opt out if you want to, but you'd have to deliberately design your function to do so.

return(mock_resp)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the mock should instead be run after the next few lines since they appear to modify req (though I haven't looked at what they do). I vaguely remember some having to handle some subtlety in httptest because of default values getting set somewhere deep down that weren't a part of the request object earlier. It seems that here, for example, you wouldn't be able to test anything about auth or oauth tokens being set in the request because that doesn't get added to the request object until after the mock is run.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think req_verbosity() is meaningful for mocking; it's only used for debugging live requests.

And the challenge with OAuth is that signing process might need to perform its own requests (e.g. in order to refresh a token) — you'd then have to be careful to also mock those requests, which is going to be hard because they only occur occasionally.

@sckott
Copy link

sckott commented Sep 1, 2021

It looks good to me.

I played around with it locally and it looks like it will work with webmockr and vcr (though I didn't test an actual integration with httr2).

@hadley
Copy link
Member Author

hadley commented Sep 9, 2021

Thanks for all the feedback!

@hadley
Copy link
Member Author

hadley commented Sep 9, 2021

BTW I'm aiming to submit the first release of httr2 to CRAN next Friday. httr2 certainly isn't done, but I think it's at a point where it makes sense for other people to try it out.

@hadley hadley merged commit e2cc168 into master Sep 9, 2021
@hadley hadley deleted the mocking branch September 29, 2023 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants