import unittest 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 def test_health_check(self): """Check health endpoint.""" response = self.app.get('/health') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, b'healthy') @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 = '' 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'', 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()