Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

130 lines · 3.3 KiB · Python

Last changed by

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

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–14why, one step back
9from httpx.utils import (
10 ElapsedTimer,11 get_netrc_login,12 guess_json_utf,13 parse_header_links,14)

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

lines 15–48why, one step back
15
1617@pytest.mark.parametrize(18 "encoding",19 (20 "utf-32",21 "utf-8-sig",22 "utf-16",23 "utf-8",24 "utf-16-be",25 "utf-16-le",26 "utf-32-be",27 "utf-32-le",28 ),29)30def test_encoded(encoding):31 data = "{}".encode(encoding)32 assert guess_json_utf(data) == encoding333435def test_bad_utf_like_encoding():36 assert guess_json_utf(b"\x00\x00\x00\x00") is None373839@pytest.mark.parametrize(40 ("encoding", "expected"),41 (42 ("utf-16-be", "utf-16"),43 ("utf-16-le", "utf-16"),44 ("utf-32-be", "utf-32"),45 ("utf-32-le", "utf-32"),46 ),47)48def test_guess_by_bom(encoding, expected):

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

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

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

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

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

lines 51–61why, one step back
51
5253def test_bad_get_netrc_login():54 assert get_netrc_login("url") is None5556 os.environ["NETRC"] = "tests/.netrc"57 assert get_netrc_login("url") is None5859 os.environ["NETRC"] = "wrongpath"60 assert get_netrc_login("url") is None61

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

lines 62–67why, one step back
62 from httpx import utils
6364 utils.NETRC_STATIC_FILES = ()65 os.environ["NETRC"] = ""66 assert utils.get_netrc_login("url") is None67

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

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

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

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

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

lines 99–110why, one step back
99
100101@pytest.mark.asyncio102@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])103async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):104 os.environ["HTTPX_DEBUG"] = httpx_debug105106 # Force a reload on the logging handlers107 utils._LOGGER_INITIALIZED = False108 utils.get_logger("httpx")109110 async with httpx.AsyncClient() as client:

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

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

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

lines 112–119why, one step back
112
113 if httpx_debug in ("1", "True"):114 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err115 else:116 assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err117118 # Reset the logger so we don't have verbose output in all unit tests119 logging.getLogger("httpx").handlers = []

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

lines 120–130why, one step back
120
121122@pytest.mark.asyncio123async def test_elapsed_timer():124 with ElapsedTimer() as timer:125 assert timer.elapsed.total_seconds() == pytest.approx(0, abs=0.05)126 await asyncio.sleep(0.1)127 await asyncio.sleep(128 0.1129 ) # test to ensure time spent after timer exits isn't accounted for.130 assert timer.elapsed.total_seconds() == pytest.approx(0.1, abs=0.05)