conftest.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. try:
  3. from http.server import HTTPServer
  4. from http.server import SimpleHTTPRequestHandler
  5. except ImportError:
  6. from BaseHTTPServer import HTTPServer
  7. from SimpleHTTPServer import SimpleHTTPRequestHandler
  8. import ssl
  9. import tempfile
  10. import threading
  11. import pytest
  12. from requests.compat import urljoin
  13. def prepare_url(value):
  14. # Issue #1483: Make sure the URL always has a trailing slash
  15. httpbin_url = value.url.rstrip('/') + '/'
  16. def inner(*suffix):
  17. return urljoin(httpbin_url, '/'.join(suffix))
  18. return inner
  19. @pytest.fixture
  20. def httpbin(httpbin):
  21. return prepare_url(httpbin)
  22. @pytest.fixture
  23. def httpbin_secure(httpbin_secure):
  24. return prepare_url(httpbin_secure)
  25. @pytest.fixture
  26. def nosan_server(tmp_path_factory):
  27. # delay importing until the fixture in order to make it possible
  28. # to deselect the test via command-line when trustme is not available
  29. import trustme
  30. tmpdir = tmp_path_factory.mktemp("certs")
  31. ca = trustme.CA()
  32. # only commonName, no subjectAltName
  33. server_cert = ca.issue_cert(common_name=u"localhost")
  34. ca_bundle = str(tmpdir / "ca.pem")
  35. ca.cert_pem.write_to_path(ca_bundle)
  36. context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
  37. server_cert.configure_cert(context)
  38. server = HTTPServer(("localhost", 0), SimpleHTTPRequestHandler)
  39. server.socket = context.wrap_socket(server.socket, server_side=True)
  40. server_thread = threading.Thread(target=server.serve_forever)
  41. server_thread.start()
  42. yield "localhost", server.server_address[1], ca_bundle
  43. server.shutdown()
  44. server_thread.join()