1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
import ssl
import sys
import unittest
import urllib.request
import pytest
import requests.exceptions
import urllib3
import pytest_httpbin.certs
def test_httpbin_gets_injected(httpbin):
assert httpbin.url
def test_httpbin_accepts_get_requests(httpbin):
assert requests.get(httpbin.url + "/get").status_code == 200
def test_httpbin_secure_accepts_get_requests(httpbin_secure):
assert requests.get(httpbin_secure.url + "/get").status_code == 200
def test_httpbin_secure_accepts_lots_of_get_requests(httpbin_secure):
for i in range(10):
assert requests.get(httpbin_secure.url + "/get").status_code == 200
def test_httpbin_accepts_lots_of_get_requests_in_single_session(httpbin):
session = requests.Session()
for i in range(10):
assert session.get(httpbin.url + "/get").status_code == 200
def test_httpbin_both(httpbin_both):
# this test will get called twice, once with an http url, once with an
# https url
assert requests.get(httpbin_both.url + "/get").status_code == 200
def test_httpbin_join(httpbin):
assert httpbin.join("foo") == httpbin.url + "/foo"
def test_httpbin_str(httpbin):
assert httpbin + "/foo" == httpbin.url + "/foo"
def test_chunked_encoding(httpbin):
assert requests.get(httpbin.url + "/stream/20").status_code == 200
@pytest.mark.xfail(
condition=sys.version_info < (3, 8) and ssl.OPENSSL_VERSION_INFO >= (3, 0, 0),
reason="fails on python3.7 openssl 3+",
raises=requests.exceptions.SSLError,
)
def test_chunked_encoding_secure(httpbin_secure):
assert requests.get(httpbin_secure.url + "/stream/20").status_code == 200
@pytest_httpbin.use_class_based_httpbin
@pytest_httpbin.use_class_based_httpbin_secure
class TestClassBassedTests(unittest.TestCase):
def test_http(self):
assert requests.get(self.httpbin.url + "/get").status_code == 200
def test_http_secure(self):
assert requests.get(self.httpbin_secure.url + "/get").status_code == 200
def test_with_urllib2(httpbin_secure):
url = httpbin_secure.url + "/get"
context = ssl.create_default_context(cafile=pytest_httpbin.certs.where())
with urllib.request.urlopen(url, context=context) as response:
assert response.getcode() == 200
def test_with_urllib3(httpbin_secure):
with urllib3.PoolManager(
cert_reqs="CERT_REQUIRED",
ca_certs=pytest_httpbin.certs.where(),
) as pool:
pool.request(
"POST", httpbin_secure.url + "/post", {"key1": "value1", "key2": "value2"}
)
|