Turn off tracking unless HTTPBIN_TRACKING is set.

Fixes #245.
This commit is contained in:
Florian Bruhin
2015-08-31 20:48:25 +02:00
parent 39009b9a8c
commit 6858420e24
4 changed files with 40 additions and 2 deletions
+1
View File
@@ -21,3 +21,4 @@ Patches and Suggestions
- Cory Benfield (Lukasa) <cory@lukasa.co.uk>
- Matt Robenolt (https://github.com/mattrobenolt)
- Dave Challis (https://github.com/davechallis)
- Florian Bruhin (https://github.com/The-Compiler)
+1 -1
View File
@@ -93,7 +93,7 @@ def set_cors_headers(response):
def view_landing_page():
"""Generates Landing Page."""
return render_template('index.html')
return render_template('index.html', env=os.environ)
@app.route('/html')
+3 -1
View File
@@ -56,7 +56,9 @@
{% include 'httpbin.1.html' %}
{% include 'trackingscripts.html' %}
{% if env.get('HTTPBIN_TRACKING', '0') == '1' %}
{% include 'trackingscripts.html' %}
{% endif %}
</body>
</html>
+35
View File
@@ -1,7 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import base64
import unittest
import contextlib
import six
import json
from werkzeug.http import parse_dict_header
@@ -11,6 +13,24 @@ from six import BytesIO
import httpbin
@contextlib.contextmanager
def _setenv(key, value):
"""Context manager to set an environment variable temporarily."""
old_value = os.environ.get(key, None)
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
yield
if old_value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
def _string_to_base64(string):
"""Encodes string to utf-8 and then base64"""
utf8_encoded = string.encode('utf-8')
@@ -419,5 +439,20 @@ class HttpbinTestCase(unittest.TestCase):
)
self.assertEqual(response.status_code, 416)
def test_tracking_disabled(self):
with _setenv('HTTPBIN_TRACKING', None):
response = self.app.get('/')
data = response.data.decode('utf-8')
self.assertNotIn('google-analytics', data)
self.assertNotIn('perfectaudience', data)
def test_tracking_enabled(self):
with _setenv('HTTPBIN_TRACKING', '1'):
response = self.app.get('/')
data = response.data.decode('utf-8')
self.assertIn('google-analytics', data)
self.assertIn('perfectaudience', data)
if __name__ == '__main__':
unittest.main()