mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 01:25:34 +00:00
Replaced Request 'host, port, protocol, path' with 'uri'
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
class Request(object):
|
||||
|
||||
def __init__(self, protocol, host, port, method, path, body, headers):
|
||||
self.protocol = protocol
|
||||
self.host = host
|
||||
self.port = port
|
||||
def __init__(self, method, uri, body, headers):
|
||||
self.method = method
|
||||
self.path = path
|
||||
self.uri = uri
|
||||
self.body = body
|
||||
# make headers a frozenset so it will be hashable
|
||||
self.headers = frozenset(headers.items())
|
||||
@@ -17,14 +14,12 @@ class Request(object):
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return "{0}://{1}{2}".format(self.protocol, self.host, self.path)
|
||||
return self.uri
|
||||
|
||||
def __key(self):
|
||||
return (
|
||||
self.host,
|
||||
self.port,
|
||||
self.method,
|
||||
self.path,
|
||||
self.uri,
|
||||
self.body,
|
||||
self.headers
|
||||
)
|
||||
@@ -36,18 +31,15 @@ class Request(object):
|
||||
return hash(self) == hash(other)
|
||||
|
||||
def __str__(self):
|
||||
return "<Request ({0}) {1}>".format(self.method, self.url)
|
||||
return "<Request ({0}) {1}>".format(self.method, self.uri)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
def _to_dict(self):
|
||||
return {
|
||||
'protocol': self.protocol,
|
||||
'host': self.host,
|
||||
'port': self.port,
|
||||
'method': self.method,
|
||||
'path': self.path,
|
||||
'uri': self.uri,
|
||||
'body': self.body,
|
||||
'headers': self.headers,
|
||||
}
|
||||
|
||||
@@ -119,14 +119,29 @@ class VCRConnection:
|
||||
# A reference to the cassette that's currently being patched in
|
||||
cassette = None
|
||||
|
||||
def _uri(self, url):
|
||||
"""Returns request absolute URI"""
|
||||
return "{0}://{1}:{2}{3}".format(
|
||||
self._protocol,
|
||||
self.real_connection.host,
|
||||
self.real_connection.port,
|
||||
url,
|
||||
)
|
||||
|
||||
def _url(self, uri):
|
||||
"""Returns request selector url from absolute URI"""
|
||||
prefix = "{0}://{1}:{2}".format(
|
||||
self._protocol,
|
||||
self.real_connection.host,
|
||||
self.real_connection.port,
|
||||
)
|
||||
return uri.replace(prefix, '', 1)
|
||||
|
||||
def request(self, method, url, body=None, headers=None):
|
||||
'''Persist the request metadata in self._vcr_request'''
|
||||
self._vcr_request = Request(
|
||||
protocol=self._protocol,
|
||||
host=self.real_connection.host,
|
||||
port=self.real_connection.port,
|
||||
method=method,
|
||||
path=url,
|
||||
uri=self._uri(url),
|
||||
body=body,
|
||||
headers=headers or {}
|
||||
)
|
||||
@@ -144,11 +159,8 @@ class VCRConnection:
|
||||
of putheader() calls.
|
||||
"""
|
||||
self._vcr_request = Request(
|
||||
protocol=self._protocol,
|
||||
host=self.real_connection.host,
|
||||
port=self.real_connection.port,
|
||||
method=method,
|
||||
path=url,
|
||||
uri=self._uri(url),
|
||||
body="",
|
||||
headers={}
|
||||
)
|
||||
@@ -211,7 +223,7 @@ class VCRConnection:
|
||||
)
|
||||
self.real_connection.request(
|
||||
method=self._vcr_request.method,
|
||||
url=self._vcr_request.path,
|
||||
url=self._url(self._vcr_request.uri),
|
||||
body=self._vcr_request.body,
|
||||
headers=dict(self._vcr_request.headers or {})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user