diff --git a/tests/fixtures/wild/domain_redirect.yaml b/tests/fixtures/wild/domain_redirect.yaml index 2b8af68..465f071 100644 --- a/tests/fixtures/wild/domain_redirect.yaml +++ b/tests/fixtures/wild/domain_redirect.yaml @@ -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 | diff --git a/tests/integration/test_request.py b/tests/integration/test_request.py index 829c6eb..882783e 100644 --- a/tests/integration/test_request.py +++ b/tests/integration/test_request.py @@ -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 diff --git a/tests/unit/test_matchers.py b/tests/unit/test_matchers.py index 0075485..056fef7 100644 --- a/tests/unit/test_matchers.py +++ b/tests/unit/test_matchers.py @@ -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) diff --git a/tests/unit/test_request.py b/tests/unit/test_request.py index 304522a..adecee6 100644 --- a/tests/unit/test_request.py +++ b/tests/unit/test_request.py @@ -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) == '' + req = Request('GET', 'http://www.google.com:80/', '', {}) + str(req) == '' diff --git a/vcr/request.py b/vcr/request.py index bbe950b..a224225 100644 --- a/vcr/request.py +++ b/vcr/request.py @@ -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 "".format(self.method, self.url) + return "".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, } diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index 04c0f79..77391b2 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -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 {}) )