Skip to content

Commit

Permalink
Add tests to test User-Agent headers in request
Browse files Browse the repository at this point in the history
  • Loading branch information
ogajduse committed Dec 12, 2023
1 parent 09f7ded commit 9e641d4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion custom_components/feedparser/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
DEFAULT_SCAN_INTERVAL = timedelta(hours=1)
DEFAULT_THUMBNAIL = "https://www.home-assistant.io/images/favicon-192x192-full.png"
DEFAULT_TOPN = 9999
USER_AGENT = f"Home Assistant Feedparser Integration {__version__}"
USER_AGENT = f"Home Assistant Feed-parser Integration {__version__}"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ dev = [
"homeassistant-stubs",
"pytest==7.4.3",
"mypy",
"requests",
"requests-file",
"ruff",
"types-python-dateutil",
"types-PyYAML",
"types-requests",
"voluptuous-stubs",
"pyyaml",

Expand Down
11 changes: 11 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,14 @@
DEFAULT_EXCLUSIONS: list[str] = []
DEFAULT_INCLUSIONS = ["image", "title", "link", "published"]
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S UTC%z"

URLS_HEADERS_REQUIRED = [
{
"name": "elcomercio_gijon",
"url": "https://www.elcomercio.es/rss/2.0/?section=gijon",
},
{
"name": "nasdaq_options",
"url": "https://www.nasdaq.com/feed/rssoutbound?category=Options",
},
]
23 changes: 22 additions & 1 deletion tests/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import feedparser
import pytest
from constants import DATE_FORMAT
from constants import DATE_FORMAT, URLS_HEADERS_REQUIRED
from feedsource import FeedSource

from custom_components.feedparser.sensor import (
Expand Down Expand Up @@ -157,3 +157,24 @@ def test_check_duplicates(feed_sensor: FeedParserSensor) -> None:
feed_sensor.update()
after_second_update = len(feed_sensor.feed_entries)
assert after_first_update == after_second_update


@pytest.mark.parametrize(
"online_feed",
URLS_HEADERS_REQUIRED,
ids=lambda feed_url: feed_url["name"],
)
def test_fetch_data_headers_required(online_feed: dict) -> None:
"""Test fetching feed from remote server that requires request with headers."""
feed_sensor = FeedParserSensor(
feed=online_feed["url"],
name=online_feed["name"],
date_format=DATE_FORMAT,
local_time=False,
show_topn=9999,
inclusions=["image", "title", "link", "published"],
exclusions=[],
scan_interval=DEFAULT_SCAN_INTERVAL,
)
feed_sensor.update()
assert feed_sensor.feed_entries

0 comments on commit 9e641d4

Please sign in to comment.