1. from datetime import date
    
  2. 
    
  3. from django.test import override_settings
    
  4. 
    
  5. from .base import SitemapTestsBase
    
  6. 
    
  7. 
    
  8. @override_settings(ROOT_URLCONF="sitemaps_tests.urls.https")
    
  9. class HTTPSSitemapTests(SitemapTestsBase):
    
  10.     protocol = "https"
    
  11. 
    
  12.     def test_secure_sitemap_index(self):
    
  13.         "A secure sitemap index can be rendered"
    
  14.         response = self.client.get("/secure/index.xml")
    
  15.         expected_content = """<?xml version="1.0" encoding="UTF-8"?>
    
  16. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    
  17. <sitemap><loc>%s/secure/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
    
  18. </sitemapindex>
    
  19. """ % (
    
  20.             self.base_url,
    
  21.             date.today(),
    
  22.         )
    
  23.         self.assertXMLEqual(response.content.decode(), expected_content)
    
  24. 
    
  25.     def test_secure_sitemap_section(self):
    
  26.         "A secure sitemap section can be rendered"
    
  27.         response = self.client.get("/secure/sitemap-simple.xml")
    
  28.         expected_content = (
    
  29.             '<?xml version="1.0" encoding="UTF-8"?>\n'
    
  30.             '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
    
  31.             'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
    
  32.             "<url><loc>%s/location/</loc><lastmod>%s</lastmod>"
    
  33.             "<changefreq>never</changefreq><priority>0.5</priority></url>\n"
    
  34.             "</urlset>"
    
  35.         ) % (
    
  36.             self.base_url,
    
  37.             date.today(),
    
  38.         )
    
  39.         self.assertXMLEqual(response.content.decode(), expected_content)
    
  40. 
    
  41. 
    
  42. @override_settings(SECURE_PROXY_SSL_HEADER=False)
    
  43. class HTTPSDetectionSitemapTests(SitemapTestsBase):
    
  44.     extra = {"wsgi.url_scheme": "https"}
    
  45. 
    
  46.     def test_sitemap_index_with_https_request(self):
    
  47.         "A sitemap index requested in HTTPS is rendered with HTTPS links"
    
  48.         response = self.client.get("/simple/index.xml", **self.extra)
    
  49.         expected_content = """<?xml version="1.0" encoding="UTF-8"?>
    
  50. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    
  51. <sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
    
  52. </sitemapindex>
    
  53. """ % (
    
  54.             self.base_url.replace("http://", "https://"),
    
  55.             date.today(),
    
  56.         )
    
  57.         self.assertXMLEqual(response.content.decode(), expected_content)
    
  58. 
    
  59.     def test_sitemap_section_with_https_request(self):
    
  60.         "A sitemap section requested in HTTPS is rendered with HTTPS links"
    
  61.         response = self.client.get("/simple/sitemap-simple.xml", **self.extra)
    
  62.         expected_content = (
    
  63.             '<?xml version="1.0" encoding="UTF-8"?>\n'
    
  64.             '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
    
  65.             'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
    
  66.             "<url><loc>%s/location/</loc><lastmod>%s</lastmod>"
    
  67.             "<changefreq>never</changefreq><priority>0.5</priority></url>\n"
    
  68.             "</urlset>"
    
  69.         ) % (
    
  70.             self.base_url.replace("http://", "https://"),
    
  71.             date.today(),
    
  72.         )
    
  73.         self.assertXMLEqual(response.content.decode(), expected_content)