1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 17:15:35 +00:00

Update unit tests for body matcher. Simplified logic.

This commit is contained in:
Ivan Malison
2015-07-14 20:50:09 -07:00
parent a046697567
commit 33a4fb98c6
2 changed files with 130 additions and 39 deletions

View File

@@ -1,5 +1,7 @@
import itertools import itertools
import pytest
from vcr import matchers from vcr import matchers
from vcr import request from vcr import request
@@ -35,36 +37,105 @@ def test_uri_matcher():
assert matched assert matched
def test_body_matcher(): req1_body = (b"<?xml version='1.0'?><methodCall><methodName>test</methodName>"
# raw b"<params><param><value><array><data><value><struct>"
req1 = request.Request('POST', 'http://host.com/', '123', {}) b"<member><name>a</name><value><string>1</string></value></member>"
req2 = request.Request('POST', 'http://another-host.com/', '123', {'Some-Header': 'value'}) b"<member><name>b</name><value><string>2</string></value></member>"
assert matchers.body(req1, req2) b"</struct></value></data></array></value></param></params></methodCall>")
req2_body = (b"<?xml version='1.0'?><methodCall><methodName>test</methodName>"
b"<params><param><value><array><data><value><struct>"
b"<member><name>b</name><value><string>2</string></value></member>"
b"<member><name>a</name><value><string>1</string></value></member>"
b"</struct></value></data></array></value></param></params></methodCall>")
# application/x-www-form-urlencoded
req1 = request.Request('POST', 'http://host.com/', 'a=1&b=2', {'Content-Type': 'application/x-www-form-urlencoded'})
req2 = request.Request('POST', 'http://host.com/', 'b=2&a=1', {'Content-Type': 'application/x-www-form-urlencoded'})
assert matchers.body(req1, req2)
# application/json @pytest.mark.parametrize("r1, r2", [
req1 = request.Request('POST', 'http://host.com/', '{"a": 1, "b": 2}', {'Content-Type': 'application/json'}) (
req2 = request.Request('POST', 'http://host.com/', '{"b": 2, "a": 1}', {'content-type': 'application/json'}) request.Request('POST', 'http://host.com/', '123', {}),
assert matchers.body(req1, req2) request.Request('POST', 'http://another-host.com/',
'123', {'Some-Header': 'value'})
),
(
request.Request('POST', 'http://host.com/', 'a=1&b=2',
{'Content-Type': 'application/x-www-form-urlencoded'}),
request.Request('POST', 'http://host.com/', 'b=2&a=1',
{'Content-Type': 'application/x-www-form-urlencoded'})
),
(
request.Request('POST', 'http://host.com/', '123', {}),
request.Request('POST', 'http://another-host.com/', '123', {'Some-Header': 'value'})
),
(
request.Request(
'POST', 'http://host.com/', 'a=1&b=2',
{'Content-Type': 'application/x-www-form-urlencoded'}
),
request.Request(
'POST', 'http://host.com/', 'b=2&a=1',
{'Content-Type': 'application/x-www-form-urlencoded'}
)
),
(
request.Request(
'POST', 'http://host.com/', '{"a": 1, "b": 2}',
{'Content-Type': 'application/json'}
),
request.Request(
'POST', 'http://host.com/', '{"b": 2, "a": 1}',
{'content-type': 'application/json'}
)
),
(
request.Request(
'POST', 'http://host.com/', req1_body,
{'User-Agent': 'xmlrpclib', 'Content-Type': 'text/xml'}
),
request.Request(
'POST', 'http://host.com/', req2_body,
{'user-agent': 'somexmlrpc', 'content-type': 'text/xml'}
)
),
(
request.Request(
'POST', 'http://host.com/',
'{"a": 1, "b": 2}', {'Content-Type': 'application/json'}
),
request.Request(
'POST', 'http://host.com/',
'{"b": 2, "a": 1}', {'content-type': 'application/json'}
)
)
])
def test_body_matcher_does_match(r1, r2):
assert matchers.body(r1, r2)
# xmlrpc
req1_body = (b"<?xml version='1.0'?><methodCall><methodName>test</methodName>" @pytest.mark.parametrize("r1, r2", [
b"<params><param><value><array><data><value><struct>" (
b"<member><name>a</name><value><string>1</string></value></member>" request.Request('POST', 'http://host.com/', '{"a": 1, "b": 2}', {}),
b"<member><name>b</name><value><string>2</string></value></member>" request.Request('POST', 'http://host.com/', '{"b": 2, "a": 1}', {}),
b"</struct></value></data></array></value></param></params></methodCall>") ),
req2_body = (b"<?xml version='1.0'?><methodCall><methodName>test</methodName>" (
b"<params><param><value><array><data><value><struct>" request.Request(
b"<member><name>b</name><value><string>2</string></value></member>" 'POST', 'http://host.com/',
b"<member><name>a</name><value><string>1</string></value></member>" '{"a": 1, "b": 3}', {'Content-Type': 'application/json'}
b"</struct></value></data></array></value></param></params></methodCall>") ),
req1 = request.Request('POST', 'http://host.com/', req1_body, {'User-Agent': 'xmlrpclib', 'Content-Type': 'text/xml'}) request.Request(
req2 = request.Request('POST', 'http://host.com/', req2_body, {'user-agent': 'somexmlrpc', 'content-type': 'text/xml'}) 'POST', 'http://host.com/',
assert matchers.body(req1, req2) '{"b": 2, "a": 1}', {'content-type': 'application/json'}
)
),
(
request.Request(
'POST', 'http://host.com/', req1_body, {'Content-Type': 'text/xml'}
),
request.Request(
'POST', 'http://host.com/', req2_body, {'content-type': 'text/xml'}
)
)
])
def test_body_match_does_not_match(r1, r2):
assert not matchers.body(r1, r2)
def test_query_matcher(): def test_query_matcher():

