body matcher
Methods
Method matches()
Match a request body pattern against a pattern
Examples
# make a request signature
bb <- RequestSignature$new(
method = "get",
uri = "https:/httpbin.org/get",
options = list(
body = list(foo = "bar", a = 5)
)
)
# make body pattern object
## FALSE
z <- BodyPattern$new(pattern = list(foo = "bar"))
z$pattern
#> $foo
#> [1] "bar"
#>
z$matches(bb$body)
#> [1] FALSE
## TRUE
z <- BodyPattern$new(pattern = list(foo = "bar", a = 5))
z$pattern
#> $foo
#> [1] "bar"
#>
#> $a
#> [1] 5
#>
z$matches(bb$body)
#> [1] TRUE
# uploads in bodies
## upload NOT in a list
bb <- RequestSignature$new(
method = "post", uri = "https:/httpbin.org/post",
options = list(body = crul::upload(system.file("CITATION")))
)
bb$body
#> Form file: CITATION (type: text/plain)
z <- BodyPattern$new(
pattern =
crul::upload(system.file("CITATION"))
)
z$pattern
#> $path
#> [1] "/usr/lib/R/library/base/CITATION"
#>
#> $type
#> [1] "text/plain"
#>
#> $name
#> NULL
#>
z$matches(bb$body)
#> [1] TRUE
## upload in a list
bb <- RequestSignature$new(
method = "post", uri = "https:/httpbin.org/post",
options = list(body = list(y = crul::upload(system.file("CITATION"))))
)
bb$body
#> $y
#> Form file: CITATION (type: text/plain)
#>
z <- BodyPattern$new(
pattern =
list(y = crul::upload(system.file("CITATION")))
)
z$pattern
#> $y
#> Form file: CITATION (type: text/plain)
#>
z$matches(bb$body)
#> [1] TRUE
# partial matching
## including
partial_incl <- including(list(foo = "bar"))
z <- BodyPattern$new(pattern = partial_incl)
z$pattern
#> $foo
#> [1] "bar"
#>
z$matches(list(foo = "bar", a = 5)) # TRUE
#> [1] TRUE
## excluding
partial_excl <- excluding(list(hello = "world"))
z <- BodyPattern$new(pattern = partial_excl)
z$pattern
#> $hello
#> [1] "world"
#>
z$matches(list(a = 5)) # TRUE
#> [1] TRUE
z$matches(list(hello = "mars", a = 5)) # TRUE
#> [1] TRUE
z$matches(list(hello = "world")) # FALSE
#> [1] FALSE