diff --git a/tests/sitemaps_tests/test_http.py b/tests/sitemaps_tests/test_http.py index 3a820e140f..af787ff41f 100644 --- a/tests/sitemaps_tests/test_http.py +++ b/tests/sitemaps_tests/test_http.py @@ -352,3 +352,48 @@ class HTTPSitemapTests(SitemapTestsBase): """ self.assertXMLEqual(response.content.decode(), expected_content) + + def test_callable_sitemod_partial(self): + """ + Not all items have `lastmod`. Therefore the `Last-Modified` header + is not set by the detail sitemap view. + """ + index_response = self.client.get('/callable-lastmod-partial/index.xml') + sitemap_response = self.client.get('/callable-lastmod-partial/sitemap.xml') + self.assertNotIn('Last-Modified', index_response) + self.assertNotIn('Last-Modified', sitemap_response) + expected_content_index = """ + + http://example.com/simple/sitemap-callable-lastmod.xml + + """ + expected_content_sitemap = """ + + http://example.com/location/2013-03-13http://example.com/location/ + + """ + self.assertXMLEqual(index_response.content.decode(), expected_content_index) + self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap) + + def test_callable_sitemod_full(self): + """ + All items in the sitemap have `lastmod`. The `Last-Modified` header + is set for the detail sitemap view. The index view does not (currently) + set the `Last-Modified` header. + """ + index_response = self.client.get('/callable-lastmod-full/index.xml') + sitemap_response = self.client.get('/callable-lastmod-full/sitemap.xml') + self.assertNotIn('Last-Modified', index_response) + self.assertEqual(sitemap_response.headers['Last-Modified'], 'Thu, 13 Mar 2014 10:00:00 GMT') + expected_content_index = """ + + http://example.com/simple/sitemap-callable-lastmod.xml + + """ + expected_content_sitemap = """ + + http://example.com/location/2013-03-13http://example.com/location/2014-03-13 + + """ + self.assertXMLEqual(index_response.content.decode(), expected_content_index) + self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap) diff --git a/tests/sitemaps_tests/urls/http.py b/tests/sitemaps_tests/urls/http.py index e4cba4c42f..936f346e41 100644 --- a/tests/sitemaps_tests/urls/http.py +++ b/tests/sitemaps_tests/urls/http.py @@ -81,6 +81,35 @@ class TimezoneSiteMap(SimpleSitemap): lastmod = datetime(2013, 3, 13, 10, 0, 0, tzinfo=timezone.get_fixed_timezone(-300)) +class CallableLastmodPartialSitemap(Sitemap): + """Not all items have `lastmod`.""" + location = '/location/' + + def items(self): + o1 = TestModel() + o1.lastmod = datetime(2013, 3, 13, 10, 0, 0) + o2 = TestModel() + return [o1, o2] + + def lastmod(self, obj): + return obj.lastmod + + +class CallableLastmodFullSitemap(Sitemap): + """All items have `lastmod`.""" + location = '/location/' + + def items(self): + o1 = TestModel() + o1.lastmod = datetime(2013, 3, 13, 10, 0, 0) + o2 = TestModel() + o2.lastmod = datetime(2014, 3, 13, 10, 0, 0) + return [o1, o2] + + def lastmod(self, obj): + return obj.lastmod + + def testmodelview(request, id): return HttpResponse() @@ -158,6 +187,14 @@ generic_sitemaps_lastmod = { }), } +callable_lastmod_partial_sitemap = { + 'callable-lastmod': CallableLastmodPartialSitemap, +} + +callable_lastmod_full_sitemap = { + 'callable-lastmod': CallableLastmodFullSitemap, +} + urlpatterns = [ path('simple/index.xml', views.index, {'sitemaps': simple_sitemaps}), path('simple-paged/index.xml', views.index, {'sitemaps': simple_sitemaps_paged}), @@ -246,6 +283,10 @@ urlpatterns = [ path( 'sitemap-without-entries/sitemap.xml', views.sitemap, {'sitemaps': {}}, name='django.contrib.sitemaps.views.sitemap'), + path('callable-lastmod-partial/index.xml', views.index, {'sitemaps': callable_lastmod_partial_sitemap}), + path('callable-lastmod-partial/sitemap.xml', views.sitemap, {'sitemaps': callable_lastmod_partial_sitemap}), + path('callable-lastmod-full/index.xml', views.index, {'sitemaps': callable_lastmod_full_sitemap}), + path('callable-lastmod-full/sitemap.xml', views.sitemap, {'sitemaps': callable_lastmod_full_sitemap}), ] urlpatterns += i18n_patterns(