Mocking writing to disk

Examples

# enable mocking
enable()
#> CrulAdapter enabled!
#> HttrAdapter enabled!
#> Httr2Adapter enabled!

# Write to a file before mocked request -------------

# crul
library(crul)
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
#> [1] "{\"hello\":\"world\"}"
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(body = file(f))
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: /tmp/Rtmpf6SnBV/file180b3a51147a74.json
#>     response_headers: 
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
(out <- HttpClient$new("https://httpbin.org/get")$get(disk = f))
#> <crul response> 
#>   url: https://httpbin.org/get
#>   request_headers: 
#>     User-Agent: libcurl/7.81.0 r-curl/6.4.0 crul/1.5.0
#>     Accept-Encoding: gzip, deflate
#>     Accept: application/json, text/xml, application/xml, */*
#>   response_headers: 
#>     status: HTTP/2 200
#>     date: Tue, 29 Jul 2025 19:12:28 GMT
#>     content-type: application/json
#>     content-length: 368
#>     server: gunicorn/19.9.0
#>     access-control-allow-origin: *
#>     access-control-allow-credentials: true
#>   status: 200
out$content
#> [1] "/tmp/Rtmpf6SnBV/file180b3a51147a74.json"
readLines(out$content)
#>  [1] "{"                                                                     
#>  [2] "  \"args\": {}, "                                                      
#>  [3] "  \"headers\": {"                                                      
#>  [4] "    \"Accept\": \"application/json, text/xml, application/xml, */*\", "
#>  [5] "    \"Accept-Encoding\": \"gzip, deflate\", "                          
#>  [6] "    \"Host\": \"httpbin.org\", "                                       
#>  [7] "    \"User-Agent\": \"libcurl/7.81.0 r-curl/6.4.0 crul/1.5.0\", "      
#>  [8] "    \"X-Amzn-Trace-Id\": \"Root=1-68891d1c-2b37aa8817c26ef2578f3519\"" 
#>  [9] "  }, "                                                                 
#> [10] "  \"origin\": \"3.145.237.107\", "                                     
#> [11] "  \"url\": \"https://httpbin.org/get\""                                
#> [12] "}"                                                                     
stub_registry_clear()

# httr
library(httr)
#> 
#> Attaching package: ‘httr’
#> The following object is masked from ‘package:crul’:
#> 
#>     handle
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
#> [1] "{\"hello\":\"world\"}"
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = file(f),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: /tmp/Rtmpf6SnBV/file180b3a28019684.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
## with httr, you must set overwrite=TRUE or you'll get an errror
out <- GET("https://httpbin.org/get", write_disk(f, overwrite = TRUE))
out
#> Response [https://httpbin.org/get]
#>   Date: 2025-07-29 19:12
#>   Status: 200
#>   Content-Type: application/json
#>   Size: 18 B
#> <ON DISK>  /tmp/Rtmpf6SnBV/file180b3a28019684.json
out$content
#> [1] "/tmp/Rtmpf6SnBV/file180b3a28019684.json"
#> attr(,"class")
#> [1] "path"
content(out, "text", encoding = "UTF-8")
#> [1] "{\"hello\":\"world\"}\n"
stub_registry_clear()

# httr2
library(httr2)
#> 
#> Attaching package: ‘httr2’
#> The following objects are masked from ‘package:crul’:
#> 
#>     url_build, url_parse
#> The following object is masked from ‘package:webmockr’:
#> 
#>     last_request
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
#> [1] "{\"hello\":\"world\"}"
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = file(f),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: /tmp/Rtmpf6SnBV/file180b3a1f33bfbf.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
req <- request("https://httpbin.org/get")
out <- req_perform(req, path = f)
out
#> <httr2_response>
#> GET https://httpbin.org/get
#> Status: 200 OK
#> Content-Type: application/json
#> Body: On disk /tmp/Rtmpf6SnBV/file180b3a1f33bfbf.json (18 bytes)
out$body
#> [1] "/tmp/Rtmpf6SnBV/file180b3a1f33bfbf.json"
#> attr(,"class")
#> [1] "httr2_path"
out$headers
#> <httr2_headers>
#> content-type: application/json
readLines(out$body)
#> [1] "{\"hello\":\"world\"}"
stub_registry_clear()

