Permalink
Browse files

Turn off tracking unless HTTPBIN_TRACKING is set.

Fixes #245.
  • Loading branch information...
1 parent 39009b9 commit 6858420e24673536bcc7401b928e6fadab09ac71 @The-Compiler The-Compiler committed Aug 31, 2015
Showing with 40 additions and 2 deletions.
  1. +1 −0 AUTHORS
  2. +1 −1 httpbin/core.py
  3. +3 −1 httpbin/templates/index.html
  4. +35 −0 test_httpbin.py
View
@@ -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)
View
@@ -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')
@@ -56,7 +56,9 @@
{% include 'httpbin.1.html' %}
-{% include 'trackingscripts.html' %}
+{% if env.get('HTTPBIN_TRACKING', '0') == '1' %}
+ {% include 'trackingscripts.html' %}
+{% endif %}
</body>
</html>
View
@@ -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 @@
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 @@ def test_request_out_of_bounds(self):
)
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()

0 comments on commit 6858420

Please sign in to comment.