]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Replaced Request 'host, port, protocol, path' with 'uri'
authorMax Shytikov <mshytikov@gmail.com>
Sun, 6 Apr 2014 21:49:54 +0000 (23:49 +0200)
committerMax Shytikov <mshytikov@gmail.com>
Wed, 30 Apr 2014 00:27:52 +0000 (02:27 +0200)
tests/fixtures/wild/domain_redirect.yaml
tests/integration/test_request.py
tests/unit/test_matchers.py
tests/unit/test_request.py
vcr/request.py
vcr/stubs/__init__.py

index 2b8af68651a8bb4e4ed4f2349599a446957f3d02..465f07186788fa4b98fee5809c7ccb74987cfbc2 100644 (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",
     - - !!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 |
index 829c6eb86275629ec919106bf95ffca23f77a74e..882783e41b6a536db09203e7ce44a2d1f59ffac6 100644 (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
index 0075485a2b6e90fb9430f658e33094496e5dd496..056fef7082285462d97a394a26bc23380d158adc 100644 (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)
index 304522a780bbb8d30efc160d44959f96b9755087..adecee6f34d94b2098985ab9d66a9cacfc73cb50 100644 (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/>'
index bbe950b3edd673d60637aff3f6c8f797cf48f426..a224225758bb7014bff7dae8103cd09c1c485f21 100644 (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,
         }
index 04c0f7982f6a3c50d11f0bf64d24b45de0652e53..77391b25f9cdc3dd2969fa2490d59c9cc7930c1b 100644 (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 {})
             )