|
1 | 1 | """Tests for the download/language functionality of LanguageTool.""" |
2 | 2 |
|
| 3 | +import io |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
3 | 6 | import pytest |
4 | 7 |
|
5 | | -from language_tool_python.exceptions import LanguageToolError |
| 8 | +from language_tool_python.download_lt import http_get |
| 9 | +from language_tool_python.exceptions import LanguageToolError, PathError |
6 | 10 |
|
7 | 11 |
|
8 | 12 | def test_install_inexistent_version() -> None: |
@@ -31,3 +35,51 @@ def test_inexistent_language() -> None: |
31 | 35 |
|
32 | 36 | with language_tool_python.LanguageTool("en-US") as tool, pytest.raises(ValueError): |
33 | 37 | language_tool_python.LanguageTag("xx-XX", tool._get_languages()) |
| 38 | + |
| 39 | + |
| 40 | +def test_http_get_403_forbidden() -> None: |
| 41 | + """ |
| 42 | + Test that http_get raises PathError when receiving a 403 Forbidden status code. |
| 43 | + This test verifies that the function correctly handles forbidden access errors |
| 44 | + when attempting to download files. |
| 45 | +
|
| 46 | + :raises AssertionError: If PathError is not raised for a 403 status code. |
| 47 | + """ |
| 48 | + mock_response = MagicMock() |
| 49 | + mock_response.status_code = 403 |
| 50 | + mock_response.headers = {} |
| 51 | + |
| 52 | + with ( |
| 53 | + patch( |
| 54 | + "language_tool_python.download_lt.requests.get", return_value=mock_response |
| 55 | + ), |
| 56 | + pytest.raises(PathError, match="Access forbidden to URL"), |
| 57 | + ): |
| 58 | + out_file = io.BytesIO() |
| 59 | + http_get("https://example.com/test.zip", out_file) |
| 60 | + |
| 61 | + |
| 62 | +def test_http_get_other_error_codes() -> None: |
| 63 | + """ |
| 64 | + Test that http_get raises PathError for various HTTP error codes (other than 404 and 403). |
| 65 | + This test verifies that the function correctly handles different HTTP error codes |
| 66 | + like 500 (Internal Server Error), 503 (Service Unavailable), etc. |
| 67 | +
|
| 68 | + :raises AssertionError: If PathError is not raised for error status codes. |
| 69 | + """ |
| 70 | + error_codes = [500, 502, 503, 504] |
| 71 | + |
| 72 | + for error_code in error_codes: |
| 73 | + mock_response = MagicMock() |
| 74 | + mock_response.status_code = error_code |
| 75 | + mock_response.headers = {} |
| 76 | + |
| 77 | + with ( |
| 78 | + patch( |
| 79 | + "language_tool_python.download_lt.requests.get", |
| 80 | + return_value=mock_response, |
| 81 | + ), |
| 82 | + pytest.raises(PathError, match=f"Failed to download.*{error_code}"), |
| 83 | + ): |
| 84 | + out_file = io.BytesIO() |
| 85 | + http_get("https://example.com/test.zip", out_file) |
0 commit comments