Skip to content

Commit

Permalink
Merge pull request #116 from sigmavirus24/bug/114
Browse files Browse the repository at this point in the history
Handle differences in Requests 2.11.0
  • Loading branch information
sigmavirus24 committed Aug 10, 2016
2 parents 492116a + 8a2aa83 commit 5172166
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
19 changes: 10 additions & 9 deletions betamax/matchers/body.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# -*- coding: utf-8 -*-
from .base import BaseMatcher
from betamax.util import deserialize_prepared_request

from betamax import util


class BodyMatcher(BaseMatcher):
# Matches based on the body of the request
name = 'body'

def match(self, request, recorded_request):
recorded_request = deserialize_prepared_request(recorded_request)
recorded_request = util.deserialize_prepared_request(recorded_request)

request_body = b''
if request.body:
if isinstance(recorded_request.body, type(request.body)):
request_body = request.body
else:
request_body = request.body.encode('utf-8')
else:
request_body = b''
request_body = util.coerce_content(request.body)

recorded_body = b''
if recorded_request.body:
recorded_body = util.coerce_content(recorded_request.body)

return recorded_request.body == request_body
return recorded_body == request_body
27 changes: 27 additions & 0 deletions tests/regression/test_requests_2_11_body_matcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import unittest

import pytest
import requests

from betamax import Betamax


class TestRequests211BodyMatcher(unittest.TestCase):
def tearDown(self):
os.unlink('tests/cassettes/requests_2_11_body_matcher.json')

@pytest.mark.skipif(requests.__build__ < 0x020401,
reason="No json keyword.")
def test_requests_with_json_body(self):
s = requests.Session()
with Betamax(s).use_cassette('requests_2_11_body_matcher',
match_requests_on=['body']):
r = s.post('https://httpbin.org/post', json={'a': 2})
assert r.json() is not None

s = requests.Session()
with Betamax(s).use_cassette('requests_2_11_body_matcher',
match_requests_on=['body']):
r = s.post('https://httpbin.org/post', json={'a': 2})
assert r.json() is not None

0 comments on commit 5172166

Please sign in to comment.