Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible webmockr integration #174

Closed
wants to merge 17 commits into from
Closed

possible webmockr integration #174

wants to merge 17 commits into from

Conversation

sckott
Copy link

@sckott sckott commented Jan 15, 2019

@jeroen opening this to start a discussion to see if you'd be willing to incorporate changes for webmockr - if so, if you have any thoughts on the changes I've made.

A few people have requested curl integration for webmockr, suggesting there's more than that that would use it.

I want an even smaller change (few lines of code) to curl than I've made in this PR - perhaps you can advise on how.

The only thing changed right now in my fork of curl is curl_fetch_memory(): I first check if webmockr is installed, if not move on. If installed, then check if webmocking for curl is enabled; if so then proceed to call webmockr::curl_mock_req: https://github.com/ropensci/webmockr/blob/adapter-curl/R/curl_mocking.R

example:

remotes::install_github("sckott/curl@mocking-minimal-footprint")
remotes::install_github("ropensci/webmockr@adapter-curl")
library(curl)
library(webmockr)

# before mocking turned on
h <- new_handle()
curl_fetch_memory("https://httpbin.org/get?foo=bar", h)
#> normal curl response
# after mocking turned on
enable("curl")
# fails with webmockr message
curl_fetch_memory("https://httpbin.org/get?foo=bar", h) 
#> Error: Real HTTP connections are disabled.
#> Unregistered request:
#>   GET:  https://httpbin.org/get?foo=bar   with headers {accept: */*, accept-encoding: gzip, deflate, host: localhost:9359, user-agent: R (3.5.2 x86_64-apple-darwin15.6.0 x86_64 darwin15.6.0)}
#> 
#> You can stub this request with the following snippet:
#> 
#>    stub_request('get', uri = 'https://httpbin.org/get?foo=bar') %>%
#>      wi_th(
#>        headers = list('accept' = '*/*', 'accept-encoding' = 'gzip, deflate', 'host' = 'localhost:9359', 'user-agent' = 'R (3.5.2 x86_64-apple-darwin15.6.0 x86_64 darwin15.6.0)')
#>      )
#> ============================================================
# after stubbing request
stub_request("get", "https://httpbin.org/get?foo=bar")
#> <webmockr stub>
#>   method: get
#>   uri: https://httpbin.org/get?foo=bar
#>   with:
#>     query:
#>     body:
#>     request_headers:
#>   to_return:
#>     status:
#>     body:
#>     response_headers:
#>   should_timeout: FALSE
#>   should_raise: FALS
# now you get a mocked response
curl_fetch_memory("https://httpbin.org/get?foo=bar", h)
#>  $url
#>  [1] "https://httpbin.org/get?foo=bar"
#>  
#>  $status_code
#>  [1] 200
#>  
#>  $type
#>  [1] NA
#>  
#>  $headers
#>  raw(0)
#>  
#>  $modified
#>  [1] NA
#>  
#>  $times
#>  numeric(0)
#>  
#>  $content
#>  raw(0)

other things:

@jeroen
Copy link
Owner

jeroen commented Feb 26, 2019

Note from call: consider conditioning the callback on a unique secret header.

@codecov-io
Copy link

Codecov Report

Merging #174 into master will decrease coverage by 0.03%.
The diff coverage is 71.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #174      +/-   ##
==========================================
- Coverage   78.81%   78.77%   -0.04%     
==========================================
  Files          35       35              
  Lines        1369     1376       +7     
==========================================
+ Hits         1079     1084       +5     
- Misses        290      292       +2
Impacted Files Coverage Δ
R/fetch.R 40.62% <71.42%> (+8.62%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1a51e8f...e281d3c. Read the comment docs.

@jeroen
Copy link
Owner

jeroen commented May 24, 2019

I'm curious, how does webmockr::curl_mock_req know what is inside the handle? How does it know what the mocked request is?

@jeroen
Copy link
Owner

jeroen commented May 24, 2019

If curl would provide a way to explicitly let the handle be mock-able e.g. handle_allow_mock(h) or by setting a given request header, would that work, or is that impractical for how mocking is used?

@sckott
Copy link
Author

sckott commented May 24, 2019

how does webmockr::curl_mock_req know what is inside the handle?

by running curl::curl_echo https://github.com/ropensci/webmockr/blob/adapter-curl/R/curl_mocking.R#L25

How does it know what the mocked request is?

this https://github.com/ropensci/webmockr/blob/adapter-curl/R/curl_mocking.R#L37-L38 will pick up the stub for the matched request signature from webmockr

@sckott
Copy link
Author

sckott commented May 24, 2019

If curl would provide a way to explicitly let the handle be mock-able ...

would make things easier for sure as wouldn't have to run curl_echo

@sckott sckott mentioned this pull request Aug 9, 2019
25 tasks
@sckott
Copy link
Author

sckott commented Nov 7, 2019

closing for now - we talked and decided to not do this

@sckott sckott closed this Nov 7, 2019
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