-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #126 add ability to show body diff
- can be shown upon a non-match to any stubs - can be done manually as well with a new function - uses diffobj package to do diffing
- Loading branch information
Showing
19 changed files
with
378 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#' Get the last HTTP request made | ||
#' | ||
#' @export | ||
#' @return `NULL` if no requests registered; otherwise the last | ||
#' registered request made as a `RequestSignature` class | ||
#' @examplesIf interactive() | ||
#' # no requests | ||
#' request_registry_clear() | ||
#' last_request() | ||
#' | ||
#' # a request is found | ||
#' enable() | ||
#' stub_request("head", "https://nytimes.com") | ||
#' library(crul) | ||
#' crul::ok("https://nytimes.com") | ||
#' last_request() | ||
#' | ||
#' # cleanup | ||
#' request_registry_clear() | ||
#' stub_registry_clear() | ||
last_request <- function() { | ||
last(webmockr_request_registry$request_signatures$hash)$sig | ||
} | ||
|
||
#' Get the last stub created | ||
#' | ||
#' @export | ||
#' @return `NULL` if no stubs found; otherwise the last stub created | ||
#' as a `StubbedRequest` class | ||
#' @examplesIf interactive() | ||
#' # no requests | ||
#' stub_registry_clear() | ||
#' last_stub() | ||
#' | ||
#' # a stub is found | ||
#' stub_request("head", "https://nytimes.com") | ||
#' last_stub() | ||
#' | ||
#' stub_request("post", "https://nytimes.com/stories") | ||
#' last_stub() | ||
#' | ||
#' # cleanup | ||
#' stub_registry_clear() | ||
last_stub <- function() { | ||
tmp <- last(webmockr_stub_registry$request_stubs) | ||
if (rlang::is_empty(tmp)) { | ||
return(NULL) | ||
} | ||
tmp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#' Get a diff of a stub request body and a request body from an http request | ||
#' | ||
#' @export | ||
#' @param stub object of class `StubbedRequest`. required. default is to | ||
#' call [last_stub()], which gets the last stub created | ||
#' @param request object of class `RequestSignature`. required. default is to | ||
#' call [last_request()], which gets the last stub created | ||
#' @return object of class `Diff` from the \pkg{diffobj} package | ||
#' @details If either `stub` or `request` are `NULL`, this function will | ||
#' return an error message. You may not intentionally pass in a `NULL`, but | ||
#' the return value of [last_stub()] and [last_request()] when there's | ||
#' nothing found is `NULL`. | ||
#' @examplesIf interactive() | ||
#' # stops with error if no stub and request | ||
#' request_registry_clear() | ||
#' stub_registry_clear() | ||
#' stub_body_diff() | ||
#' | ||
#' # Gives diff when there's a stub and request found - however, no request body | ||
#' stub_request("get", "https://hb.opencpu.org/get") | ||
#' enable() | ||
#' library(crul) | ||
#' HttpClient$new("https://hb.opencpu.org")$get(path = "get") | ||
#' stub_body_diff() | ||
#' | ||
#' # Gives diff when there's a stub and request found - with request body | ||
#' stub_request("post", "https://hb.opencpu.org/post") %>% | ||
#' wi_th(body = list(apple = "green")) | ||
#' enable() | ||
#' library(crul) | ||
#' HttpClient$new("https://hb.opencpu.org")$post( | ||
#' path = "post", body = list(apple = "red")) | ||
#' stub_body_diff() | ||
#' | ||
#' # Gives diff when there's a stub and request found - with request body | ||
#' stub_request("post", "https://hb.opencpu.org/post") %>% | ||
#' wi_th(body = "the quick brown fox") | ||
#' HttpClient$new("https://hb.opencpu.org")$post( | ||
#' path = "post", body = "the quick black fox") | ||
#' stub_body_diff() | ||
stub_body_diff <- function(stub = last_stub(), request = last_request()) { | ||
check_installed("diffobj") | ||
if (is_empty(stub) || is_empty(request)) { | ||
abort(c("`stub` and/or `request` are NULL or otherwise empty", | ||
"see `?stub_body_diff`")) | ||
} | ||
assert(stub, "StubbedRequest") | ||
assert(request, "RequestSignature") | ||
diffobj::diffObj(stub$body, request$body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.