Skip to content

Instantly share code, notes, and snippets.

@davidfischer
Created August 15, 2018 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfischer/06244f91791088a5847f43888eb683a9 to your computer and use it in GitHub Desktop.
Save davidfischer/06244f91791088a5847f43888eb683a9 to your computer and use it in GitHub Desktop.
Test Read the Docs HTTPS redirects
from __future__ import print_function, unicode_literals
import requests
from requests.exceptions import RequestException
def request_url(url):
try:
return requests.get(url)
except RequestException as e:
print('- {exc} exception accessing {url}'.format(exc=e.message, url=url))
if e.response:
return e.response
return None
def check_redirects(slug, urls, expected):
print('Checking {slug} redirects...'.format(slug=slug))
success = True
for url in urls:
resp = request_url(url)
if not resp:
success = False
continue
if not resp.ok or resp.url != expected:
success = False
print('- [ERROR] URL {url} redirected to {dest_url} (status={status})'.format(
url=url,
dest_url=resp.url,
status=resp.status_code,
))
if success:
print('- [SUCCESS] {slug} redirects look good'.format(slug=slug))
####################################
# Check readthedocs.io redirects
####################################
expected = 'https://docs.readthedocs.io/en/latest/'
urls = [
'http://docs.readthedocs.io/en/latest/',
'http://docs.readthedocs.io/en/latest',
'http://docs.readthedocs.io',
]
check_redirects('readthedocs.io', urls, expected)
####################################
# Check rtfd.io redirects
####################################
expected = 'https://docs.readthedocs.io/en/latest/'
urls = [
'http://docs.rtfd.io',
'https://docs.rtfd.io',
'http://docs.rtfd.io/',
'https://docs.rtfd.io/',
'http://docs.rtfd.io/en/latest',
'http://docs.rtfd.io/en/latest/',
]
check_redirects('rtfd.io', urls, expected)
####################################
# Check rtfd.org redirects
####################################
expected = 'https://docs.readthedocs.io/en/latest/'
urls = [
'http://docs.rtfd.org',
'https://docs.rtfd.org',
'http://docs.rtfd.org/',
'https://docs.rtfd.org/',
'http://docs.rtfd.org/en/latest',
'http://docs.rtfd.org/en/latest/',
]
check_redirects('rtfd.org', urls, expected)
####################################
# Check custom domain redirects
####################################
domain = 'docs.phpmyadmin.net'
expected = 'https://{domain}/en/latest/'.format(domain=domain)
urls = [
'http://{domain}'.format(domain=domain),
'https://{domain}'.format(domain=domain),
'http://{domain}/'.format(domain=domain),
'https://{domain}/'.format(domain=domain),
'http://{domain}/en/latest'.format(domain=domain),
'http://{domain}/en/latest/'.format(domain=domain),
]
check_redirects('custom domain', urls, expected)
@davidfischer
Copy link
Author

As of today, we are not yet redirecting custom domains and we don't have a valid cert on rtfd.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment