Allow matching on request body
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
requests-mock |
Fix Released
|
Medium
|
Jamie Lennox |
Bug Description
From Doug Ramirez on blog comments:
Is there anyway to have context sensitive Real HTTP Requests? For example, my app calls a service and POSTs different XML blobs and I only want to mock certain calls. So, based on the content of data in the POST to the url I'd like to mock that one call, but let the others go through. Here's an example without any mocking (pseudocode):
import requests
def _post(url, data):
with requests.Session() as session:
response = session.post(url, data=data)
return response
def test_foo_bar():
url = 'http://
# Let this call actually hit the foo.bar service
data = '<stuff>Test 1</stuff>'
response1 = _post(url, data)
print response1.
print response1.text
# Let this call actually hit the foo.bar service
data = '<stuff>Test 2</stuff>'
response2 = _post(url, data)
print response2.
print response2.text
# Mock this call and simulate an error
data = '<stuff>Test 3</stuff>'
response3 = _post(url, data)
print response3.
print response3.text
# Let this call actually hit the foo.bar service
data = '<stuff>Test 4</stuff>'
response4 = _post(url, data)
print response4.
print response4.text
Changed in requests-mock: | |
milestone: | none → 1.3 |
importance: | Undecided → Medium |
So matching on body isn't that hard, it's just not always generalizable. So far it hasn't been a problem because you can match on headers or URLs or something else, but i can see in say XMLRPC you don't have this ability.
Obvious things would be just matching on a strict string or a regexp but to start with honestly the easiest thing will just be to allow passing the user a function.
In this case i certainly don't want to parse either json/xml/yaml/etc uploads so having the user provide a function that can differentiate their own requests seem easiest.