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

Request matching fails for empty bodies when "body" is one of the matchers #157

Closed
aaronwolen opened this issue Feb 19, 2020 · 6 comments · Fixed by #161
Closed

Request matching fails for empty bodies when "body" is one of the matchers #157

aaronwolen opened this issue Feb 19, 2020 · 6 comments · Fixed by #161
Labels
Milestone

Comments

@aaronwolen
Copy link
Member

library(vcr)
library(crul)

cli <- HttpClient$new(url = "https://httpbin.org")
vcr_configure(dir = "test-fixtures", match_requests_on = c("method", "uri", "body"))
#> <vcr configuration>
#>   Cassette Dir: test-fixtures
#>   Record: once
#>   URI Parser: crul::url_parse
#>   Match Requests on: method, uri, body
#>   Preserve Bytes?: FALSE
#>   Logging?: FALSE
#>   ignored hosts: 
#>   ignore localhost?: FALSE
#>   Write disk path:

aa <- use_cassette(name = "test-empty", {
  res1 <- cli$get("get")
})
#> CrulAdapter enabled!
#> HttrAdapter enabled!
#> net connect allowed
#> net connect disabled
#> encoding couldn't be detected; assuming UTF-8
#> ejecting cassette: test-empty
#> CrulAdapter disabled!
#> HttrAdapter disabled!

Same request fails to match w/ "body" matching turned on

bb <- use_cassette(name = "test-empty", {
  res2 <- cli$get("get")
})
#> CrulAdapter enabled!
#> HttrAdapter enabled!
#> net connect allowed
#> Error: 
#> 
#> ================================================================================
#> An HTTP request has been made that vcr does not know how to handle:
#> GET https://httpbin.org/get
#> vcr is currently using the following cassette:
#>   - test-fixtures/test-empty.yml
#>     - record_mode: once
#>     - match_requests_on: method, uri, body
#> 
#> 
#> Under the current configuration vcr can not find a suitable HTTP interaction
#> to replay and is prevented from recording new requests. There are a few ways
#> you can deal with this:
#> 
#>   * If you're surprised vcr is raising this error
#>     and want insight about how vcr attempted to handle the request,
#>     you can use 'logging' to see more details [1].
#> 
#>   * You can use the :new_episodes record mode to allow vcr to
#>     record this new request to the existing cassette [2].
#> 
#>   * If you want vcr to ignore this request (and others like it), you can
#>     set an `ignore_request` function [3].
#> 
#>   * The current record mode ('once') does not allow new requests to be recorded
#>     to a previously recorded cassette. You can delete the cassette file and re-run
#>     your tests to allow the cassette to be recorded with this request [4].
#> 
#>   * The cassette contains 1 HTTP interaction that has not been
#>     played back. If your request is non-deterministic, you may need to
#>     change your 'match_requests_on' cassette option to be more lenient
#>     or use a custom request matcher to allow it to match [5].
#> 
#> [1] https://books.ropensci.org/http-testing/logging
#> 
#> [2] https://books.ropensci.org/http-testing/record-modes.html#new_episodes
#> 
#> [3] https://books.ropensci.org/http-testing/vcr-configuration#config-ignore-requests
#> 
#> [4] https://books.ropensci.org/http-testing/record-modes.html#once
#> 
#> [5] https://books.ropensci.org/http-testing/request-matching
#> 
#> ================================================================================
#> ejecting cassette: test-empty
#> CrulAdapter disabled!
#> HttrAdapter disabled!

but matches fine when "body" matching is disabled:

