From 6858420e24673536bcc7401b928e6fadab09ac71 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 31 Aug 2015 20:48:25 +0200 Subject: [PATCH] Turn off tracking unless HTTPBIN_TRACKING is set. Fixes #245. --- AUTHORS | 1 + httpbin/core.py | 2 +- httpbin/templates/index.html | 4 +++- test_httpbin.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index a4561f8..18fe967 100644 --- a/AUTHORS +++ b/AUTHORS @@ -21,3 +21,4 @@ Patches and Suggestions - Cory Benfield (Lukasa) - Matt Robenolt (https://github.com/mattrobenolt) - Dave Challis (https://github.com/davechallis) +- Florian Bruhin (https://github.com/The-Compiler) diff --git a/httpbin/core.py b/httpbin/core.py index efbb800..1c85e81 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -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') diff --git a/httpbin/templates/index.html b/httpbin/templates/index.html index daea37a..f63c749 100644 --- a/httpbin/templates/index.html +++ b/httpbin/templates/index.html @@ -56,7 +56,9 @@ {% include 'httpbin.1.html' %} -{% include 'trackingscripts.html' %} +{% if env.get('HTTPBIN_TRACKING', '0') == '1' %} + {% include 'trackingscripts.html' %} +{% endif %} diff --git a/test_httpbin.py b/test_httpbin.py index 95e366d..9893308 100755 --- a/test_httpbin.py +++ b/test_httpbin.py @@ -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()