2010-01-28 21:46:18 +08:00
|
|
|
import datetime
|
2013-07-02 04:49:07 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
from django.test import SimpleTestCase
|
2013-07-02 04:49:07 +08:00
|
|
|
from django.utils import feedgenerator
|
2022-03-23 19:15:36 +08:00
|
|
|
from django.utils.timezone import get_fixed_timezone
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class FeedgeneratorTests(SimpleTestCase):
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
Tests for the low-level syndication feed framework.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_get_tag_uri(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
get_tag_uri() correctly generates TagURIs.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
feedgenerator.get_tag_uri(
|
|
|
|
"http://example.org/foo/bar#headline", datetime.date(2004, 10, 25)
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
"tag:example.org,2004-10-25:/foo/bar/headline",
|
|
|
|
)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
def test_get_tag_uri_with_port(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
get_tag_uri() correctly generates TagURIs from URLs with port numbers.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
feedgenerator.get_tag_uri(
|
|
|
|
"http://www.example.org:8000/2008/11/14/django#headline",
|
|
|
|
datetime.datetime(2008, 11, 14, 13, 37, 0),
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
"tag:www.example.org,2008-11-14:/2008/11/14/django/headline",
|
|
|
|
)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
def test_rfc2822_date(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
rfc2822_date() correctly formats datetime objects.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
feedgenerator.rfc2822_date(datetime.datetime(2008, 11, 14, 13, 37, 0)),
|
|
|
|
"Fri, 14 Nov 2008 13:37:00 -0000",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_rfc2822_date_with_timezone(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
rfc2822_date() correctly formats datetime objects with tzinfo.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
2013-09-08 16:43:33 +08:00
|
|
|
feedgenerator.rfc2822_date(
|
|
|
|
datetime.datetime(
|
|
|
|
2008, 11, 14, 13, 37, 0, tzinfo=get_fixed_timezone(60)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-09-08 16:43:33 +08:00
|
|
|
),
|
2010-01-28 21:46:18 +08:00
|
|
|
"Fri, 14 Nov 2008 13:37:00 +0100",
|
|
|
|
)
|
|
|
|
|
2011-10-15 18:16:24 +08:00
|
|
|
def test_rfc2822_date_without_time(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
rfc2822_date() correctly formats date objects.
|
2011-10-15 18:16:24 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
feedgenerator.rfc2822_date(datetime.date(2008, 11, 14)),
|
|
|
|
"Fri, 14 Nov 2008 00:00:00 -0000",
|
|
|
|
)
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def test_rfc3339_date(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
rfc3339_date() correctly formats datetime objects.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
feedgenerator.rfc3339_date(datetime.datetime(2008, 11, 14, 13, 37, 0)),
|
|
|
|
"2008-11-14T13:37:00Z",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_rfc3339_date_with_timezone(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
rfc3339_date() correctly formats datetime objects with tzinfo.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
2013-09-08 16:43:33 +08:00
|
|
|
feedgenerator.rfc3339_date(
|
|
|
|
datetime.datetime(
|
|
|
|
2008, 11, 14, 13, 37, 0, tzinfo=get_fixed_timezone(120)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-09-08 16:43:33 +08:00
|
|
|
),
|
2010-01-28 21:46:18 +08:00
|
|
|
"2008-11-14T13:37:00+02:00",
|
|
|
|
)
|
|
|
|
|
2011-10-15 18:16:24 +08:00
|
|
|
def test_rfc3339_date_without_time(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
rfc3339_date() correctly formats date objects.
|
2011-10-15 18:16:24 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
feedgenerator.rfc3339_date(datetime.date(2008, 11, 14)),
|
|
|
|
"2008-11-14T00:00:00Z",
|
|
|
|
)
|
|
|
|
|
2011-02-13 03:12:14 +08:00
|
|
|
def test_atom1_mime_type(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Atom MIME type has UTF8 Charset parameter set
|
2011-02-13 03:12:14 +08:00
|
|
|
"""
|
|
|
|
atom_feed = feedgenerator.Atom1Feed("title", "link", "description")
|
2015-06-04 22:00:39 +08:00
|
|
|
self.assertEqual(atom_feed.content_type, "application/atom+xml; charset=utf-8")
|
2011-02-13 03:12:14 +08:00
|
|
|
|
2012-02-11 07:45:22 +08:00
|
|
|
def test_rss_mime_type(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
RSS MIME type has UTF8 Charset parameter set
|
2012-02-11 07:45:22 +08:00
|
|
|
"""
|
|
|
|
rss_feed = feedgenerator.Rss201rev2Feed("title", "link", "description")
|
2015-06-04 22:00:39 +08:00
|
|
|
self.assertEqual(rss_feed.content_type, "application/rss+xml; charset=utf-8")
|
2012-02-11 07:45:22 +08:00
|
|
|
|
2011-10-23 23:19:09 +08:00
|
|
|
# Two regression tests for #14202
|
|
|
|
|
|
|
|
def test_feed_without_feed_url_gets_rendered_without_atom_link(self):
|
|
|
|
feed = feedgenerator.Rss201rev2Feed("title", "/link/", "descr")
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(feed.feed["feed_url"])
|
2011-10-23 23:19:09 +08:00
|
|
|
feed_content = feed.writeString("utf-8")
|
2012-10-27 04:40:35 +08:00
|
|
|
self.assertNotIn("<atom:link", feed_content)
|
|
|
|
self.assertNotIn('href="/feed/"', feed_content)
|
|
|
|
self.assertNotIn('rel="self"', feed_content)
|
2011-10-23 23:19:09 +08:00
|
|
|
|
|
|
|
def test_feed_with_feed_url_gets_rendered_with_atom_link(self):
|
|
|
|
feed = feedgenerator.Rss201rev2Feed(
|
|
|
|
"title", "/link/", "descr", feed_url="/feed/"
|
|
|
|
)
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(feed.feed["feed_url"], "/feed/")
|
2011-10-23 23:19:09 +08:00
|
|
|
feed_content = feed.writeString("utf-8")
|
2012-10-27 04:40:35 +08:00
|
|
|
self.assertIn("<atom:link", feed_content)
|
|
|
|
self.assertIn('href="/feed/"', feed_content)
|
|
|
|
self.assertIn('rel="self"', feed_content)
|
2016-05-25 00:50:20 +08:00
|
|
|
|
2017-02-24 22:46:31 +08:00
|
|
|
def test_atom_add_item(self):
|
|
|
|
# Not providing any optional arguments to Atom1Feed.add_item()
|
|
|
|
feed = feedgenerator.Atom1Feed("title", "/link/", "descr")
|
|
|
|
feed.add_item("item_title", "item_link", "item_description")
|
|
|
|
feed.writeString("utf-8")
|
|
|
|
|
2017-02-10 22:29:34 +08:00
|
|
|
def test_deterministic_attribute_order(self):
|
|
|
|
feed = feedgenerator.Atom1Feed("title", "/link/", "desc")
|
|
|
|
feed_content = feed.writeString("utf-8")
|
|
|
|
self.assertIn('href="/link/" rel="alternate"', feed_content)
|
|
|
|
|
2016-05-25 00:50:20 +08:00
|
|
|
def test_latest_post_date_returns_utc_time(self):
|
|
|
|
for use_tz in (True, False):
|
|
|
|
with self.settings(USE_TZ=use_tz):
|
|
|
|
rss_feed = feedgenerator.Rss201rev2Feed("title", "link", "description")
|
2022-03-23 19:15:36 +08:00
|
|
|
self.assertEqual(
|
|
|
|
rss_feed.latest_post_date().tzinfo,
|
|
|
|
datetime.timezone.utc,
|
|
|
|
)
|