53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import unittest
|
|
from unittest.mock import patch, MagicMock
|
|
from proxy import create_app
|
|
|
|
|
|
class FlaskTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
"""Set up for tests: create a test client."""
|
|
app = create_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 = '<?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 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()
|