From 634214c49fab1a6a2895d53c2b884c207cbf5550 Mon Sep 17 00:00:00 2001 From: kgriffs Date: Tue, 31 Dec 2013 11:32:06 -0600 Subject: [PATCH] fix(HTTPUnauthorized): Move scheme into kwargs This patch moves scheme from being it's own param into kwargs, to discourage using it as a positional arg. This will avoid subtle bugs in the future that may arise as the signature for the HTTPError constructor changes over time. BREAKING CHANGE: The 'scheme' argument to HTTPUnauthorized can no longer be passed positionally; it must be a named argument. --- falcon/exceptions.py | 14 +++----------- falcon/tests/test_httperror.py | 5 +++-- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/falcon/exceptions.py b/falcon/exceptions.py index 76ba999..a603768 100644 --- a/falcon/exceptions.py +++ b/falcon/exceptions.py @@ -50,19 +50,9 @@ class HTTPUnauthorized(HTTPError): Use when authentication is required, and the provided credentials are not valid, or no credentials were provided in the first place. - Args: - title: Human-friendly error title - description: Human-friendly description of the error, along with a - helpful suggestion or two. - scheme: Authentication scheme to use as the value of the - WWW-Authenticate header in the response (default None). - - The remaining (optional) args are the same as for HTTPError. - - """ - def __init__(self, title, description, scheme=None, **kwargs): + def __init__(self, title, description, **kwargs): """Initialize Args: @@ -77,6 +67,8 @@ class HTTPUnauthorized(HTTPError): """ headers = kwargs.setdefault('headers', {}) + + scheme = kwargs.pop('scheme', None) if scheme is not None: headers['WWW-Authenticate'] = scheme diff --git a/falcon/tests/test_httperror.py b/falcon/tests/test_httperror.py index 1f72c31..b79776e 100644 --- a/falcon/tests/test_httperror.py +++ b/falcon/tests/test_httperror.py @@ -67,7 +67,7 @@ class UnauthorizedResource: def on_get(self, req, resp): raise falcon.HTTPUnauthorized('Authentication Required', 'Missing or invalid token header.', - 'Token') + scheme='Token; UUID') class UnauthorizedResourceSchemaless: @@ -259,7 +259,8 @@ class TestHTTPError(testing.TestBase): self.simulate_request('/401') self.assertEqual(self.srmock.status, falcon.HTTP_401) - self.assertIn(('WWW-Authenticate', 'Token'), self.srmock.headers) + self.assertIn(('WWW-Authenticate', 'Token; UUID'), + self.srmock.headers) def test_401_schemaless(self): self.api.add_route('/401', UnauthorizedResourceSchemaless())