|
1 | | -""" |
2 | | -A test module for the http_session package. |
3 | | -""" |
| 1 | +# """ |
| 2 | +# A test module for the http_session package. |
| 3 | +# """ |
4 | 4 |
|
5 | 5 | import unittest |
6 | | -from unittest.mock import AsyncMock, patch |
7 | | - |
8 | | -from middleware.http_session import HttpSession, HttpSessionConfig |
| 6 | +from unittest.mock import AsyncMock, patch, MagicMock |
| 7 | +from aiohttp import ClientError |
| 8 | +from middleware.http_session import ( |
| 9 | + HttpSession, |
| 10 | + HttpSessionTechnicalError, |
| 11 | + HttpSessionResponseError, |
| 12 | + HttpSessionDecodeError, |
| 13 | + HttpSessionArgumentError |
| 14 | +) |
9 | 15 |
|
10 | 16 |
|
11 | 17 | class TestHttpSession(unittest.IsolatedAsyncioTestCase): |
12 | 18 | """ |
13 | 19 | The main test class |
14 | 20 | """ |
15 | 21 |
|
16 | | - async def test_get_decoded_url(self): |
| 22 | + async def asyncSetUp(self): |
| 23 | + self.session = HttpSession(config=MagicMock()) |
| 24 | + |
| 25 | + @patch("middleware.http_session.trace.get_tracer") |
| 26 | + @patch("middleware.http_session.chardet.detect", return_value={"encoding": "utf-8"}) |
| 27 | + @patch("middleware.http_session.HttpSession.get") |
| 28 | + async def test_http_success(self, mock_get, _mock_chardet, _mock_tracer): |
| 29 | + """ |
| 30 | + test good http path |
| 31 | + """ |
| 32 | + mock_response = AsyncMock() |
| 33 | + mock_response.status = 200 |
| 34 | + mock_response.read = AsyncMock(return_value=b"hello world") |
| 35 | + mock_get.return_value = MagicMock( |
| 36 | + __aenter__=AsyncMock(return_value=mock_response), |
| 37 | + __aexit__=AsyncMock(return_value=None) |
| 38 | + ) |
| 39 | + |
| 40 | + content = await self.session.get_decoded_url("http://example.com") |
| 41 | + self.assertEqual(content, "hello world") |
| 42 | + |
| 43 | + @patch("middleware.http_session.trace.get_tracer") |
| 44 | + @patch("middleware.http_session.HttpSession.get") |
| 45 | + async def test_http_5xx_raises_technical_error(self, mock_get, _mock_tracer): |
| 46 | + """ |
| 47 | + test http 5xx raises technical error |
| 48 | + """ |
| 49 | + mock_response = AsyncMock() |
| 50 | + mock_response.status = 502 |
| 51 | + mock_get.return_value = MagicMock( |
| 52 | + __aenter__=AsyncMock(return_value=mock_response), |
| 53 | + __aexit__=AsyncMock(return_value=None) |
| 54 | + ) |
| 55 | + |
| 56 | + with self.assertRaises(HttpSessionTechnicalError): |
| 57 | + await self.session.get_decoded_url("http://example.com") |
| 58 | + |
| 59 | + @patch("middleware.http_session.trace.get_tracer") |
| 60 | + @patch("middleware.http_session.HttpSession.get") |
| 61 | + async def test_http_4xx_raises_response_error(self, mock_get, _mock_tracer): |
| 62 | + """ |
| 63 | + test http 4xx raises response error |
| 64 | + """ |
| 65 | + mock_response = AsyncMock() |
| 66 | + mock_response.status = 404 |
| 67 | + mock_get.return_value = MagicMock( |
| 68 | + __aenter__=AsyncMock(return_value=mock_response), |
| 69 | + __aexit__=AsyncMock(return_value=None) |
| 70 | + ) |
| 71 | + |
| 72 | + with self.assertRaises(HttpSessionResponseError): |
| 73 | + await self.session.get_decoded_url("http://example.com") |
| 74 | + |
| 75 | + @patch("middleware.http_session.trace.get_tracer") |
| 76 | + @patch("middleware.http_session.HttpSession.get") |
| 77 | + async def test_http_network_exception(self, mock_get, _mock_tracer): |
| 78 | + """ |
| 79 | + test network exception |
| 80 | + """ |
| 81 | + mock_get.return_value = MagicMock( |
| 82 | + __aenter__=AsyncMock(side_effect=ClientError("boom")), |
| 83 | + __aexit__=AsyncMock(return_value=None) |
| 84 | + ) |
| 85 | + |
| 86 | + with self.assertRaises(HttpSessionTechnicalError): |
| 87 | + await self.session.get_decoded_url("http://example.com") |
| 88 | + |
| 89 | + @patch("middleware.http_session.trace.get_tracer") |
| 90 | + @patch("middleware.http_session.aiofiles.open") |
| 91 | + @patch("middleware.http_session.chardet.detect", return_value={"encoding": "utf-8"}) |
| 92 | + async def test_file_success(self, _mock_chardet, mock_aiofiles, _mock_tracer): |
| 93 | + """ |
| 94 | + test good file path |
| 95 | + """ |
| 96 | + mock_file = AsyncMock() |
| 97 | + mock_file.read = AsyncMock(return_value=b"file content") |
| 98 | + mock_aiofiles.return_value.__aenter__.return_value = mock_file |
| 99 | + |
| 100 | + content = await self.session.get_decoded_url("file:///tmp/test.txt") |
| 101 | + self.assertEqual(content, "file content") |
| 102 | + |
| 103 | + @patch("middleware.http_session.trace.get_tracer") |
| 104 | + async def test_unsupported_scheme_raises_argument_error(self, _mock_tracer): |
| 105 | + """ |
| 106 | + test unsupported scheme raises argument error |
| 107 | + """ |
| 108 | + with self.assertRaises(HttpSessionArgumentError): |
| 109 | + await self.session.get_decoded_url("ftp://example.com") |
| 110 | + |
| 111 | + @patch("middleware.http_session.trace.get_tracer") |
| 112 | + @patch("middleware.http_session.chardet.detect", return_value={"encoding": "utf-8"}) |
| 113 | + @patch("middleware.http_session.HttpSession.get") |
| 114 | + async def test_decode_error_raises_decode_error(self, mock_get, _mock_chardet, _mock_tracer): |
17 | 115 | """ |
18 | | - This test was created automatically with the help of Codeium. |
19 | | - It's trivial. Better tests of HttpSession.get_decoded_url would |
20 | | - take the HttpSessionConfig into account. But this is far form |
21 | | - trivial... |
| 116 | + test decode error raises decode error |
22 | 117 | """ |
23 | | - url = "https://example.com" |
24 | | - content = b'Hello, \xe4\xb8\x96\xe7\x95\x8c!' # corresponds to "Hello, 世界!" |
25 | | - encoding = "utf-8" |
26 | | - session_config = HttpSessionConfig(**{ |
27 | | - 'connection_limit': 1, |
28 | | - 'receive_timeout': 10, |
29 | | - 'connect_timeout': 10 |
30 | | - }) |
| 118 | + mock_response = AsyncMock() |
| 119 | + mock_response.status = 200 |
| 120 | + mock_response.read = AsyncMock(return_value=b"\xff\xff") |
| 121 | + mock_get.return_value = MagicMock( |
| 122 | + __aenter__=AsyncMock(return_value=mock_response), |
| 123 | + __aexit__=AsyncMock(return_value=None) |
| 124 | + ) |
| 125 | + |
| 126 | + with self.assertRaises(HttpSessionDecodeError): |
| 127 | + await self.session.get_decoded_url("http://example.com") |
| 128 | + |
| 129 | + |
| 130 | +# import unittest |
| 131 | +# from unittest.mock import AsyncMock, patch |
| 132 | + |
| 133 | +# from middleware.http_session import HttpSession, HttpSessionConfig |
| 134 | + |
| 135 | + |
| 136 | +# class TestHttpSession(unittest.IsolatedAsyncioTestCase): |
| 137 | +# """ |
| 138 | +# The main test class |
| 139 | +# """ |
| 140 | + |
| 141 | +# async def test_get_decoded_url(self): |
| 142 | +# """ |
| 143 | +# This test was created automatically with the help of Codeium. |
| 144 | +# It's trivial. Better tests of HttpSession.get_decoded_url would |
| 145 | +# take the HttpSessionConfig into account. But this is far form |
| 146 | +# trivial... |
| 147 | +# """ |
| 148 | +# url = "https://example.com" |
| 149 | +# content = b'Hello, \xe4\xb8\x96\xe7\x95\x8c!' # corresponds to "Hello, 世界!" |
| 150 | +# encoding = "utf-8" |
| 151 | +# session_config = HttpSessionConfig(**{ |
| 152 | +# 'connection_limit': 1, |
| 153 | +# 'receive_timeout': 10, |
| 154 | +# 'connect_timeout': 10 |
| 155 | +# }) |
31 | 156 |
|
32 | | - # Mock the response object |
33 | | - response = AsyncMock() |
34 | | - response.read.return_value = content |
| 157 | +# # Mock the response object |
| 158 | +# response = AsyncMock() |
| 159 | +# response.read.return_value = content |
35 | 160 |
|
36 | | - # Patch the chardet.detect function to return the expected encoding |
37 | | - with patch("chardet.detect") as mock_detect: |
38 | | - mock_detect.return_value = {"encoding": encoding} |
| 161 | +# # Patch the chardet.detect function to return the expected encoding |
| 162 | +# with patch("chardet.detect") as mock_detect: |
| 163 | +# mock_detect.return_value = {"encoding": encoding} |
39 | 164 |
|
40 | | - # Create an instance of the class |
41 | | - async with HttpSession(session_config) as session: |
| 165 | +# # Create an instance of the class |
| 166 | +# async with HttpSession(session_config) as session: |
42 | 167 |
|
43 | | - # Patch the get method to return the mocked response |
44 | | - with patch.object(session, "get") as mock_get: |
45 | | - mock_get.return_value.__aenter__.return_value = response |
| 168 | +# # Patch the get method to return the mocked response |
| 169 | +# with patch.object(session, "get") as mock_get: |
| 170 | +# mock_get.return_value.__aenter__.return_value = response |
46 | 171 |
|
47 | | - # Call the function under test |
48 | | - decoded_content = await session.get_decoded_url(url) |
| 172 | +# # Call the function under test |
| 173 | +# decoded_content = await session.get_decoded_url(url) |
49 | 174 |
|
50 | | - # Assert the decoded content |
51 | | - self.assertEqual(decoded_content, content.decode(encoding)) |
| 175 | +# # Assert the decoded content |
| 176 | +# self.assertEqual(decoded_content, content.decode(encoding)) |
52 | 177 |
|
53 | | - # Assert that the get method was called with the correct URL |
54 | | - mock_get.assert_called_once_with(url) |
| 178 | +# # Assert that the get method was called with the correct URL |
| 179 | +# mock_get.assert_called_once_with(url) |
55 | 180 |
|
56 | | - # Assert that the chardet.detect function was called with the correct content |
57 | | - mock_detect.assert_called_once_with(content) |
| 181 | +# # Assert that the chardet.detect function was called with the correct content |
| 182 | +# mock_detect.assert_called_once_with(content) |
0 commit comments