| 1 |
"""Tests for the HTTP layer: status-code mapping and the retry loop.""" |
| 2 |
|
| 3 |
from unittest import mock |
| 4 |
|
| 5 |
import osmapi |
| 6 |
import pytest |
| 7 |
import requests |
| 8 |
|
| 9 |
from .conftest import API_BASE, make_http_response |
| 10 |
|
| 11 |
|
| 12 |
def test_http_request_get(mock_api): |
| 13 |
api, session = mock_api() |
| 14 |
|
| 15 |
response = api._session._http_request("GET", "/api/0.6/test", False, None) |
| 16 |
|
| 17 |
session.request.assert_called_with( |
| 18 |
"GET", f"{API_BASE}/api/0.6/test", data=None, timeout=30, params=None |
| 19 |
) |
| 20 |
assert response == "test response" |
| 21 |
assert session.request.call_count == 1 |
| 22 |
|
| 23 |
|
| 24 |
def test_http_request_put(mock_api): |
| 25 |
api, session = mock_api() |
| 26 |
|
| 27 |
response = api._session._http_request("PUT", "/api/0.6/testput", False, "data") |
| 28 |
|
| 29 |
session.request.assert_called_with( |
| 30 |
"PUT", f"{API_BASE}/api/0.6/testput", data="data", timeout=30, params=None |
| 31 |
) |
| 32 |
assert response == "test response" |
| 33 |
|
| 34 |
|
| 35 |
def test_http_request_delete(mock_api): |
| 36 |
api, session = mock_api() |
| 37 |
|
| 38 |
response = api._session._http_request( |
| 39 |
"DELETE", "/api/0.6/testdelete", False, "delete data" |
| 40 |
) |
| 41 |
|
| 42 |
session.request.assert_called_with( |
| 43 |
"DELETE", |
| 44 |
f"{API_BASE}/api/0.6/testdelete", |
| 45 |
data="delete data", |
| 46 |
timeout=30, |
| 47 |
params=None, |
| 48 |
) |
| 49 |
assert response == "test response" |
| 50 |
|
| 51 |
|
| 52 |
def test_http_request_auth(mock_api): |
| 53 |
api, session = mock_api() |
| 54 |
|
| 55 |
response = api._session._http_request("PUT", "/api/0.6/testauth", True, None) |
| 56 |
|
| 57 |
session.request.assert_called_with( |
| 58 |
"PUT", f"{API_BASE}/api/0.6/testauth", data=None, timeout=30, params=None |
| 59 |
) |
| 60 |
assert session.auth == ("testuser", "testpassword") |
| 61 |
assert response == "test response" |
| 62 |
|
| 63 |
|
| 64 |
def test_http_request_auth_without_session(unauthenticated_api): |
| 65 |
"""Without a session there is no way to authenticate, so osmapi refuses.""" |
| 66 |
api, session = unauthenticated_api |
| 67 |
|
| 68 |
with pytest.raises( |
| 69 |
osmapi.AuthenticationMissingError, match="no session was provided" |
| 70 |
): |
| 71 |
api._session._http_request("PUT", "/api/0.6/testauth", True, None) |
| 72 |
|
| 73 |
# the request is never attempted |
| 74 |
assert session.request.call_count == 0 |
| 75 |
|
| 76 |
|
| 77 |
def test_http_request_auth_with_session_without_credentials(mock_api): |
| 78 |
"""A session is never second-guessed: the request is sent, the API decides. |
| 79 |
|
| 80 |
`session.auth` is not a reliable signal — requests-oauthlib sets it to a |
| 81 |
no-op lambda and adds the token per request, others only set an |
| 82 |
`Authorization` header — so a session without visible credentials is |
| 83 |
still sent, and a real authorization problem comes back as a 401. |
| 84 |
""" |
| 85 |
api, session = mock_api(auth=False) |
| 86 |
|
| 87 |
response = api._session._http_request("PUT", "/api/0.6/testauth", True, None) |
| 88 |
|
| 89 |
assert session.request.call_count == 1 |
| 90 |
assert response == "test response" |
| 91 |
|
| 92 |
|
| 93 |
def test_http_request_auth_with_authorization_header(): |
| 94 |
"""The setup OSM actually uses: a bearer token, no `session.auth` at all.""" |
| 95 |
session = requests.Session() |
| 96 |
session.headers["Authorization"] = "Bearer test-token" |
| 97 |
session.request = mock.Mock(return_value=make_http_response()) |
| 98 |
api = osmapi.OsmApi(api=API_BASE, session=session) |
| 99 |
|
| 100 |
response = api._session._http_request("PUT", "/api/0.6/testauth", True, None) |
| 101 |
|
| 102 |
assert session.request.call_count == 1 |
| 103 |
assert response == "test response" |
| 104 |
api.close() |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.parametrize( |
| 108 |
"status,expected", |
| 109 |
[ |
| 110 |
(401, osmapi.UnauthorizedApiError), |
| 111 |
(404, osmapi.ElementNotFoundApiError), |
| 112 |
(410, osmapi.ElementDeletedApiError), |
| 113 |
(500, osmapi.ApiError), |
| 114 |
], |
| 115 |
) |
| 116 |
def test_http_request_error_status_mapping(mock_api, status, expected): |
| 117 |
api, _ = mock_api(status=status) |
| 118 |
|
| 119 |
with pytest.raises(expected) as execinfo: |
| 120 |
api._session._http_request("GET", f"/api/0.6/test{status}", False, None) |
| 121 |
|
| 122 |
assert execinfo.value.status == status |
| 123 |
assert execinfo.value.reason == "test reason" |
| 124 |
assert execinfo.value.payload == "test response" |
| 125 |
|
| 126 |
|
| 127 |
def test_http_request_empty_response(mock_api): |
| 128 |
api, _ = mock_api(content="") |
| 129 |
|
| 130 |
with pytest.raises(osmapi.ResponseEmptyApiError): |
| 131 |
api._session._http_request("GET", "/api/0.6/test", False, None) |
| 132 |
|
| 133 |
|
| 134 |
def test_http_request_empty_response_allowed(mock_api): |
| 135 |
"""With `return_value=False` an empty body is not an error.""" |
| 136 |
api, _ = mock_api(content="") |
| 137 |
|
| 138 |
result = api._session._http_request( |
| 139 |
"PUT", "/api/0.6/test", False, None, return_value=False |
| 140 |
) |
| 141 |
|
| 142 |
assert result == "" |
| 143 |
|
| 144 |
|
| 145 |
def test_http_request_timeout(mock_api): |
| 146 |
api, _ = mock_api(side_effect=requests.exceptions.Timeout()) |
| 147 |
|
| 148 |
with pytest.raises(osmapi.TimeoutApiError) as execinfo: |
| 149 |
api._session._http_request("GET", "/api/0.6/test", False, None) |
| 150 |
|
| 151 |
assert execinfo.value.status == 0 |
| 152 |
assert "Request timed out (timeout=30)" in execinfo.value.reason |
| 153 |
|
| 154 |
|
| 155 |
def test_http_request_connection_error(mock_api): |
| 156 |
api, _ = mock_api(side_effect=requests.exceptions.ConnectionError("boom")) |
| 157 |
|
| 158 |
with pytest.raises(osmapi.ConnectionApiError) as execinfo: |
| 159 |
api._session._http_request("GET", "/api/0.6/test", False, None) |
| 160 |
|
| 161 |
assert execinfo.value.status == 0 |
| 162 |
assert "Connection error: boom" in execinfo.value.reason |
| 163 |
|
| 164 |
|
| 165 |
def test_http_request_generic_request_exception(mock_api): |
| 166 |
"""Any other `requests` error becomes a plain ApiError with status 0.""" |
| 167 |
api, _ = mock_api(side_effect=requests.exceptions.RequestException("nope")) |
| 168 |
|
| 169 |
with pytest.raises(osmapi.ApiError) as execinfo: |
| 170 |
api._session._http_request("GET", "/api/0.6/test", False, None) |
| 171 |
|
| 172 |
assert execinfo.value.status == 0 |
| 173 |
assert execinfo.value.reason == "nope" |
| 174 |
|
| 175 |
|
| 176 |
################################################## |
| 177 |
# Retry behaviour of _http() # |
| 178 |
################################################## |
| 179 |
|
| 180 |
|
| 181 |
def test_http_retries_server_error_then_succeeds(mock_api): |
| 182 |
api, session = mock_api( |
| 183 |
responses=[ |
| 184 |
make_http_response(status=500), |
| 185 |
make_http_response(status=502), |
| 186 |
make_http_response(status=200, content="ok"), |
| 187 |
] |
| 188 |
) |
| 189 |
|
| 190 |
result = api._session._http("GET", "/api/0.6/test", False, None) |
| 191 |
|
| 192 |
assert result == "ok" |
| 193 |
assert session.request.call_count == 3 |
| 194 |
# the first retry is immediate, only later ones wait |
| 195 |
assert api._session._sleep.call_count == 1 |
| 196 |
|
| 197 |
|
| 198 |
def test_http_gives_up_after_max_retries_on_server_error(mock_api): |
| 199 |
api, session = mock_api(status=500) |
| 200 |
|
| 201 |
with pytest.raises(osmapi.ApiError) as execinfo: |
| 202 |
api._session._http("GET", "/api/0.6/test", False, None) |
| 203 |
|
| 204 |
assert execinfo.value.status == 500 |
| 205 |
assert session.request.call_count == api._session.MAX_RETRY_LIMIT |
| 206 |
assert api._session._sleep.call_count == api._session.MAX_RETRY_LIMIT - 2 |
| 207 |
|
| 208 |
|
| 209 |
def test_http_does_not_retry_client_error(mock_api): |
| 210 |
api, session = mock_api(status=404) |
| 211 |
|
| 212 |
with pytest.raises(osmapi.ElementNotFoundApiError): |
| 213 |
api._session._http("GET", "/api/0.6/test", False, None) |
| 214 |
|
| 215 |
assert session.request.call_count == 1 |
| 216 |
assert api._session._sleep.call_count == 0 |
| 217 |
|
| 218 |
|
| 219 |
def test_http_does_not_retry_missing_authentication(unauthenticated_api): |
| 220 |
api, session = unauthenticated_api |
| 221 |
|
| 222 |
with pytest.raises(osmapi.AuthenticationMissingError): |
| 223 |
api._session._http("PUT", "/api/0.6/test", True, None) |
| 224 |
|
| 225 |
assert session.request.call_count == 0 |
| 226 |
assert api._session._sleep.call_count == 0 |
| 227 |
|
| 228 |
|
| 229 |
def test_http_retries_unexpected_exception(mock_api): |
| 230 |
api, session = mock_api(side_effect=ValueError("kaboom")) |
| 231 |
|
| 232 |
with pytest.raises( |
| 233 |
osmapi.MaximumRetryLimitReachedError, match="Give up after 5 retries" |
| 234 |
): |
| 235 |
api._session._http("GET", "/api/0.6/test", False, None) |
| 236 |
|
| 237 |
assert session.request.call_count == api._session.MAX_RETRY_LIMIT |
| 238 |
assert api._session._sleep.call_count == api._session.MAX_RETRY_LIMIT - 2 |
| 239 |
|
| 240 |
|
| 241 |
def test_http_reraises_osm_api_error_after_max_retries(mock_api): |
| 242 |
"""An OsmApiError is re-raised as-is rather than wrapped.""" |
| 243 |
api, session = mock_api(side_effect=osmapi.XmlResponseInvalidError("bad xml")) |
| 244 |
|
| 245 |
with pytest.raises(osmapi.XmlResponseInvalidError, match="bad xml"): |
| 246 |
api._session._http("GET", "/api/0.6/test", False, None) |
| 247 |
|
| 248 |
assert session.request.call_count == api._session.MAX_RETRY_LIMIT |
| 249 |
|
| 250 |
|
| 251 |
def test_http_recreates_session_between_retries(mock_api): |
| 252 |
api, session = mock_api( |
| 253 |
responses=[make_http_response(status=500), make_http_response(content="ok")] |
| 254 |
) |
| 255 |
headers_updates_before = session.headers.update.call_count |
| 256 |
|
| 257 |
api._session._http("GET", "/api/0.6/test", False, None) |
| 258 |
|
| 259 |
# _get_http_session() re-applies auth and the user-agent on every rebuild |
| 260 |
assert session.headers.update.call_count == headers_updates_before + 1 |
| 261 |
|
| 262 |
|
| 263 |
def test_sleep_waits_five_seconds(): |
| 264 |
session = osmapi.http.OsmApiSession(API_BASE, "osmapi/test") |
| 265 |
|
| 266 |
with mock.patch("osmapi.http.time.sleep") as sleep: |
| 267 |
session._sleep() |
| 268 |
|
| 269 |
sleep.assert_called_once_with(5) |
| 270 |
session.close() |