test_utils.py
This file
Path
tests/test_utils.py
209 lines · 6.0 KiB · Python
Last changed by
Move remaining utility functions from _utils.py to _models.py (#3387)RafaelWObefore adoption41597ad
History of this fileTake it
The walk stopped after 200 commits: lines older than ab8177c point at it.
Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
line 1why, one step back1import jsonBump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 2–28why, one step back2import logging3import os4import random56import pytest78import httpx9from httpx._utils import (10 URLPattern,11 get_environment_proxies,12)131415@pytest.mark.parametrize(16 "encoding",17 (18 "utf-32",19 "utf-8-sig",20 "utf-16",21 "utf-8",22 "utf-16-be",23 "utf-16-le",24 "utf-32-be",25 "utf-32-le",26 ),27)28def test_encoded(encoding):Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
lines 29–31why, one step back29 content = '{"abc": 123}'.encode(encoding)30 response = httpx.Response(200, content=content)31 assert response.json() == {"abc": 123}Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 32–34why, one step back323334def test_bad_utf_like_encoding():Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
lines 35–38why, one step back35 content = b"\x00\x00\x00\x00"36 response = httpx.Response(200, content=content)37 with pytest.raises(json.decoder.JSONDecodeError):38 response.json()Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 39–50why, one step back394041@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):Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
lines 51–53why, one step back51 content = '\ufeff{"abc": 123}'.encode(encoding)52 response = httpx.Response(200, content=content)53 assert response.json() == {"abc": 123}Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 54–80why, one step back545556def test_logging_request(server, caplog):57 caplog.set_level(logging.INFO)58 with httpx.Client() as client:59 response = client.get(server.url)60 assert response.status_code == 2006162 assert caplog.record_tuples == [63 (64 "httpx",65 logging.INFO,66 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',67 )68 ]697071def test_logging_redirect_chain(server, caplog):72 caplog.set_level(logging.INFO)73 with httpx.Client(follow_redirects=True) as client:74 response = client.get(server.url.copy_with(path="/redirect_301"))75 assert response.status_code == 2007677 assert caplog.record_tuples == [78 (79 "httpx",80 logging.INFO,Ruff linter: Use the default line-length (#2922)T-256before adoptionfd60b18
lines 81–82why, one step back81 "HTTP Request: GET http://127.0.0.1:8000/redirect_301"82 ' "HTTP/1.1 301 Moved Permanently"',Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 83–102why, one step back83 ),84 (85 "httpx",86 logging.INFO,87 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',88 ),89 ]909192@pytest.mark.parametrize(93 ["environment", "proxies"],94 [95 ({}, {}),96 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),97 (98 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},99 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},100 ),101 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),102 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),fix: NO_PROXY should support IPv4, IPv6 and localhost (#2659)Jiayun Shenbefore adoption15d09a3
lines 103–108why, one step back103 ({"no_proxy": "127.0.0.1"}, {"all://127.0.0.1": None}),104 ({"no_proxy": "192.168.0.0/16"}, {"all://192.168.0.0/16": None}),105 ({"no_proxy": "::1"}, {"all://[::1]": None}),106 ({"no_proxy": "localhost"}, {"all://localhost": None}),107 ({"no_proxy": "github.com"}, {"all://*github.com": None}),108 ({"no_proxy": ".github.com"}, {"all://*.github.com": None}),Fix environment proxies (#2741)Kar Petrosyanbefore adoption3b9060e
line 109why, one step back109 ({"no_proxy": "http://github.com"}, {"http://github.com": None}),Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 110–118why, one step back110 ],111)112def test_get_environment_proxies(environment, proxies):113 os.environ.update(environment)114115 assert get_environment_proxies() == proxies116117118def test_same_origin():test `same_origin` via public api (#3062)T-256before adoptionfc84f7f
lines 119–120why, one step back119 origin = httpx.URL("https://example.com")120 request = httpx.Request("GET", "HTTPS://EXAMPLE.COM:443")Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 121why, one step back121test `same_origin` via public api (#3062)T-256before adoptionfc84f7f
lines 122–123why, one step back122 client = httpx.Client()123 headers = client._redirect_headers(request, origin, "GET")Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 124why, one step back124test `same_origin` via public api (#3062)T-256before adoptionfc84f7f
lines 125–127why, one step back125 assert headers["Host"] == request.url.netloc.decode("ascii")126127Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 128why, one step back128def test_not_same_origin():test `same_origin` via public api (#3062)T-256before adoptionfc84f7f
lines 129–135why, one step back129 origin = httpx.URL("https://example.com")130 request = httpx.Request("GET", "HTTP://EXAMPLE.COM:80")131132 client = httpx.Client()133 headers = client._redirect_headers(request, origin, "GET")134135 assert headers["Host"] == origin.netloc.decode("ascii")Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 136–138why, one step back136137138def test_is_https_redirect():test `is_https_redirect` via public api (#3064)T-256before adoptione745060
lines 139–142why, one step back139 url = httpx.URL("https://example.com")140 request = httpx.Request(141 "GET", "http://example.com", headers={"Authorization": "empty"}142 )Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 143why, one step back143test `is_https_redirect` via public api (#3064)T-256before adoptione745060
lines 144–145why, one step back144 client = httpx.Client()145 headers = client._redirect_headers(request, url, "GET")Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 146why, one step back146test `is_https_redirect` via public api (#3064)T-256before adoptione745060
lines 147–149why, one step back147 assert "Authorization" in headers148149Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 150why, one step back150def test_is_not_https_redirect():test `is_https_redirect` via public api (#3064)T-256before adoptione745060
lines 151–154why, one step back151 url = httpx.URL("https://www.example.com")152 request = httpx.Request(153 "GET", "http://example.com", headers={"Authorization": "empty"}154 )Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 155why, one step back155test `is_https_redirect` via public api (#3064)T-256before adoptione745060
lines 156–157why, one step back156 client = httpx.Client()157 headers = client._redirect_headers(request, url, "GET")Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 158why, one step back158test `is_https_redirect` via public api (#3064)T-256before adoptione745060
lines 159–161why, one step back159 assert "Authorization" not in headers160161Bump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
line 162why, one step back162def test_is_not_https_redirect_if_not_default_ports():test `is_https_redirect` via public api (#3064)T-256before adoptione745060
lines 163–171why, one step back163 url = httpx.URL("https://example.com:1337")164 request = httpx.Request(165 "GET", "http://example.com:9999", headers={"Authorization": "empty"}166 )167168 client = httpx.Client()169 headers = client._redirect_headers(request, url, "GET")170171 assert "Authorization" not in headersBump coverage from 7.2.1 to 7.2.2 (#2641)dependabot[bot]before adoptionab8177c
lines 172–209why, one step back172173174@pytest.mark.parametrize(175 ["pattern", "url", "expected"],176 [177 ("http://example.com", "http://example.com", True),178 ("http://example.com", "https://example.com", False),179 ("http://example.com", "http://other.com", False),180 ("http://example.com:123", "http://example.com:123", True),181 ("http://example.com:123", "http://example.com:456", False),182 ("http://example.com:123", "http://example.com", False),183 ("all://example.com", "http://example.com", True),184 ("all://example.com", "https://example.com", True),185 ("http://", "http://example.com", True),186 ("http://", "https://example.com", False),187 ("all://", "https://example.com:123", True),188 ("", "https://example.com:123", True),189 ],190)191def test_url_matches(pattern, url, expected):192 pattern = URLPattern(pattern)193 assert pattern.matches(httpx.URL(url)) == expected194195196def test_pattern_priority():197 matchers = [198 URLPattern("all://"),199 URLPattern("http://"),200 URLPattern("http://example.com"),201 URLPattern("http://example.com:123"),202 ]203 random.shuffle(matchers)204 assert sorted(matchers) == [205 URLPattern("http://example.com:123"),206 URLPattern("http://example.com"),207 URLPattern("http://"),208 URLPattern("all://"),209 ]