Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

194 lines · 5.5 KiB · Python

Last changed by

Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7

History of this file

Take it

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

line 1why, one step back
1import asyncio

Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186

line 2why, one step back
2import logging

added netrc support (#177)Can Sarıgölbefore adoption919e8d3

lines 3–4why, one step back
3import os
4

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 5–6why, one step back
5import pytest
6

Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186

lines 7–8why, one step back
7import httpx
8from httpx import utils

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

lines 9–10why, one step back
9from httpx.utils import (
10 ElapsedTimer,

Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7

line 11why, one step back
11 get_ca_bundle_from_env,

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

line 12why, one step back
12 get_environment_proxies,

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

lines 13–16why, one step back
13 get_netrc_login,
14 guess_json_utf,15 parse_header_links,16)

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

lines 17–50why, one step back
17
1819@pytest.mark.parametrize(20 "encoding",21 (22 "utf-32",23 "utf-8-sig",24 "utf-16",25 "utf-8",26 "utf-16-be",27 "utf-16-le",28 "utf-32-be",29 "utf-32-le",30 ),31)32def test_encoded(encoding):33 data = "{}".encode(encoding)34 assert guess_json_utf(data) == encoding353637def test_bad_utf_like_encoding():38 assert guess_json_utf(b"\x00\x00\x00\x00") is None394041@pytest.mark.parametrize(42 ("encoding", "expected"),43 (44 ("utf-16-be", "utf-16"),45 ("utf-16-le", "utf-16"),46 ("utf-32-be", "utf-32"),47 ("utf-32-le", "utf-32"),48 ),49)50def test_guess_by_bom(encoding, expected):

Use nox for automation and CI (#128)Seth Michael Larsonbefore adoption80dc3e2

line 51why, one step back
51 data = "\ufeff{}".encode(encoding)

Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006

line 52why, one step back
52 assert guess_json_utf(data) == expected

added netrc support (#177)Can Sarıgölbefore adoption919e8d3

lines 53–63why, one step back
53
5455def test_bad_get_netrc_login():56 assert get_netrc_login("url") is None5758 os.environ["NETRC"] = "tests/.netrc"59 assert get_netrc_login("url") is None6061 os.environ["NETRC"] = "wrongpath"62 assert get_netrc_login("url") is None63

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

lines 64–69why, one step back
64 from httpx import utils
6566 utils.NETRC_STATIC_FILES = ()67 os.environ["NETRC"] = ""68 assert utils.get_netrc_login("url") is None69

added netrc support (#177)Can Sarıgölbefore adoption919e8d3

lines 70–77why, one step back
70
71def test_get_netrc_login():72 os.environ["NETRC"] = "tests/.netrc"73 assert get_netrc_login("netrcexample.org") == (74 "example-username",75 None,76 "example-password",77 )

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

lines 78–100why, one step back
78
7980@pytest.mark.parametrize(81 "value, expected",82 (83 (84 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',85 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],86 ),87 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),88 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),89 (90 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',91 [92 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},93 {"url": "http://.../back.jpeg"},94 ],95 ),96 ("", []),97 ),98)99def test_parse_header_links(value, expected):100 assert parse_header_links(value) == expected

Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186

lines 101–112why, one step back
101
102103@pytest.mark.asyncio104@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])105async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):106 os.environ["HTTPX_DEBUG"] = httpx_debug107108 # Force a reload on the logging handlers109 utils._LOGGER_INITIALIZED = False110 utils.get_logger("httpx")111112 async with httpx.AsyncClient() as client:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 113why, one step back
113 await client.get(server.url)

Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186

lines 114–121why, one step back
114
115 if httpx_debug in ("1", "True"):116 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err117 else:118 assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err119120 # Reset the logger so we don't have verbose output in all unit tests121 logging.getLogger("httpx").handlers = []

Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7

lines 122–157why, one step back
122
123124def test_get_ssl_cert_file():125 # Two environments is not set.126 assert get_ca_bundle_from_env() is None127128 os.environ["SSL_CERT_DIR"] = "tests/"129 # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.130 assert get_ca_bundle_from_env() == "tests"131132 del os.environ["SSL_CERT_DIR"]133 os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"134 # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.135 assert get_ca_bundle_from_env() == "tests/test_utils.py"136137 os.environ["SSL_CERT_FILE"] = "wrongfile"138 # SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.139 assert get_ca_bundle_from_env() is None140141 del os.environ["SSL_CERT_FILE"]142 os.environ["SSL_CERT_DIR"] = "wrongpath"143 # SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.144 assert get_ca_bundle_from_env() is None145146 os.environ["SSL_CERT_DIR"] = "tests/"147 os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"148 # Two environments is correctly set.149 assert get_ca_bundle_from_env() == "tests/test_utils.py"150151 os.environ["SSL_CERT_FILE"] = "wrongfile"152 # Two environments is set but SSL_CERT_FILE is not a file.153 assert get_ca_bundle_from_env() == "tests"154155 os.environ["SSL_CERT_DIR"] = "wrongpath"156 # Two environments is set but both are not correct.157 assert get_ca_bundle_from_env() is None

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

lines 158–168why, one step back
158
159160@pytest.mark.asyncio161async def test_elapsed_timer():162 with ElapsedTimer() as timer:163 assert timer.elapsed.total_seconds() == pytest.approx(0, abs=0.05)164 await asyncio.sleep(0.1)165 await asyncio.sleep(166 0.1167 ) # test to ensure time spent after timer exits isn't accounted for.168 assert timer.elapsed.total_seconds() == pytest.approx(0.1, abs=0.05)

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

lines 169–194why, one step back
169
170171@pytest.mark.parametrize(172 ["environment", "proxies"],173 [174 ({}, {}),175 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http": "http://127.0.0.1"}),176 (177 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},178 {"https": "http://127.0.0.1", "http": "https://127.0.0.1"},179 ),180 (181 {"all_proxy": "http://127.0.0.1", "ALL_PROXY": "https://1.1.1.1"},182 {"all": "http://127.0.0.1"},183 ),184 (185 {"https_proxy": "http://127.0.0.1", "HTTPS_PROXY": "https://1.1.1.1"},186 {"https": "http://127.0.0.1"},187 ),188 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),189 ],190)191def test_get_environment_proxies(environment, proxies):192 os.environ.update(environment)193194 assert get_environment_proxies() == proxies