'''Test using a proxy.'''

# External imports
import multiprocessing
import pytest
requests = pytest.importorskip("requests")

from six.moves import socketserver, SimpleHTTPServer
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'))):
        requests.get(httpbin.url, proxies={'http': proxy_server})
        requests.get(httpbin.url, proxies={'http': proxy_server})

'''Test using a proxy.'''

# External imports
import multiprocessing
import pytest

from six.moves import socketserver, SimpleHTTPServer
from six.moves.urllib.request import urlopen

# Internal imports
import vcr

# Conditional imports
requests = pytest.importorskip("requests")


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():
    httpd = socketserver.ThreadingTCPServer(('', 0), Proxy)
    proxy_process = multiprocessing.Process(
        target=httpd.serve_forever,
    )
    proxy_process.start()
    yield 'http://{0}:{1}'.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'))):
        requests.get(httpbin.url, proxies={'http': proxy_server})
        requests.get(httpbin.url, proxies={'http': proxy_server})

Fix format string for Python 2.6
Fix format string for Python 2.6

  '''Test using a proxy.'''
  
  # External imports
  import multiprocessing
  import pytest
- requests = pytest.importorskip("requests")
  
  from six.moves import socketserver, SimpleHTTPServer
  from six.moves.urllib.request import urlopen
  
  # Internal imports
  import vcr
+ 
+ # Conditional imports
+ requests = pytest.importorskip("requests")
  
  
  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):
+ def proxy_server():
-     httpd = socketserver.ForkingTCPServer(('', 0), Proxy)
+     httpd = socketserver.ThreadingTCPServer(('', 0), Proxy)
      proxy_process = multiprocessing.Process(
          target=httpd.serve_forever,
      )
      proxy_process.start()
-     yield 'http://{}:{}'.format(*httpd.server_address)
+     yield 'http://{0}:{1}'.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'))):
          requests.get(httpbin.url, proxies={'http': proxy_server})
          requests.get(httpbin.url, proxies={'http': proxy_server})
  
Fix format string for Python 2.6
## Code Before:
'''Test using a proxy.'''

# External imports
import multiprocessing
import pytest
requests = pytest.importorskip("requests")

from six.moves import socketserver, SimpleHTTPServer
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'))):
        requests.get(httpbin.url, proxies={'http': proxy_server})
        requests.get(httpbin.url, proxies={'http': proxy_server})

## Instruction:
Fix format string for Python 2.6
## Code After:
'''Test using a proxy.'''

# External imports
import multiprocessing
import pytest

from six.moves import socketserver, SimpleHTTPServer
from six.moves.urllib.request import urlopen

# Internal imports
import vcr

# Conditional imports
requests = pytest.importorskip("requests")


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():
    httpd = socketserver.ThreadingTCPServer(('', 0), Proxy)
    proxy_process = multiprocessing.Process(
        target=httpd.serve_forever,
    )
    proxy_process.start()
    yield 'http://{0}:{1}'.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'))):
        requests.get(httpbin.url, proxies={'http': proxy_server})
        requests.get(httpbin.url, proxies={'http': proxy_server})
