test_https.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from __future__ import unicode_literals
  2. from datetime import date
  3. from django.test import ignore_warnings, override_settings
  4. from django.utils.deprecation import RemovedInDjango20Warning
  5. from .base import SitemapTestsBase
  6. @override_settings(ROOT_URLCONF='sitemaps_tests.urls.https')
  7. class HTTPSSitemapTests(SitemapTestsBase):
  8. protocol = 'https'
  9. @ignore_warnings(category=RemovedInDjango20Warning)
  10. def test_secure_sitemap_index(self):
  11. "A secure sitemap index can be rendered"
  12. # The URL for views.sitemap in tests/urls/https.py has been updated
  13. # with a name but since reversing by Python path is tried first
  14. # before reversing by name and works since we're giving
  15. # name='django.contrib.sitemaps.views.sitemap', we need to silence
  16. # the erroneous warning until reversing by dotted path is removed.
  17. # The test will work without modification when it's removed.
  18. response = self.client.get('/secure/index.xml')
  19. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  20. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  21. <sitemap><loc>%s/secure/sitemap-simple.xml</loc></sitemap>
  22. </sitemapindex>
  23. """ % self.base_url
  24. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  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 = """<?xml version="1.0" encoding="UTF-8"?>
  29. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  30. <url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  31. </urlset>
  32. """ % (self.base_url, date.today())
  33. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  34. @override_settings(SECURE_PROXY_SSL_HEADER=False)
  35. class HTTPSDetectionSitemapTests(SitemapTestsBase):
  36. extra = {'wsgi.url_scheme': 'https'}
  37. @ignore_warnings(category=RemovedInDjango20Warning)
  38. def test_sitemap_index_with_https_request(self):
  39. "A sitemap index requested in HTTPS is rendered with HTTPS links"
  40. # The URL for views.sitemap in tests/urls/https.py has been updated
  41. # with a name but since reversing by Python path is tried first
  42. # before reversing by name and works since we're giving
  43. # name='django.contrib.sitemaps.views.sitemap', we need to silence
  44. # the erroneous warning until reversing by dotted path is removed.
  45. # The test will work without modification when it's removed.
  46. response = self.client.get('/simple/index.xml', **self.extra)
  47. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  48. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  49. <sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
  50. </sitemapindex>
  51. """ % self.base_url.replace('http://', 'https://')
  52. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  53. def test_sitemap_section_with_https_request(self):
  54. "A sitemap section requested in HTTPS is rendered with HTTPS links"
  55. response = self.client.get('/simple/sitemap-simple.xml', **self.extra)
  56. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  57. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  58. <url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  59. </urlset>
  60. """ % (self.base_url.replace('http://', 'https://'), date.today())
  61. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)