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.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user