]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Add test for proxies
authorSamuel Fekete <samuel.fekete@hogarthww.com>
Mon, 3 Jul 2017 11:25:01 +0000 (12:25 +0100)
committerSamuel Fekete <samuel.fekete@gmail.com>
Fri, 13 Jul 2018 15:34:21 +0000 (16:34 +0100)
tests/integration/test_proxy.py [new file with mode: 0644]

diff --git a/tests/integration/test_proxy.py b/tests/integration/test_proxy.py
new file mode 100644 (file)
index 0000000..3212b24
--- /dev/null
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+'''Test using a proxy.'''
+
+# External imports
+import multiprocessing
+import SocketServer
+import SimpleHTTPServer
+import pytest
+requests = pytest.importorskip("requests")
+
+from six.moves.urllib.request import urlopen
+
+# Internal imports
+import vcr
+
+
+class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
+    '''
+    Simple proxy server.
+
+    (from: http://effbot.org/librarybook/simplehttpserver.htm).
+    '''
+    def do_GET(self):
+        self.copyfile(urlopen(self.path), self.wfile)
+
+
+@pytest.yield_fixture(scope='session')
+def proxy_server(httpbin):
+    httpd = SocketServer.ForkingTCPServer(('', 0), Proxy)
+    proxy_process = multiprocessing.Process(
+        target=httpd.serve_forever,
+    )
+    proxy_process.start()
+    yield 'http://{}:{}'.format(*httpd.server_address)
+    proxy_process.terminate()
+
+
+def test_use_proxy(tmpdir, httpbin, proxy_server):
+    '''Ensure that it works with a proxy.'''
+    with vcr.use_cassette(str(tmpdir.join('proxy.yaml'))) as cass:
+        requests.get(httpbin.url, proxies={'http': proxy_server})
+        requests.get(httpbin.url, proxies={'http': proxy_server})