Allow matching on request body

Bug #1657308 reported by Jamie Lennox
6
This bug affects 1 person
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://foo.bar'

# Let this call actually hit the foo.bar service
data = '<stuff>Test 1</stuff>'
response1 = _post(url, data)
print response1.status_code
print response1.text

# Let this call actually hit the foo.bar service
data = '<stuff>Test 2</stuff>'
response2 = _post(url, data)
print response2.status_code
print response2.text

# Mock this call and simulate an error
data = '<stuff>Test 3</stuff>'
response3 = _post(url, data)
print response3.status_code
print response3.text

# Let this call actually hit the foo.bar service
data = '<stuff>Test 4</stuff>'
response4 = _post(url, data)
print response4.status_code

print response4.text

Revision history for this message
Jamie Lennox (jamielennox) wrote :

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.

Revision history for this message
Jamie Lennox (jamielennox) wrote :

So as of today the way to do this would be:

def test_something(self):

    def match_body(request):
        if 'hello' in request.text:
            return requests_mock.create_response(request, status_code=200, text="My Response")

    self.requests_mock.add_matcher(match_body)

    requests.post('http://example.com', data='hello world')

however that means you have to do all your url, method etc matching as part of that body because currently that matcher will actually match everything with that body now.

We would like to match body text as a part of the rest of the system, not a complete override

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to requests-mock (master)

Fix proposed to branch: master
Review: https://review.openstack.org/421671

Changed in requests-mock:
assignee: nobody → Jamie Lennox (jamielennox)
status: New → In Progress
Changed in requests-mock:
milestone: none → 1.3
importance: Undecided → Medium
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to requests-mock (master)

Reviewed: https://review.openstack.org/421671
Committed: https://git.openstack.org/cgit/openstack/requests-mock/commit/?id=10c84747e09295eecdc0513eed9392283174d126
Submitter: Jenkins
Branch: master

commit 10c84747e09295eecdc0513eed9392283174d126
Author: Jamie Lennox <email address hidden>
Date: Wed Jan 18 16:31:24 2017 +1100

    Allow arbirtrary matcher to be add to match

    To handle requests for additional features on matching add a callback
    function that can match anything on the request object. This is easier
    than adding every combination of matcher option - though we should still
    add things that are commonly used.

    Change-Id: I300f74a1f2103545eca60087b2352a535add188d
    Closes-Bug: #1657308

Changed in requests-mock:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.