webmockr icon indicating copy to clipboard operation
webmockr copied to clipboard

Make it possible to flexibly compare XML bodies

Open sckott opened this issue 1 year ago • 1 comments

Right now a user has to give an xml class item to stub_request to match to an xml request body because for request bodies if we detect xml we just use xml2::read_xml which gives a xml_document class, which appears we can not compare to even another xml_doc probably because they have different pointers

x = read_xml("<foo> <bar> text <baz/> </bar> </foo>")
y = read_xml("<foo> <bar> text <baz/> </bar> </foo>")
identical(x, y)
#> [1] FALSE

What we're aiming for is to be able to do the following

stub_request(:post, "www.example.com").
  with(body: {data: {a: '1', b: 'five'}})

RestClient.post('www.example.com', "data[a]=1&data[b]=five",
  content_type: 'application/x-www-form-urlencoded')    # ===> Success

RestClient.post('www.example.com', '{"data":{"a":"1","b":"five"}}',
  content_type: 'application/json')    # ===> Success

RestClient.post('www.example.com', '<data a="1" b="five" />',
  content_type: 'application/xml')    # ===> Success

In which the stub can just have an R list while the request body has XML (or JSON or a list itself), but we can still compare them

sckott avatar Oct 14 '24 16:10 sckott

some work done in commit above - not totally sure this is done yet, to do at least:

  • [ ] new warning in xml handling section of body_as_hash seems to get supressed, not sure why yet

sckott avatar Oct 15 '24 04:10 sckott