test_wildcard-cert-nohttps.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import pytest
  2. from ssl import CertificateError
  3. from requests import ConnectionError
  4. from requests.exceptions import SSLError
  5. @pytest.mark.parametrize("subdomain,should_redirect_to_https", [
  6. (1, True),
  7. (2, True),
  8. (3, False),
  9. ])
  10. def test_http_redirects_to_https(docker_compose, nginxproxy, subdomain, should_redirect_to_https):
  11. r = nginxproxy.get(f"http://{subdomain}.web.nginx-proxy.tld/port")
  12. if should_redirect_to_https:
  13. assert len(r.history) > 0
  14. assert r.history[0].is_redirect
  15. assert r.history[0].headers.get("Location") == f"https://{subdomain}.web.nginx-proxy.tld/port"
  16. assert f"answer from port 8{subdomain}\n" == r.text
  17. @pytest.mark.parametrize("subdomain", [1, 2])
  18. def test_https_get_served(docker_compose, nginxproxy, subdomain):
  19. r = nginxproxy.get(f"https://{subdomain}.web.nginx-proxy.tld/port", allow_redirects=False)
  20. assert r.status_code == 200
  21. assert f"answer from port 8{subdomain}\n" == r.text
  22. @pytest.mark.filterwarnings('ignore::urllib3.exceptions.InsecureRequestWarning')
  23. def test_https_request_to_nohttps_vhost_goes_to_fallback_server(docker_compose, nginxproxy):
  24. with pytest.raises( (CertificateError, SSLError) ) as excinfo:
  25. nginxproxy.get("https://3.web.nginx-proxy.tld/port")
  26. assert """certificate is not valid for '3.web.nginx-proxy.tld'""" in str(excinfo.value) or \
  27. """hostname '3.web.nginx-proxy.tld' doesn't match 'nginx-proxy.tld'""" in str(excinfo.value)
  28. r = nginxproxy.get("https://3.web.nginx-proxy.tld/port", verify=False)
  29. assert r.status_code == 503
  30. @pytest.mark.parametrize("subdomain,acme_should_work", [
  31. (1, True),
  32. (2, True),
  33. (3, False),
  34. ])
  35. def test_acme_challenge_works(
  36. docker_compose, nginxproxy, acme_challenge_path, subdomain, acme_should_work
  37. ):
  38. if acme_should_work:
  39. r = nginxproxy.get(
  40. f"https://{subdomain}.web.nginx-proxy.tld/{acme_challenge_path}",
  41. allow_redirects=False
  42. )
  43. assert r.status_code == 404
  44. else:
  45. with pytest.raises(ConnectionError):
  46. nginxproxy.get(
  47. f"https://{subdomain}.web.nginx-proxy.tld/{acme_challenge_path}",
  48. allow_redirects=False
  49. )