Skip to content

Commit

Permalink
Merge branch 'pr/71'
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmavirus24 committed Jul 12, 2015
2 parents f743c04 + 06783ca commit d1418ce
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Contributors
------------

- Marc Abramowitz (@msabramo)
- Bryce Boe <bbzbryce@gmail.com> (@bboe)
4 changes: 4 additions & 0 deletions betamax/cassette/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import base64
import io
import sys


def coerce_content(content, encoding=None):
Expand Down Expand Up @@ -52,6 +53,9 @@ def add_body(r, preserve_exact_body_bytes, body_dict):

if (preserve_exact_body_bytes or
'gzip' in r.headers.get('Content-Encoding', '')):
if sys.version_info >= (3, 0) and hasattr(body, 'encode'):
body = body.encode(body_dict['encoding'] or 'utf-8')

body_dict['base64_string'] = base64.b64encode(body).decode()
else:
body_dict['string'] = coerce_content(body, body_dict['encoding'])
Expand Down
11 changes: 10 additions & 1 deletion betamax/matchers/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ class BodyMatcher(BaseMatcher):

def match(self, request, recorded_request):
recorded_request = deserialize_prepared_request(recorded_request)
return recorded_request.body == (request.body or 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''

return recorded_request.body == request_body
2 changes: 1 addition & 1 deletion tests/cassettes/preserve_exact_bytes.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"http_interactions": [{"request": {"body": {"base64_string": "", "encoding": "utf-8"}, "headers": {"Accept-Encoding": ["gzip, deflate, compress"], "Accept": ["*/*"], "User-Agent": ["python-requests/2.0.0 CPython/2.7.5 Darwin/13.1.0"]}, "method": "GET", "uri": "https://httpbin.org/get"}, "response": {"body": {"base64_string": "ewogICJhcmdzIjoge30sCiAgImhlYWRlcnMiOiB7CiAgICAiVXNlci1BZ2VudCI6ICJweXRob24tcmVxdWVzdHMvMi4wLjAgQ1B5dGhvbi8yLjcuNSBEYXJ3aW4vMTMuMS4wIiwKICAgICJBY2NlcHQiOiAiKi8qIiwKICAgICJYLVJlcXVlc3QtSWQiOiAiYWY0ZTNhZjUtYzdkNS00NGI4LTgyZjUtZTAwODAwYjVmYjY4IiwKICAgICJBY2NlcHQtRW5jb2RpbmciOiAiZ3ppcCwgZGVmbGF0ZSwgY29tcHJlc3MiLAogICAgIkhvc3QiOiAiaHR0cGJpbi5vcmciLAogICAgIkNvbm5lY3Rpb24iOiAiY2xvc2UiCiAgfSwKICAidXJsIjogImh0dHA6Ly9odHRwYmluLm9yZy9nZXQiLAogICJvcmlnaW4iOiAiNjYuMTcxLjE3My4yNTAiCn0=", "encoding": null}, "headers": {"content-length": ["356"], "server": ["gunicorn/0.17.4"], "connection": ["keep-alive"], "date": ["Fri, 11 Apr 2014 19:32:57 GMT"], "access-control-allow-origin": ["*"], "content-type": ["application/json"]}, "status": {"message": "OK", "code": 200}, "url": "https://httpbin.org/get"}, "recorded_at": "2014-04-11T19:32:57"}], "recorded_with": "betamax/{version}"}
{"recorded_with": "betamax/0.4.2", "http_interactions": [{"recorded_at": "2015-06-27T15:39:25", "request": {"method": "POST", "uri": "https://httpbin.org/post", "headers": {"Content-Length": ["3"], "User-Agent": ["python-requests/2.7.0 CPython/3.4.3 Darwin/14.3.0"], "Connection": ["keep-alive"], "Accept-Encoding": ["gzip, deflate"], "Content-Type": ["application/x-www-form-urlencoded"], "Accept": ["*/*"]}, "body": {"base64_string": "YT0x", "encoding": "utf-8"}}, "response": {"url": "https://httpbin.org/post", "headers": {"access-control-allow-origin": ["*"], "content-type": ["application/json"], "server": ["nginx"], "date": ["Sat, 27 Jun 2015 15:39:25 GMT"], "content-length": ["430"], "access-control-allow-credentials": ["true"], "connection": ["keep-alive"]}, "status": {"code": 200, "message": "OK"}, "body": {"base64_string": "ewogICJhcmdzIjoge30sIAogICJkYXRhIjogIiIsIAogICJmaWxlcyI6IHt9LCAKICAiZm9ybSI6IHsKICAgICJhIjogIjEiCiAgfSwgCiAgImhlYWRlcnMiOiB7CiAgICAiQWNjZXB0IjogIiovKiIsIAogICAgIkFjY2VwdC1FbmNvZGluZyI6ICJnemlwLCBkZWZsYXRlIiwgCiAgICAiQ29udGVudC1MZW5ndGgiOiAiMyIsIAogICAgIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi94LXd3dy1mb3JtLXVybGVuY29kZWQiLCAKICAgICJIb3N0IjogImh0dHBiaW4ub3JnIiwgCiAgICAiVXNlci1BZ2VudCI6ICJweXRob24tcmVxdWVzdHMvMi43LjAgQ1B5dGhvbi8zLjQuMyBEYXJ3aW4vMTQuMy4wIgogIH0sIAogICJqc29uIjogbnVsbCwgCiAgIm9yaWdpbiI6ICI2OC42LjkxLjIzOSIsIAogICJ1cmwiOiAiaHR0cHM6Ly9odHRwYmluLm9yZy9wb3N0Igp9Cg==", "encoding": null}}}]}
7 changes: 5 additions & 2 deletions tests/integration/test_preserve_exact_body_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ def test_preserve_exact_body_bytes_does_not_munge_response_content(self):

with Betamax(self.session) as b:
b.use_cassette('preserve_exact_bytes',
preserve_exact_body_bytes=True)
r = self.session.get('https://httpbin.org/get')
preserve_exact_body_bytes=True,
match_requests_on=['uri', 'method', 'body'])
r = self.session.post('https://httpbin.org/post',
data={'a': 1})
assert 'headers' in r.json()

interaction = b.current_cassette.interactions[0].json
assert 'base64_string' in interaction['request']['body']
assert 'base64_string' in interaction['response']['body']


Expand Down

0 comments on commit d1418ce

Please sign in to comment.