-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from sigmavirus24/bug/114
Handle differences in Requests 2.11.0
- Loading branch information
Showing
2 changed files
with
37 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |