This commit is contained in:
2025-03-03 02:54:33 +03:00
parent 528bb601c5
commit 0898d92fc7
5 changed files with 79 additions and 40 deletions

View File

@@ -1,11 +1,12 @@
import unittest
from proxy import app
from unittest.mock import patch, MagicMock
from proxy import proxy_app
class FlaskTestCase(unittest.TestCase):
def setUp(self):
"""Set up for tests: create a test client."""
app = proxy_app()
self.app = app.test_client()
self.app.testing = True
@@ -13,12 +14,37 @@ class FlaskTestCase(unittest.TestCase):
"""Check health endpoint."""
response = self.app.get('/health')
self.assertEqual(response.status_code, 200)
self.assertIn(b'healthy', response.data)
self.assertEqual(response.data, b'healthy')
def test_proxy(self):
"""Test RSS proxying."""
# Sample URL for proxying
url = 'http://example.com/rss'
response = self.app.get(f'/proxy?url={url}')
@patch("proxy.rss_proxy.requests.get")
def test_proxy_success(self, mock_get):
"""Test successful RSS proxying."""
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.headers = {"content-type": "text/xml; charset=windows-1251"}
mock_response.text = '<?xml version="1.0" encoding="windows-1251"?><rss></rss>'
mock_response.apparent_encoding = "windows-1251"
mock_get.return_value = mock_response
response = self.app.get('/proxy?url=https://example.com/rss.xml')
self.assertEqual(response.status_code, 200)
self.assertIn(b'<?xml', response.data) # Check for XML response
self.assertIn(b'<?xml version="1.0" encoding="UTF-8"?>', response.data) # Проверка перекодировки
@patch("proxy.rss_proxy.requests.get")
def test_proxy_missing_url(self, mock_get):
"""Test proxy request without URL parameter."""
response = self.app.get('/proxy')
self.assertEqual(response.status_code, 400)
self.assertIn(b'Missing URL', response.data)
@patch("proxy.rss_proxy.requests.get")
def test_proxy_request_failure(self, mock_get):
"""Test RSS proxy failure when request fails."""
mock_get.side_effect = Exception("Request failed")
response = self.app.get('/proxy?url=https://example.com/rss.xml')
self.assertEqual(response.status_code, 500)
self.assertIn(b'Error: Request failed', response.data)
if __name__ == '__main__':
unittest.main()