summaryrefslogtreecommitdiff
path: root/webtest
diff options
context:
space:
mode:
authorGael Pasgrimaud <gael@gawel.org>2014-04-16 20:32:11 +0200
committerGael Pasgrimaud <gael@gawel.org>2014-04-16 20:34:04 +0200
commitb31cdebb5655b6e2fe0620d807c2d85f5b0f6c18 (patch)
treee84f68d9c8caaeb9db3f374b30bf49fc5c190480 /webtest
parentbc5b86cc8c2feef77cd852efe65527148b241a14 (diff)
downloadwebtest-b31cdebb5655b6e2fe0620d807c2d85f5b0f6c18.tar.gz
add authorization property
Diffstat (limited to 'webtest')
-rw-r--r--webtest/app.py37
-rw-r--r--webtest/debugapp.py2
2 files changed, 38 insertions, 1 deletions
diff --git a/webtest/app.py b/webtest/app.py
index 6acdde1..3a21643 100644
--- a/webtest/app.py
+++ b/webtest/app.py
@@ -17,6 +17,8 @@ import random
import re
import warnings
+from base64 import b64encode
+
from six import StringIO
from six import BytesIO
from six import string_types
@@ -146,6 +148,41 @@ class TestApp(object):
if parser_features:
self.RequestClass.ResponseClass.parser_features = parser_features
+ def get_authorization(self):
+ """Allow to set the HTTP_AUTHORIZATION environ key. Value should looks
+ like ``('Basic', ('user', 'password'))``
+
+ If value is None the the HTTP_AUTHORIZATION is removed
+ """
+ return self.authorization_value
+
+ def set_authorization(self, value):
+ self.authorization_value = value
+ if value is not None:
+ invalid_value = (
+ "You should use a value like ('Basic', ('user', 'password'))"
+ )
+ if isinstance(value, (list, tuple)) and len(value) == 2:
+ authtype, val = value
+ if authtype == 'Basic' and val and \
+ isinstance(val, (list, tuple)):
+ val = ':'.join(list(val))
+ val = b64encode(to_bytes(val)).strip()
+ val = val.decode('latin1')
+ else:
+ raise ValueError(invalid_value)
+ value = str('%s %s' % (authtype, val))
+ else:
+ raise ValueError(invalid_value)
+ self.extra_environ.update({
+ 'HTTP_AUTHORIZATION': value,
+ })
+ else:
+ if 'HTTP_AUTHORIZATION' in self.extra_environ:
+ del self.extra_environ['HTTP_AUTHORIZATION']
+
+ authorization = property(get_authorization, set_authorization)
+
@property
def cookies(self):
return dict([(cookie.name, cookie.value) for cookie in self.cookiejar])
diff --git a/webtest/debugapp.py b/webtest/debugapp.py
index 970f31c..60b4435 100644
--- a/webtest/debugapp.py
+++ b/webtest/debugapp.py
@@ -11,7 +11,7 @@ class DebugApp(object):
"""The WSGI application used for testing"""
def __init__(self, form=None, show_form=False):
- if os.path.isfile(form):
+ if form and os.path.isfile(form):
fd = open(form, 'rb')
self.form = fd.read()
fd.close()