1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

Replaced Request 'host, port, protocol, path' with 'uri'

This commit is contained in:
Max Shytikov
2014-04-06 23:49:54 +02:00
parent e0c6a8429d
commit edf1df9188
6 changed files with 41 additions and 48 deletions

View File

@@ -4,11 +4,8 @@
- - !!python/tuple [Accept-Encoding, 'gzip, deflate, compress']
- !!python/tuple [User-Agent, vcrpy-test]
- !!python/tuple [Accept, '*/*']
host: seomoz.org
method: GET
path: /
port: 80
protocol: http
uri: http://seomoz.org:80/
response:
body: {string: ''}
headers: ["Location: http://moz.com/\r\n", "Server: BigIP\r\n", "Connection: Keep-Alive\r\n",
@@ -20,11 +17,8 @@
- - !!python/tuple [Accept-Encoding, 'gzip, deflate, compress']
- !!python/tuple [User-Agent, vcrpy-test]
- !!python/tuple [Accept, '*/*']
host: moz.com
method: GET
path: /
port: 80
protocol: http
uri: http://moz.com:80/
response:
body:
string: !!binary |

View File

@@ -6,6 +6,6 @@ def test_recorded_request_url_with_redirected_request(tmpdir):
with vcr.use_cassette(str(tmpdir.join('test.yml'))) as cass:
assert len(cass) == 0
urlopen('http://httpbin.org/redirect/3')
assert cass.requests[0].url == 'http://httpbin.org/redirect/3'
assert cass.requests[3].url == 'http://httpbin.org/get'
assert cass.requests[0].url == 'http://httpbin.org:80/redirect/3'
assert cass.requests[3].url == 'http://httpbin.org:80/get'
assert len(cass) == 4

View File

@@ -3,28 +3,28 @@ from vcr import request
def test_method():
req_get = request.Request('http', 'google.com', 80, 'GET', '/', '', {})
req_get = request.Request('GET', 'http://google.com:80/', '', {})
assert True == matchers.method(req_get, req_get)
req_get1 = request.Request('https', 'httpbin.org', 80, 'GET', '/', '', {})
req_get1 = request.Request('GET', 'https://httpbin.org:80/', '', {})
assert True == matchers.method(req_get, req_get1)
req_post = request.Request('http', 'google.com', 80, 'POST', '/', '', {})
req_post = request.Request('POST', 'http://google.com:80/', '', {})
assert False == matchers.method(req_get, req_post)
def test_url():
req1 = request.Request('http', 'google.com', 80, 'GET', '/', '', {})
req1 = request.Request('GET', 'http://google.com:80/', '', {})
assert True == matchers.url(req1, req1)
req2 = request.Request('http', 'httpbin.org', 80, 'GET', '/', '', {})
req2 = request.Request('GET', 'https://httpbin.org:80/', '', {})
assert False == matchers.url(req1, req2)
req1_post = request.Request('http', 'google.com', 80, 'POST', '/', '', {})
req1_post = request.Request('POST', 'http://google.com:80/', '', {})
assert True == matchers.url(req1, req1_post)
req_query_string = request.Request(
'http', 'google.com?p1=t1&p2=t2', 80, 'GET', '/', '', {})
'GET', 'http://google.com:80/?p1=t1&p2=t2', '', {})
req_query_string1 = request.Request(
'http', 'google.com?p2=t2&p1=t1', 80, 'GET', '/', '', {})
'GET', 'http://google.com:80/?p2=t2&p1=t1', '', {})
assert False == matchers.url(req_query_string, req_query_string1)

View File

@@ -1,11 +1,6 @@
from vcr.request import Request
def test_url():
req = Request('http', 'www.google.com', 80, 'GET', '/', '', {})
assert req.url == 'http://www.google.com/'
def test_str():
req = Request('http', 'www.google.com', 80, 'GET', '/', '', {})
str(req) == '<Request (GET) http://www.google.com>'
req = Request('GET', 'http://www.google.com:80/', '', {})
str(req) == '<Request (GET) http://www.google.com:80/>'

View File

@@ -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,
}

View File

@@ -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 {})
)