# Use mock_file to have webmockr handle file and contents -------------

# crul
library(crul)
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(body = mock_file(f, "{\"hello\":\"mars\"}\n"))
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: mock file, path: /tmp/Rtmpf6SnBV/file180b3a3176c4d4.json
#>     response_headers: 
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
(out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = f))
#> <crul response> 
#>   url: https://httpbin.org/get
#>   request_headers: 
#>     User-Agent: libcurl/7.81.0 r-curl/6.4.0 crul/1.5.0
#>     Accept-Encoding: gzip, deflate
#>     Accept: application/json, text/xml, application/xml, */*
#>   response_headers: 
#>     status: HTTP/2 200
#>     date: Tue, 29 Jul 2025 19:12:28 GMT
#>     content-type: application/json
#>     content-length: 368
#>     server: gunicorn/19.9.0
#>     access-control-allow-origin: *
#>     access-control-allow-credentials: true
#>   status: 200
out$content
#> [1] "/tmp/Rtmpf6SnBV/file180b3a3176c4d4.json"
readLines(out$content)
#>  [1] "{"                                                                     
#>  [2] "  \"args\": {}, "                                                      
#>  [3] "  \"headers\": {"                                                      
#>  [4] "    \"Accept\": \"application/json, text/xml, application/xml, */*\", "
#>  [5] "    \"Accept-Encoding\": \"gzip, deflate\", "                          
#>  [6] "    \"Host\": \"httpbin.org\", "                                       
#>  [7] "    \"User-Agent\": \"libcurl/7.81.0 r-curl/6.4.0 crul/1.5.0\", "      
#>  [8] "    \"X-Amzn-Trace-Id\": \"Root=1-68891d1c-712d495b02600d464256d4d7\"" 
#>  [9] "  }, "                                                                 
#> [10] "  \"origin\": \"3.145.237.107\", "                                     
#> [11] "  \"url\": \"https://httpbin.org/get\""                                
#> [12] "}"                                                                     
stub_registry_clear()

# httr
library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: mock file, path: /tmp/Rtmpf6SnBV/file180b3a112e1ce1.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
out <- GET("https://httpbin.org/get", write_disk(f))
out
#> Response [https://httpbin.org/get]
#>   Date: 2025-07-29 19:12
#>   Status: 200
#>   Content-Type: application/json
#>   Size: 15 B
#> <ON DISK>  /tmp/Rtmpf6SnBV/file180b3a112e1ce1.json
## view stubbed file content
out$content
#> [1] "/tmp/Rtmpf6SnBV/file180b3a112e1ce1.json"
#> attr(,"class")
#> [1] "path"
readLines(out$content)
#> [1] "{\"foo\": \"bar\"}"
content(out, "text", encoding = "UTF-8")
#> [1] "{\"foo\": \"bar\"}\n"
stub_registry_clear()

# httr2
library(httr2)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
  to_return(
    body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
    headers = list("content-type" = "application/json")
  )
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>     auth: 
#>   to_return: 
#>   - status: 
#>     body: mock file, path: /tmp/Rtmpf6SnBV/file180b3a3098ae89.json
#>     response_headers: content-type=application/json
#>     should_timeout: FALSE
#>     should_raise: FALSE
## make a request
req <- request("https://httpbin.org/get")
out <- req_perform(req, path = f)
out
#> <httr2_response>
#> GET https://httpbin.org/get
#> Status: 200 OK
#> Content-Type: application/json
#> Body: On disk /tmp/Rtmpf6SnBV/file180b3a3098ae89.json (15 bytes)
## view stubbed file content
out$body
#> [1] "/tmp/Rtmpf6SnBV/file180b3a3098ae89.json"
#> attr(,"class")
#> [1] "httr2_path"
readLines(out$body)
#> [1] "{\"foo\": \"bar\"}"
stub_registry_clear()

# disable mocking
disable()
#> CrulAdapter disabled!
#> HttrAdapter disabled!
#> Httr2Adapter disabled!