test_https.py 2.6 KB

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