test_https.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import pytest
  2. import requests
  3. from pytest_localserver import https
  4. from pytest_localserver import plugin
  5. # define test fixture here again in order to run tests without having to
  6. # install the plugin anew every single time
  7. httpsserver = plugin.httpsserver
  8. def test_httpsserver_funcarg(httpsserver):
  9. assert isinstance(httpsserver, https.SecureContentServer)
  10. assert httpsserver.is_alive()
  11. assert httpsserver.server_address
  12. def test_server_does_not_serve_file_at_startup(httpsserver):
  13. assert httpsserver.code == 204
  14. assert httpsserver.content == ""
  15. def test_some_content_retrieval(httpsserver):
  16. httpsserver.serve_content("TEST!")
  17. resp = requests.get(httpsserver.url, verify=False)
  18. assert resp.text == "TEST!"
  19. assert resp.status_code == 200
  20. def test_GET_request(httpsserver):
  21. httpsserver.serve_content("TEST!", headers={"Content-type": "text/plain"})
  22. resp = requests.get(httpsserver.url, headers={"User-Agent": "Test method"}, verify=False)
  23. assert resp.text == "TEST!"
  24. assert resp.status_code == 200
  25. assert "text/plain" in resp.headers["Content-type"]
  26. def test_HEAD_request(httpsserver):
  27. httpsserver.serve_content("TEST!", headers={"Content-type": "text/plain"})
  28. print(httpsserver.url)
  29. resp = requests.head(httpsserver.url, verify=False)
  30. assert resp.status_code == 200
  31. assert resp.headers["Content-type"] == "text/plain"
  32. def test_client_does_not_trust_self_signed_certificate(httpsserver):
  33. httpsserver.serve_content("TEST!", headers={"Content-type": "text/plain"})
  34. with pytest.raises(requests.exceptions.SSLError, match="CERTIFICATE_VERIFY_FAILED"):
  35. requests.get(httpsserver.url, verify=True)
  36. def test_add_server_certificate_to_client_trust_chain(httpsserver):
  37. httpsserver.serve_content("TEST!", headers={"Content-type": "text/plain"})
  38. resp = requests.get(httpsserver.url, verify=httpsserver.certificate)
  39. assert resp.status_code == 200
  40. assert resp.headers["Content-type"] == "text/plain"