<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/webmockr-mock-http-requests/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/technotes/2018/02/20/webmockr-intro/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>webmockr</h2>
<p><code>webmockr</code> is an R library for stubbing and setting expectations on HTTP requests.<br>
It is a port of the Ruby gem <a rel="nofollow" href="https://github.com/bblimke/webmock" target="_blank">webmock</a>.</p>
<p><code>webmockr</code> works by plugging in to another R package that does HTTP requests. It currently only works with <a rel="nofollow" href="https://github.com/ropensci/crul" target="_blank">crul</a> right now, but we plan to add support for <a rel="nofollow" href="https://github.com/ropensci/curl" target="_blank">curl</a> and <a rel="nofollow" href="https://github.com/r-lib/httr" target="_blank">httr</a> later.</p>
<p><code>webmockr</code> has the following high level features:</p>
<ul>
<li>Stubbing HTTP requests at low http client level</li>
<li>Setting expectations on HTTP requests</li>
<li>Matching requests based any combination of HTTP method (e.g., GET/POST/PUT), URI (i.e., URL), request headers and body</li>
<li>Will soon integrate with <a rel="nofollow" href="https://github.com/ropensci/vcr" target="_blank">vcr</a> so that you can cache real HTTP responses – and easily integrate with <code>testthat</code></li>
</ul>
<p><code>webmockr</code> has been on CRAN for a little while now, but I’ve recently made some improvements and am nearing another<br>
CRAN release, which is also preparation for a first release of the related <a rel="nofollow" href="https://github.com/ropensci/vcr" target="_blank">vcr</a> package. The following is a<br>
run down of major features and how to use the package, including a bit on testing at the end.</p>
<p></p>
<h2>Similar art in other languages</h2>
<p><code>webmockr</code> was most closely inspired by Ruby’s <a rel="nofollow" href="https://github.com/bblimke/webmock" target="_blank">webmock</a>, but there are others out there, e.g,. <a rel="nofollow" href="https://github.com/gabrielfalcao/HTTPretty" target="_blank">HTTPretty</a> and <a rel="nofollow" href="https://github.com/h2non/pook" target="_blank">pook</a> for Python, from which we’ll steal ideas as time allows.</p>
<p></p>
<h2>Installation</h2>
<p><code>webmockr</code> is on CRAN, so you can install that version</p>
<pre><code>install.packages(&quot;webmockr&quot;)
</code></pre>
<p>But I’ve been making some improvements, so you’ll probably want the dev version:</p>
<p>Install some dependencies</p>
<pre><code>if (!requireNamespace(&quot;remotes&quot;, quietly=TRUE)) install.packages(&quot;remotes&quot;)
remotes::install_github(c(&quot;ropensci/vcr@webmockit&quot;, &quot;ropenscilabs/fauxpas&quot;))
</code></pre>
<p>Install <code>webmockr</code></p>
<pre><code>remotes::install_github(&quot;ropensci/webmockr&quot;)
</code></pre>
<p>Load <code>webmockr</code></p>
<pre><code>library(webmockr)
</code></pre>

<h2>Using webmockr</h2>
<p>After loading <code>webmockr</code></p>
<pre><code>library(webmockr)
</code></pre>
<p><code>webmockr</code> is loaded but not “turned on”. At this point <code>webmockr</code> doesn’t<br>
change anything about your HTTP requests.</p>
<p>Once you turn on <code>webmockr</code>:</p>
<pre><code>webmockr::enable()
</code></pre>
<p><code>webmockr</code> will now by default <strong>NOT</strong> allow real HTTP requests from the http<br>
libraries that adapters are loaded for (right now only <code>crul</code>).</p>
<p>Next, you’ll want to think about stubbing requests</p>
<h2>Stubbing requests</h2>
<p>Stubbing requests simply refers to the act of saying “I want all HTTP<br>
requests that match this pattern to return this thing”. The “thing”<br>
can be made up of a lot of different things.</p>
<p>First, load <code>crul</code> and enable <code>webmockr</code></p>
<pre><code>library(crul)
webmockr::enable()
</code></pre>
<p>You can stub requests based on HTTP method and uri</p>
<pre><code>stub_request(&quot;get&quot;, &quot;https://httpbin.org/get&quot;)
#> </code></pre>