forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_http_utils.py
More file actions
97 lines (68 loc) · 3.31 KB
/
Copy pathtest_http_utils.py
File metadata and controls
97 lines (68 loc) · 3.31 KB
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
89
90
91
92
93
94
95
96
97
"""Tests for http_utils module (Issue #1207)."""
from __future__ import annotations
import os
import urllib.request
from unittest.mock import patch
from scripts.http_utils import get_proxy_url, get_proxy_handler, get_proxy_opener
def test_get_proxy_url_from_https_proxy(monkeypatch):
"""HTTPS_PROXY takes precedence."""
monkeypatch.setenv("HTTPS_PROXY", "http://proxy.corp.com:8080")
monkeypatch.delenv("HTTP_PROXY", raising=False)
assert get_proxy_url() == "http://proxy.corp.com:8080"
def test_get_proxy_url_from_http_proxy(monkeypatch):
"""Falls back to HTTP_PROXY if HTTPS_PROXY not set."""
monkeypatch.delenv("HTTPS_PROXY", raising=False)
monkeypatch.setenv("HTTP_PROXY", "http://proxy.corp.com:3128")
assert get_proxy_url() == "http://proxy.corp.com:3128"
def test_get_proxy_url_no_proxy(monkeypatch):
"""Returns None when no proxy configured."""
monkeypatch.delenv("HTTPS_PROXY", raising=False)
monkeypatch.delenv("HTTP_PROXY", raising=False)
monkeypatch.delenv("https_proxy", raising=False)
monkeypatch.delenv("http_proxy", raising=False)
assert get_proxy_url() is None
def test_get_proxy_handler_with_proxy(monkeypatch):
"""Returns ProxyHandler when proxy configured."""
monkeypatch.setenv("HTTPS_PROXY", "http://proxy.corp.com:8080")
handler = get_proxy_handler()
assert handler is not None
assert isinstance(handler, urllib.request.ProxyHandler)
def test_get_proxy_handler_no_proxy(monkeypatch):
"""Returns None when no proxy configured."""
monkeypatch.delenv("HTTPS_PROXY", raising=False)
monkeypatch.delenv("HTTP_PROXY", raising=False)
handler = get_proxy_handler()
assert handler is None
def test_get_proxy_opener_with_proxy(monkeypatch):
"""Returns opener with proxy handler."""
monkeypatch.setenv("HTTPS_PROXY", "http://proxy.corp.com:8080")
opener = get_proxy_opener()
assert opener is not None
# Verify proxy handler is in opener's handlers
handler_types = [type(h).__name__ for h in opener.handlers]
assert "ProxyHandler" in handler_types
def test_get_proxy_opener_no_proxy(monkeypatch):
"""Returns default opener when no proxy."""
monkeypatch.delenv("HTTPS_PROXY", raising=False)
monkeypatch.delenv("HTTP_PROXY", raising=False)
opener = get_proxy_opener()
assert opener is not None
# Should not have proxy handler
handler_types = [type(h).__name__ for h in opener.handlers]
assert "ProxyHandler" not in handler_types
def test_case_insensitive_env_vars(monkeypatch):
"""Lowercase env vars also work."""
monkeypatch.setenv("https_proxy", "http://proxy.corp.com:8080")
assert get_proxy_url() == "http://proxy.corp.com:8080"
if __name__ == "__main__":
import tempfile
monkeypatch = type("Monkeypatch", (), {"setenv": staticmethod(lambda k, v: os.environ.update({k: v})), "delenv": staticmethod(lambda k, **kw: os.environ.pop(k, None))})()
test_get_proxy_url_from_https_proxy(monkeypatch)
test_get_proxy_url_from_http_proxy(monkeypatch)
test_get_proxy_url_no_proxy(monkeypatch)
test_get_proxy_handler_with_proxy(monkeypatch)
test_get_proxy_handler_no_proxy(monkeypatch)
test_get_proxy_opener_with_proxy(monkeypatch)
test_get_proxy_opener_no_proxy(monkeypatch)
test_case_insensitive_env_vars(monkeypatch)
print("All tests passed ✓")