cc <- use_cassette(name = "test-empty", {
  res3 <- cli$get("get")
}, match_requests_on = c("method", "uri"))
#> CrulAdapter enabled!
#> HttrAdapter enabled!
#> net connect disabled
#> ejecting cassette: test-empty
#> CrulAdapter disabled!
#> HttrAdapter disabled!

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.2 (2019-12-12)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2020-02-19                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version  date       lib source                            
#>  assertthat    0.2.1    2019-03-21 [1] CRAN (R 3.6.0)                    
#>  backports     1.1.5    2019-10-02 [1] CRAN (R 3.6.0)                    
#>  base64enc     0.1-3    2015-07-28 [1] CRAN (R 3.6.0)                    
#>  callr         3.4.2    2020-02-12 [1] CRAN (R 3.6.2)                    
#>  cli           2.0.1    2020-01-08 [1] CRAN (R 3.6.0)                    
#>  crayon        1.3.4    2017-09-16 [1] CRAN (R 3.6.0)                    
#>  crul        * 0.9.0    2019-11-06 [1] CRAN (R 3.6.0)                    
#>  curl          4.3      2019-12-02 [1] CRAN (R 3.6.0)                    
#>  desc          1.2.0    2018-05-01 [1] CRAN (R 3.6.0)                    
#>  devtools      2.2.1    2019-09-24 [1] CRAN (R 3.6.0)                    
#>  digest        0.6.24   2020-02-12 [1] CRAN (R 3.6.0)                    
#>  ellipsis      0.3.0    2019-09-20 [1] CRAN (R 3.6.0)                    
#>  evaluate      0.14     2019-05-28 [1] CRAN (R 3.6.0)                    
#>  fansi         0.4.1    2020-01-08 [1] CRAN (R 3.6.0)                    
#>  fauxpas       0.2.0    2018-03-01 [1] CRAN (R 3.6.0)                    
#>  fs            1.3.1    2019-05-06 [1] CRAN (R 3.6.0)                    
#>  glue          1.3.1    2019-03-12 [1] CRAN (R 3.6.0)                    
#>  highr         0.8      2019-03-20 [1] CRAN (R 3.6.0)                    
#>  htmltools     0.4.0    2019-10-04 [1] CRAN (R 3.6.0)                    
#>  httpcode      0.2.0    2016-11-14 [1] CRAN (R 3.6.0)                    
#>  httr          1.4.1    2019-08-05 [1] CRAN (R 3.6.0)                    
#>  jsonlite      1.6.1    2020-02-02 [1] CRAN (R 3.6.0)                    
#>  knitr         1.28     2020-02-06 [1] CRAN (R 3.6.2)                    
#>  lazyeval      0.2.2    2019-03-15 [1] CRAN (R 3.6.0)                    
#>  magrittr      1.5      2014-11-22 [1] CRAN (R 3.6.0)                    
#>  memoise       1.1.0    2017-04-21 [1] CRAN (R 3.6.0)                    
#>  pkgbuild      1.0.6    2019-10-09 [1] CRAN (R 3.6.0)                    
#>  pkgload       1.0.2    2018-10-29 [1] CRAN (R 3.6.0)                    
#>  prettyunits   1.1.1    2020-01-24 [1] CRAN (R 3.6.2)                    
#>  processx      3.4.2    2020-02-09 [1] CRAN (R 3.6.0)                    
#>  ps            1.3.2    2020-02-13 [1] CRAN (R 3.6.0)                    
#>  R6            2.4.1    2019-11-12 [1] CRAN (R 3.6.0)                    
#>  Rcpp          1.0.3    2019-11-08 [1] CRAN (R 3.6.0)                    
#>  remotes       2.1.1    2020-02-15 [1] CRAN (R 3.6.0)                    
#>  rlang         0.4.4    2020-01-28 [1] CRAN (R 3.6.0)                    
#>  rmarkdown     2.1      2020-01-20 [1] CRAN (R 3.6.0)                    
#>  rprojroot     1.3-2    2018-01-03 [1] CRAN (R 3.6.0)                    
#>  sessioninfo   1.1.1    2018-11-05 [1] CRAN (R 3.6.0)                    
#>  stringi       1.4.5    2020-01-11 [1] CRAN (R 3.6.0)                    
#>  stringr       1.4.0    2019-02-10 [1] CRAN (R 3.6.0)                    
#>  testthat      2.3.1    2019-12-01 [1] CRAN (R 3.6.0)                    
#>  triebeard     0.3.0    2016-08-04 [1] CRAN (R 3.6.0)                    
#>  urltools      1.7.3    2019-04-14 [1] CRAN (R 3.6.0)                    
#>  usethis       1.5.1    2019-07-04 [1] CRAN (R 3.6.0)                    
#>  vcr         * 0.4.1.97 2020-02-19 [1] local                             
#>  webmockr      0.5.1.98 2020-02-19 [1] Github (ropensci/webmockr@21c4b06)
#>  withr         2.1.2    2018-03-15 [1] CRAN (R 3.6.0)                    
#>  xfun          0.12     2020-01-13 [1] CRAN (R 3.6.0)                    
#>  yaml          2.2.1    2020-02-01 [1] CRAN (R 3.6.0)                    
#> 
#> [1] /Users/aaronwolen/R
#> [2] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

Created on 2020-02-19 by the reprex package (v0.3.0)

@sckott
Copy link
Collaborator

sckott commented Feb 19, 2020

thanks, having a look

@sckott sckott added the bug label Feb 20, 2020
@sckott sckott added this to the v0.5 milestone Feb 20, 2020
sckott added a commit that referenced this issue Feb 20, 2020
uses webmockr::pluck_body which returns NULL if the body is empty
if pluck_body gives NULL, change it to an empty string "" to match what is on disk in cassettes
@sckott
Copy link
Collaborator

sckott commented Feb 20, 2020

@aaronwolen see branch https://github.com/ropensci/vcr/compare/fix-empty-body-match

I think this fixes it, all existing tests and checks work with this

@sckott
Copy link
Collaborator

sckott commented Feb 20, 2020

is this change risky at all?

sckott added a commit that referenced this issue Feb 20, 2020
@sckott
Copy link
Collaborator

sckott commented Feb 20, 2020

added a test file for this

@aaronwolen
Copy link
Member Author

aaronwolen commented Feb 20, 2020

That worked for me and it seems like the right thing to do. What makes you think it might be risky?

@sckott
Copy link
Collaborator

sckott commented Feb 29, 2020

it works I think actually

sckott added a commit that referenced this issue Feb 29, 2020
uses webmockr::pluck_body which returns NULL if the body is empty
if pluck_body gives NULL, change it to an empty string "" to match what is on disk in cassettes
sckott added a commit that referenced this issue Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants