Skip to content

Instantly share code, notes, and snippets.

@dwightgunning
Created August 25, 2017 14:33
Show Gist options
  • Save dwightgunning/b0cd3b95a88981a6c93eb46c33e470ce to your computer and use it in GitHub Desktop.
Save dwightgunning/b0cd3b95a88981a6c93eb46c33e470ce to your computer and use it in GitHub Desktop.
Tests for the (sub)-domain redirects set on the Stream blog - https://getstream.io/blog/
"""Tests for the (sub)-domain redirects set on the Stream blog.
See: https://getstream.io/blog/
Runs with Python 2.7 and 3.6
Dependencies:
- requests==2.18.4
Copyright (c) 2017 Dwight Gunning and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import time
import unittest
import requests
def concat_request_url(protocol, host, path=''):
"""Concatenate the three commonly referenced URL fragments """
return '%s%s%s' % (protocol, host, path)
class StreamBlogRedirectsTest(unittest.TestCase):
"""Test Blog Redirects (https://getstream.io/blog/) """
protocols = {
'http': 'http://',
'https': 'https://'
}
hosts = {
'blog': 'blog.getstream.io',
'www': 'www.getstream.io',
}
def test_robots(self):
"""robots.txt on blog.getstream.io
http://blog.getstream.io/robots.txt
https://blog.getstream.io/robots.txt
--> HTTP 200
"""
path = '/robots.txt'
for protocol in self.protocols.values():
time.sleep(1) # Let's not flood the network
host = self.hosts['blog']
request_url = concat_request_url(protocol, host, path)
response = requests.head(request_url, allow_redirects=True)
self.verify_response(response, request_url)
def test_subdomain_redirects_blog(self):
"""URLs on blog.getstream.io host
http://blog.getstream.io
https://blog.getstream.io
http://blog.getstream.io/
https://blog.getstream.io/
--> HTTP 301 https://getstream.io/blog/
"""
host = self.hosts['blog']
expected_url = 'https://getstream.io/blog/'
for protocol in self.protocols.values():
for path in ['', '/']:
time.sleep(1) # Let's not flood the network
request_url = concat_request_url(protocol, host, path)
response = requests.head(request_url, allow_redirects=True)
self.verify_response(response, expected_url, True)
def test_subdomain_redirects_path(self):
"""URLs on blog.getstream.io/blog with paths
http://blog.getstream.io/blog/category/case-study
https://blog.getstream.io/blog/category/case-study
http://blog.getstream.io/blog/category/case-study/
https://blog.getstream.io/blog/category/case-study/
--> HTTP 301 https://getstream.io/blog/category/case-study/
"""
host = self.hosts['blog']
expected_url = 'https://getstream.io/blog/category/case-study/'
for protocol in self.protocols.values():
for path in ['/category/case-study', '/category/case-study/']:
time.sleep(1) # Let's not flood the network
request_url = concat_request_url(protocol, host, path)
response = requests.head(request_url, allow_redirects=True)
self.verify_response(response, expected_url, True)
def test_www_redirects(self):
"""URLs on www.getstream.io host with /blog root url
http://www.getstream.io/blog
https://www.getstream.io/blog
http://www.getstream.io/blog/
https://www.getstream.io/blog/
--> HTTP 301 https://getstream.io/blog/
"""
host = self.hosts['www']
expected_url = 'https://getstream.io/blog/'
for protocol in self.protocols.values():
for path in ['/blog', '/blog/']:
time.sleep(1) # Let's not flood the network
request_url = concat_request_url(protocol, host, path)
response = requests.head(request_url, allow_redirects=True)
self.verify_response(response, expected_url, True)
def test_www_redirects_blog_path(self):
"""URLs on www.getstream.io/blog/category/case-study with path
http://www.getstream.io/blog/category/case-study
https://www.getstream.io/blog/category/case-study
http://www.getstream.io/blog/category/case-study/
https://www.getstream.io/blog/category/case-study/
--> HTTP 301 HTTP 301 https://getstream.io/blog/category/case-study/
"""
host = self.hosts['www']
expected_url = 'https://getstream.io/blog/category/case-study/'
for protocol in self.protocols.values():
for path in ['/blog/category/case-study',
'/blog/category/case-study/']:
time.sleep(1) # Let's not flood the network
request_url = concat_request_url(protocol, host, path)
response = requests.head(request_url, allow_redirects=True)
self.verify_response(response, expected_url, True)
def verify_response(self, response, expected_url, expected_redirect=False):
"""Verify responses all result in 200 OK, the expected final URL and
potentially a redirect.
"""
self.failUnlessEqual(response.status_code, 200)
self.failUnlessEqual(response.url, expected_url)
self.failUnlessEqual(len(response.history),
1 if expected_redirect else 0,
[request.headers for request in response.history])
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment