mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Replaced Request 'host, port, protocol, path' with 'uri'
This commit is contained in:
10
tests/fixtures/wild/domain_redirect.yaml
vendored
10
tests/fixtures/wild/domain_redirect.yaml
vendored
@@ -4,11 +4,8 @@
|
|||||||
- - !!python/tuple [Accept-Encoding, 'gzip, deflate, compress']
|
- - !!python/tuple [Accept-Encoding, 'gzip, deflate, compress']
|
||||||
- !!python/tuple [User-Agent, vcrpy-test]
|
- !!python/tuple [User-Agent, vcrpy-test]
|
||||||
- !!python/tuple [Accept, '*/*']
|
- !!python/tuple [Accept, '*/*']
|
||||||
host: seomoz.org
|
|
||||||
method: GET
|
method: GET
|
||||||
path: /
|
uri: http://seomoz.org:80/
|
||||||
port: 80
|
|
||||||
protocol: http
|
|
||||||
response:
|
response:
|
||||||
body: {string: ''}
|
body: {string: ''}
|
||||||
headers: ["Location: http://moz.com/\r\n", "Server: BigIP\r\n", "Connection: Keep-Alive\r\n",
|
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 [Accept-Encoding, 'gzip, deflate, compress']
|
||||||
- !!python/tuple [User-Agent, vcrpy-test]
|
- !!python/tuple [User-Agent, vcrpy-test]
|
||||||
- !!python/tuple [Accept, '*/*']
|
- !!python/tuple [Accept, '*/*']
|
||||||
host: moz.com
|
|
||||||
method: GET
|
method: GET
|
||||||
path: /
|
uri: http://moz.com:80/
|
||||||
port: 80
|
|
||||||
protocol: http
|
|
||||||
response:
|
response:
|
||||||
body:
|
body:
|
||||||
string: !!binary |
|
string: !!binary |
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ def test_recorded_request_url_with_redirected_request(tmpdir):
|
|||||||
with vcr.use_cassette(str(tmpdir.join('test.yml'))) as cass:
|
with vcr.use_cassette(str(tmpdir.join('test.yml'))) as cass:
|
||||||
assert len(cass) == 0
|
assert len(cass) == 0
|
||||||
urlopen('http://httpbin.org/redirect/3')
|
urlopen('http://httpbin.org/redirect/3')
|
||||||
assert cass.requests[0].url == 'http://httpbin.org/redirect/3'
|
assert cass.requests[0].url == 'http://httpbin.org:80/redirect/3'
|
||||||
assert cass.requests[3].url == 'http://httpbin.org/get'
|
assert cass.requests[3].url == 'http://httpbin.org:80/get'
|
||||||
assert len(cass) == 4
|
assert len(cass) == 4
|
||||||
|
|||||||
@@ -3,28 +3,28 @@ from vcr import request
|
|||||||
|
|
||||||
|
|
||||||
def test_method():
|
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)
|
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)
|
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)
|
assert False == matchers.method(req_get, req_post)
|
||||||
|
|
||||||
|
|
||||||
def test_url():
|
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)
|
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)
|
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)
|
assert True == matchers.url(req1, req1_post)
|
||||||
|
|
||||||
req_query_string = request.Request(
|
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(
|
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)
|
assert False == matchers.url(req_query_string, req_query_string1)
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
from vcr.request import Request
|
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():
|
def test_str():
|
||||||
req = Request('http', 'www.google.com', 80, 'GET', '/', '', {})
|
req = Request('GET', 'http://www.google.com:80/', '', {})
|
||||||
str(req) == '<Request (GET) http://www.google.com>'
|
str(req) == '<Request (GET) http://www.google.com:80/>'
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
class Request(object):
|
class Request(object):
|
||||||
|
|
||||||
def __init__(self, protocol, host, port, method, path, body, headers):
|
def __init__(self, method, uri, body, headers):
|
||||||
self.protocol = protocol
|
|
||||||
self.host = host
|
|
||||||
self.port = port
|
|
||||||
self.method = method
|
self.method = method
|
||||||
self.path = path
|
self.uri = uri
|
||||||
self.body = body
|
self.body = body
|
||||||
# make headers a frozenset so it will be hashable
|
# make headers a frozenset so it will be hashable
|
||||||
self.headers = frozenset(headers.items())
|
self.headers = frozenset(headers.items())
|
||||||
@@ -17,14 +14,12 @@ class Request(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def url(self):
|
def url(self):
|
||||||
return "{0}://{1}{2}".format(self.protocol, self.host, self.path)
|
return self.uri
|
||||||
|
|
||||||
def __key(self):
|
def __key(self):
|
||||||
return (
|
return (
|
||||||
self.host,
|
|
||||||
self.port,
|
|
||||||
self.method,
|
self.method,
|
||||||
self.path,
|
self.uri,
|
||||||
self.body,
|
self.body,
|
||||||
self.headers
|
self.headers
|
||||||
)
|
)
|
||||||
@@ -36,18 +31,15 @@ class Request(object):
|
|||||||
return hash(self) == hash(other)
|
return hash(self) == hash(other)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "<Request ({0}) {1}>".format(self.method, self.url)
|
return "<Request ({0}) {1}>".format(self.method, self.uri)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
def _to_dict(self):
|
def _to_dict(self):
|
||||||
return {
|
return {
|
||||||
'protocol': self.protocol,
|
|
||||||
'host': self.host,
|
|
||||||
'port': self.port,
|
|
||||||
'method': self.method,
|
'method': self.method,
|
||||||
'path': self.path,
|
'uri': self.uri,
|
||||||
'body': self.body,
|
'body': self.body,
|
||||||
'headers': self.headers,
|
'headers': self.headers,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,14 +119,29 @@ class VCRConnection:
|
|||||||
# A reference to the cassette that's currently being patched in
|
# A reference to the cassette that's currently being patched in
|
||||||
cassette = None
|
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):
|
def request(self, method, url, body=None, headers=None):
|
||||||
'''Persist the request metadata in self._vcr_request'''
|
'''Persist the request metadata in self._vcr_request'''
|
||||||
self._vcr_request = Request(
|
self._vcr_request = Request(
|
||||||
protocol=self._protocol,
|
|
||||||
host=self.real_connection.host,
|
|
||||||
port=self.real_connection.port,
|
|
||||||
method=method,
|
method=method,
|
||||||
path=url,
|
uri=self._uri(url),
|
||||||
body=body,
|
body=body,
|
||||||
headers=headers or {}
|
headers=headers or {}
|
||||||
)
|
)
|
||||||
@@ -144,11 +159,8 @@ class VCRConnection:
|
|||||||
of putheader() calls.
|
of putheader() calls.
|
||||||
"""
|
"""
|
||||||
self._vcr_request = Request(
|
self._vcr_request = Request(
|
||||||
protocol=self._protocol,
|
|
||||||
host=self.real_connection.host,
|
|
||||||
port=self.real_connection.port,
|
|
||||||
method=method,
|
method=method,
|
||||||
path=url,
|
uri=self._uri(url),
|
||||||
body="",
|
body="",
|
||||||
headers={}
|
headers={}
|
||||||
)
|
)
|
||||||
@@ -211,7 +223,7 @@ class VCRConnection:
|
|||||||
)
|
)
|
||||||
self.real_connection.request(
|
self.real_connection.request(
|
||||||
method=self._vcr_request.method,
|
method=self._vcr_request.method,
|
||||||
url=self._vcr_request.path,
|
url=self._url(self._vcr_request.uri),
|
||||||
body=self._vcr_request.body,
|
body=self._vcr_request.body,
|
||||||
headers=dict(self._vcr_request.headers or {})
|
headers=dict(self._vcr_request.headers or {})
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user