Skip to contents

body matcher

Public fields

pattern

a list

partial

bool, default: FALSE

partial_type

a string, default: NULL

Methods


Method new()

Create a new BodyPattern object

Usage

BodyPattern$new(pattern)

Arguments

pattern

(list) a body object

Returns

A new BodyPattern object


Method matches()

Match a request body pattern against a pattern

Usage

BodyPattern$matches(body, content_type = "")

Arguments

body

(list) the body

content_type

(character) content type

Returns

a boolean


Method to_s()

Print pattern for easy human consumption

Usage

BodyPattern$to_s()

Returns

a string


Method clone()

The objects of this class are cloneable with this method.

Usage

BodyPattern$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

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