Skip to content

Instantly share code, notes, and snippets.

@leonsmith
Last active January 3, 2016 14:19
Show Gist options
  • Save leonsmith/75ed9c221fde3bf17d4f to your computer and use it in GitHub Desktop.
Save leonsmith/75ed9c221fde3bf17d4f to your computer and use it in GitHub Desktop.
Black Box Tester
#!/usr/bin/env python
import requests
import unittest
import html5lib
from cssselect import HTMLTranslator
from unittest.util import safe_repr
class RequestsTestCase(unittest.TestCase):
def setUp(self):
super(RequestsTestCase, self).setUp()
self.session = requests.Session()
def assertResponseIsOk(self, response, msg=None):
self.assertTrue(response.ok, msg=msg)
def assertResponseRedirectsTo(self, response, expected, msg=None):
self.assertTrue(response.history, 'No redirection')
self._baseAssertEqual(response.url, expected, msg=msg)
def assertResponseMaxRedirects(self, response, maximum, msg=None):
standard_msg = 'Exceeded maximum number of redirects (%s/%s)' \
% (safe_repr(len(response.history)), safe_repr(maximum))
msg = self._formatMessage(msg, standard_msg)
self.assertLessEqual(maximum, len(response.history), msg=msg)
def assertResponseHeadersContain(self, response, header, msg=None):
self.assertIn(header, response.headers, msg=msg)
def assertResponseHeaderEquals(self, response, header, value, msg=None):
self.assertResponseHeadersContain(response, header, msg=msg)
self.assertEqual(response.headers.get(header, ''), value, msg=msg)
def assertResponseContains(self, response, text, msg=None):
self.assertIn(text, response.text, msg=msg)
class TestRedirects(RequestsTestCase):
def testHTTPToHTTPS(self):
response = self.session.get('http://www.reviewo.com/')
self.assertResponseIsOk(response)
self.assertResponseRedirectsTo(response, 'https://reviewo.com/')
self.assertResponseMaxRedirects(response, 1)
response = self.session.get('http://reviewo.com/')
self.assertResponseIsOk(response)
self.assertResponseRedirectsTo(response, 'https://reviewo.com/')
self.assertResponseMaxRedirects(response, 1)
def testCanonicalRedirect(self):
response = self.session.get('https://www.reviewo.com/')
self.assertResponseIsOk(response)
self.assertResponseRedirectsTo(response, 'https://reviewo.com/')
self.assertResponseMaxRedirects(response, 1)
class TestStaticFiles(RequestsTestCase):
def testRobotsTxt(self):
response = self.session.get('https://reviewo.com/robots.txt')
self.assertResponseIsOk(response)
self.assertResponseHeaderEquals(response, 'content-type', 'text/plain')
self.assertResponseContains(response, 'Sitemap: https://reviewo.com/sitemap/index.xml')
def testSitemap(self):
response = self.session.get('https://reviewo.com/sitemap/index.xml')
self.assertResponseIsOk(response)
self.assertResponseHeaderEquals(response, 'content-type', 'text/xml')
self.assertResponseContains(response, '<sitemap>')
self.assertResponseContains(response, '<loc>')
self.assertResponseContains(response, '</loc>')
self.assertResponseContains(response, '</sitemapindex>')
class TestHeaders(RequestsTestCase):
def setUp(self):
super(TestHeaders, self).setUp()
self.response = self.session.get('https://reviewo.com')
def testGZipEnabled(self):
self.assertResponseHeaderEquals(self.response, 'content-encoding', 'gzip')
def testStrictTransportSecurityEnabled(self):
self.assertResponseHeadersContain(self.response, 'strict-transport-security')
def testXFramePresent(self):
self.assertResponseHeadersContain(self.response, 'x-frame-options')
class TestMinification(RequestsTestCase):
def setUp(self):
super(TestMinification, self).setUp()
self.response = self.session.get('https://reviewo.com')
self.document = html5lib.parse(self.response.text, treebuilder="lxml", namespaceHTMLElements=False)
def testJsMinification(self):
selector = HTMLTranslator().css_to_xpath('script[src]')
for script in self.document.xpath(selector):
if script.attrib['src'].startswith('/static/compressed/'):
return
self.fail("No minified javascript found")
def testCssMinification(self):
selector = HTMLTranslator().css_to_xpath('link[rel="stylesheet"][href]')
for css in self.document.xpath(selector):
if css.attrib['href'].startswith('/static/compressed/'):
return
self.fail("No minified css found")
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment