-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_poll_http.py
More file actions
175 lines (136 loc) · 6.92 KB
/
Copy pathtest_poll_http.py
File metadata and controls
175 lines (136 loc) · 6.92 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Direct unit tests for `poll.http` (roadmap M4, ADR-0025, extended ADR-0031).
`tests/test_poll_adapters.py` already covers `get_json`/`strip_html`'s original ADR-0025
behavior end to end via the greenhouse/lever adapters. This file covers the primitives ADR-0031
added (`post_json`, `get_text`, retries, `HttpError`, `BlockedUrlError`, `is_public_http_url`)
directly, at the module's own boundary (mocking `urlopen`), rather than only incidentally through
whichever adapter happens to call them.
"""
from __future__ import annotations
import email.message
import json
from unittest.mock import MagicMock, patch
from urllib.error import HTTPError, URLError
import pytest
from openjobradar.poll.http import (
BlockedUrlError,
HttpError,
UnsupportedUrlError,
get_json,
get_text,
is_public_http_url,
post_json,
strip_html,
)
def _response(status: int, body: bytes) -> MagicMock:
resp = MagicMock()
resp.status = status
resp.read.return_value = body
resp.__enter__.return_value = resp
resp.__exit__.return_value = False
return resp
class TestGetJson:
def test_rejects_non_https_urls(self) -> None:
with pytest.raises(UnsupportedUrlError):
get_json("http://example.com/insecure")
with pytest.raises(UnsupportedUrlError):
get_json("file:///etc/passwd")
def test_parses_successful_response(self) -> None:
with patch("openjobradar.poll.http.urlopen", return_value=_response(200, b'{"a": 1}')):
assert get_json("https://example.com/api") == {"a": 1}
def test_raises_http_error_on_4xx(self) -> None:
with (
patch("openjobradar.poll.http.urlopen", return_value=_response(404, b"{}")),
pytest.raises(HttpError) as exc_info,
):
get_json("https://example.com/api")
assert exc_info.value.status == 404
def test_http_error_response_is_parsed_not_raised_by_urlopen(self) -> None:
# urlopen itself raises HTTPError for a 4xx/5xx; get_json must still surface it as our
# own HttpError with the real status, not let the raw HTTPError escape.
err = HTTPError(
"https://example.com/api", 500, "boom", email.message.Message(), MagicMock(read=lambda: b"{}")
)
with patch("openjobradar.poll.http.urlopen", side_effect=err), pytest.raises(HttpError) as exc_info:
get_json("https://example.com/api")
assert exc_info.value.status == 500
class TestPostJson:
def test_rejects_non_https_urls(self) -> None:
with pytest.raises(UnsupportedUrlError):
post_json("http://example.com/api", {})
def test_posts_body_and_parses_response(self) -> None:
with patch("openjobradar.poll.http.urlopen", return_value=_response(200, b'{"ok": true}')) as mock_open:
result = post_json("https://example.com/api", {"q": "engineer"})
assert result == {"ok": True}
request = mock_open.call_args[0][0]
assert request.method == "POST"
assert json.loads(request.data) == {"q": "engineer"}
def test_raises_http_error_on_failure_status(self) -> None:
with (
patch("openjobradar.poll.http.urlopen", return_value=_response(403, b"{}")),
pytest.raises(HttpError),
):
post_json("https://example.com/api", {})
class TestGetText:
def test_returns_status_and_body_without_raising(self) -> None:
with patch("openjobradar.poll.http.urlopen", return_value=_response(200, b"hello")):
status, body = get_text("https://example.com/page")
assert status == 200
assert body == "hello"
def test_returns_error_status_rather_than_raising(self) -> None:
with patch("openjobradar.poll.http.urlopen", return_value=_response(404, b"not found")):
status, body = get_text("https://example.com/missing")
assert status == 404
assert body == "not found"
def test_rejects_non_https_urls(self) -> None:
with pytest.raises(UnsupportedUrlError):
get_text("http://example.com/page")
class TestRetries:
def test_retries_transient_transport_errors_then_succeeds(self) -> None:
calls = [URLError("connection reset"), _response(200, b'{"a": 1}')]
def fake_urlopen(request: object, timeout: int) -> object:
result = calls.pop(0)
if isinstance(result, Exception):
raise result
return result
with patch("openjobradar.poll.http.urlopen", side_effect=fake_urlopen), patch(
"openjobradar.poll.http.time.sleep"
):
assert get_json("https://example.com/api", retries=1) == {"a": 1}
def test_exhausts_retries_and_raises(self) -> None:
with patch("openjobradar.poll.http.urlopen", side_effect=URLError("down")), patch(
"openjobradar.poll.http.time.sleep"
), pytest.raises(URLError):
get_json("https://example.com/api", retries=2)
class TestStripHtml:
def test_removes_tags_and_collapses_whitespace(self) -> None:
assert strip_html("<p>Hello <b>world</b></p>\n<p>!</p>") == "Hello world !"
def test_of_empty_string_is_empty(self) -> None:
assert strip_html("") == ""
def test_handles_double_encoded_entities(self) -> None:
assert strip_html("<p>Hi</p>") == "Hi"
class TestIsPublicHttpUrl:
def test_rejects_non_https_scheme(self) -> None:
assert is_public_http_url("http://example.com") is False
assert is_public_http_url("ftp://example.com") is False
def test_rejects_url_with_embedded_credentials(self) -> None:
assert is_public_http_url("https://user:pass@example.com") is False
def test_rejects_malformed_url(self) -> None:
assert is_public_http_url("https://[::not-valid") is False
def test_rejects_private_and_link_local_hosts(self) -> None:
with patch("openjobradar.poll.http.socket.getaddrinfo") as mock_dns:
mock_dns.return_value = [(None, None, None, None, ("169.254.169.254", 0))]
assert is_public_http_url("https://metadata.internal/") is False
def test_accepts_a_resolvable_public_host(self) -> None:
with patch("openjobradar.poll.http.socket.getaddrinfo") as mock_dns:
mock_dns.return_value = [(None, None, None, None, ("93.184.216.34", 0))]
assert is_public_http_url("https://example.com/careers") is True
def test_fails_closed_when_dns_resolution_fails(self) -> None:
with patch("openjobradar.poll.http.socket.getaddrinfo", side_effect=OSError("no dns")):
assert is_public_http_url("https://nonexistent.invalid/") is False
def test_http_error_message_includes_detail() -> None:
err = HttpError(404, "https://example.com", "board not found")
assert "404" in str(err)
assert "board not found" in str(err)
def test_blocked_url_error_message_includes_reason() -> None:
err = BlockedUrlError("https://169.254.169.254/", "private address")
assert "private address" in str(err)