|
| 1 | +"""Tests for RFC 9457 (Problem Details for HTTP APIs) support.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pip._internal.exceptions import HTTPProblemDetailsError, NetworkConnectionError |
| 8 | +from pip._internal.network.rfc9457 import ( |
| 9 | + ProblemDetails, |
| 10 | + is_problem_details_response, |
| 11 | + parse_problem_details, |
| 12 | +) |
| 13 | +from pip._internal.network.utils import raise_for_status |
| 14 | +from tests.lib.requests_mocks import MockResponse |
| 15 | + |
| 16 | + |
| 17 | +class TestProblemDetails: |
| 18 | + def test_from_dict(self) -> None: |
| 19 | + data = { |
| 20 | + "status": 404, |
| 21 | + "title": "Not Found", |
| 22 | + "detail": "Resource not found", |
| 23 | + } |
| 24 | + |
| 25 | + problem = ProblemDetails.from_dict(data) |
| 26 | + assert problem.status == 404 |
| 27 | + assert problem.title == "Not Found" |
| 28 | + assert problem.detail == "Resource not found" |
| 29 | + |
| 30 | + def test_from_json(self) -> None: |
| 31 | + json_str = json.dumps({ |
| 32 | + "status": 404, |
| 33 | + "title": "Not Found", |
| 34 | + "detail": "Resource not found", |
| 35 | + }) |
| 36 | + |
| 37 | + problem = ProblemDetails.from_json(json_str) |
| 38 | + assert problem.status == 404 |
| 39 | + assert problem.title == "Not Found" |
| 40 | + |
| 41 | + def test_string_representation(self) -> None: |
| 42 | + """Test string representation of ProblemDetails.""" |
| 43 | + problem = ProblemDetails( |
| 44 | + status=403, |
| 45 | + title="Access Denied", |
| 46 | + detail="Your API token does not have permission", |
| 47 | + ) |
| 48 | + |
| 49 | + str_repr = str(problem) |
| 50 | + assert "Access Denied" in str_repr |
| 51 | + assert "403" in str_repr |
| 52 | + assert "API token" in str_repr |
| 53 | + |
| 54 | +class TestIsProblemDetailsResponse: |
| 55 | + def test_detects_problem_json_content_type(self) -> None: |
| 56 | + resp = MockResponse(b"") |
| 57 | + resp.headers = {"Content-Type": "application/problem+json"} |
| 58 | + |
| 59 | + assert is_problem_details_response(resp) is True |
| 60 | + |
| 61 | + def test_detects_problem_json_with_charset(self) -> None: |
| 62 | + resp = MockResponse(b"") |
| 63 | + resp.headers = {"Content-Type": "application/problem+json; charset=utf-8"} |
| 64 | + |
| 65 | + assert is_problem_details_response(resp) is True |
| 66 | + |
| 67 | + def test_does_not_detect_regular_json(self) -> None: |
| 68 | + resp = MockResponse(b"") |
| 69 | + resp.headers = {"Content-Type": "application/json"} |
| 70 | + |
| 71 | + assert is_problem_details_response(resp) is False |
| 72 | + |
| 73 | + def test_does_not_detect_without_content_type(self) -> None: |
| 74 | + resp = MockResponse(b"") |
| 75 | + resp.headers = {} |
| 76 | + |
| 77 | + assert is_problem_details_response(resp) is False |
| 78 | + |
| 79 | +class TestParseProblemDetails: |
| 80 | + def test_parses_valid_problem_details(self) -> None: |
| 81 | + problem_data = { |
| 82 | + "status": 404, |
| 83 | + "title": "Not Found", |
| 84 | + "detail": "The package 'test-package' was not found", |
| 85 | + } |
| 86 | + resp = MockResponse(json.dumps(problem_data).encode()) |
| 87 | + resp.headers = {"Content-Type": "application/problem+json"} |
| 88 | + resp.status_code = 404 |
| 89 | + |
| 90 | + problem = parse_problem_details(resp) |
| 91 | + assert problem is not None |
| 92 | + assert problem.status == 404 |
| 93 | + assert problem.title == "Not Found" |
| 94 | + assert "test-package" in problem.detail |
| 95 | + |
| 96 | + def test_returns_none_for_non_problem_details(self) -> None: |
| 97 | + resp = MockResponse(b"<html>Error</html>") |
| 98 | + resp.headers = {"Content-Type": "text/html"} |
| 99 | + |
| 100 | + problem = parse_problem_details(resp) |
| 101 | + assert problem is None |
| 102 | + |
| 103 | + def test_handles_malformed_json(self) -> None: |
| 104 | + resp = MockResponse(b"not valid json") |
| 105 | + resp.headers = {"Content-Type": "application/problem+json"} |
| 106 | + |
| 107 | + problem = parse_problem_details(resp) |
| 108 | + assert problem is None |
| 109 | + |
| 110 | +@pytest.mark.parametrize( |
| 111 | + "status_code, title, detail", |
| 112 | + [ |
| 113 | + (404, "Package Not Found", "The requested package does not exist"), |
| 114 | + (500, "Internal Server Error", "An unexpected error occurred"), |
| 115 | + (403, "Forbidden", "Access denied to this resource"), |
| 116 | + ], |
| 117 | +) |
| 118 | + |
| 119 | +class TestRaiseForStatusWithProblemDetails: |
| 120 | + def test_raises_http_problem_details_error( |
| 121 | + self, status_code: int, title: str, detail: str |
| 122 | + ) -> None: |
| 123 | + problem_data = { |
| 124 | + "status": status_code, |
| 125 | + "title": title, |
| 126 | + "detail": detail, |
| 127 | + } |
| 128 | + resp = MockResponse(json.dumps(problem_data).encode()) |
| 129 | + resp.status_code = status_code |
| 130 | + resp.headers = {"Content-Type": "application/problem+json"} |
| 131 | + resp.url = "https://pypi.org/simple/some-package/" |
| 132 | + |
| 133 | + with pytest.raises(HTTPProblemDetailsError) as excinfo: |
| 134 | + raise_for_status(resp) |
| 135 | + |
| 136 | + assert excinfo.value.problem_details.status == status_code |
| 137 | + assert excinfo.value.problem_details.title == title |
| 138 | + assert excinfo.value.response == resp |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.parametrize( |
| 142 | + "status_code, error_type", |
| 143 | + [ |
| 144 | + (404, "Client Error"), |
| 145 | + (500, "Server Error"), |
| 146 | + (403, "Client Error"), |
| 147 | + ], |
| 148 | +) |
| 149 | + |
| 150 | +class TestRaiseForStatusBackwardCompatibility: |
| 151 | + def test_raises_network_connection_error( |
| 152 | + self, status_code: int, error_type: str |
| 153 | + ) -> None: |
| 154 | + resp = MockResponse(b"<html>Error</html>") |
| 155 | + resp.status_code = status_code |
| 156 | + resp.headers = {"Content-Type": "text/html"} |
| 157 | + resp.url = "https://pypi.org/simple/nonexistent-package/" |
| 158 | + resp.reason = "Error" |
| 159 | + |
| 160 | + with pytest.raises(NetworkConnectionError) as excinfo: |
| 161 | + raise_for_status(resp) |
| 162 | + |
| 163 | + assert f"{status_code} {error_type}" in str(excinfo.value) |
| 164 | + assert excinfo.value.response == resp |
0 commit comments