View File

@@ -37,19 +37,39 @@ def raw_body(r1, r2):
return read_body(r1) == read_body(r2) return read_body(r1) == read_body(r2)
def _header_checker(value, header='Content-Type'):
def checker(headers):
return value in headers.get(header, '').lower()
return checker
_xml_header_checker = _header_checker('text/xml')
_xmlrpc_header_checker = _header_checker('xmlrpc', header='User-Agent')
_checker_transformer_pairs = (
(_header_checker('application/x-www-form-urlencoded'), urllib.parse.parse_qs),
(_header_checker('application/json'), json.loads),
(lambda request: _xml_header_checker(request) and _xmlrpc_header_checker(request), xmlrpc_client.loads),
)
def _identity(x):
return x
def _get_transformer(request):
headers = CaseInsensitiveDict(request.headers)
for checker, transformer in _checker_transformer_pairs:
if checker(headers): return transformer
else:
return _identity
def body(r1, r2): def body(r1, r2):
r1_body = read_body(r1) transformer = _get_transformer(r1)
r2_body = read_body(r2) r2_transformer = _get_transformer(r2)
r1_headers = CaseInsensitiveDict(r1.headers) if transformer != r2_transformer:
r2_headers = CaseInsensitiveDict(r2.headers) transformer = _identity
if r1_headers.get('Content-Type') == r2_headers.get('Content-Type') == 'application/x-www-form-urlencoded': return transformer(read_body(r1)) == transformer(read_body(r2))
return urllib.parse.parse_qs(r1_body) == urllib.parse.parse_qs(r2_body)
if r1_headers.get('Content-Type') == r2_headers.get('Content-Type') == 'application/json':
return json.loads(r1_body) == json.loads(r2_body)
if ('xmlrpc' in r1_headers.get('User-Agent', '') and 'xmlrpc' in r2_headers.get('User-Agent', '') and
r1_headers.get('Content-Type') == r2_headers.get('Content-Type') == 'text/xml'):
return xmlrpc_client.loads(r1_body) == xmlrpc_client.loads(r2_body)
return r1_body == r2_body
def headers(r1, r2): def headers(r1, r2):