def rss_proxy_handler(request: WerkzeugRequest) -> WerkzeugResponse: try: data = json.loads(request.data.decode('utf-8')) assert data['token'] == _RSS_PROXY_TOKEN assert data.get('method') in (None, 'GET', 'POST') url = urlparse(data['url']) query = _parse_query(url.query) assert url.path == '/not-proxy' assert HTTPHeaders(data['headers'])['user-agent'] except Exception as ex: LOG.warning(ex, exc_info=ex) msg = traceback.format_exception_only(type(ex), ex) return WerkzeugResponse(msg, status=400) status = query.get('status') error = query.get('error') if error: if error == 'ERROR': headers = {'x-rss-proxy-status': 'ERROR'} return WerkzeugResponse(str(status), status=200, headers=headers) else: return WerkzeugResponse(str(status), status=int(error)) else: status = int(status) if status else 200 headers = {'x-rss-proxy-status': status} return WerkzeugResponse(str(status), status=200, headers=headers)
def test_read_status(reader_class: Type[FeedReader], httpserver: HTTPServer, status: int): options = dict(allow_non_webpage=True, allow_private_address=True) local_resp = WerkzeugResponse(str(status), status=status) httpserver.expect_request("/status").respond_with_response(local_resp) url = httpserver.url_for("/status") with reader_class(**options) as reader: response = reader.read(url) assert response.status == status assert response.content == str(status).encode()
def test_read_non_webpage(reader_class: Type[FeedReader], httpserver: HTTPServer, mime_type: str): options = dict(allow_private_address=True) local_resp = WerkzeugResponse(b'xxxxxxxx', mimetype=mime_type) httpserver.expect_request("/non-webpage").respond_with_response(local_resp) url = httpserver.url_for("/non-webpage") with reader_class(**options) as reader: response = reader.read(url) assert response.status == FeedResponseStatus.CONTENT_TYPE_NOT_SUPPORT_ERROR assert not response.content
def test_read_testdata(reader_class: Type[FeedReader], httpserver: HTTPServer, filepath: str): filepath = _data_dir / filepath content = filepath.read_bytes() urls = [] for i, x in enumerate(_collect_header_cases()): local_resp = WerkzeugResponse(content, content_type=x) httpserver.expect_request(f"/testdata/{i}").respond_with_response(local_resp) urls.append(httpserver.url_for(f"/testdata/{i}")) options = dict(allow_private_address=True) with reader_class(**options) as reader: for url in urls: response = reader.read(url) assert response.ok assert response.content == content assert response.encoding assert response.feed_type