BodyPattern: BodyPattern

BodyPatternR Documentation

BodyPattern

Description

body matcher

Public fields

pattern

a list

partial

bool, default: FALSE

partial_type

a string, default: NULL

Methods

Public methods


Method new()

Create a new BodyPattern object

Usage
BodyPattern$new(pattern)
Arguments
pattern

(list) a body object - from a request stub (i.e., the mock)

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, i.e., from the HTTP request

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
z$matches(bb$body)
## TRUE
z <- BodyPattern$new(pattern = list(foo = "bar", a = 5))
z$pattern
z$matches(bb$body)

# 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
z <- BodyPattern$new(
  pattern =
    crul::upload(system.file("CITATION"))
)
z$pattern
z$matches(bb$body)

## 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
z <- BodyPattern$new(
  pattern =
    list(y = crul::upload(system.file("CITATION")))
)
z$pattern
z$matches(bb$body)

# partial matching
## including
partial_incl <- including(list(foo = "bar"))
z <- BodyPattern$new(pattern = partial_incl)
z$pattern
z$matches(list(foo = "bar", a = 5)) # TRUE

## excluding
partial_excl <- excluding(list(hello = "world"))
z <- BodyPattern$new(pattern = partial_excl)
z$pattern
z$matches(list(a = 5)) # TRUE
z$matches(list(hello = "mars", a = 5)) # TRUE
z$matches(list(hello = "world")) # FALSE

webmockr documentation built on April 4, 2025, 12:08 a.m.