-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Util to check if request is Hawk request (#43)
- Loading branch information
Showing
4 changed files
with
52 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def get_auth_header(request): | ||
return request.META.get('HTTP_AUTHORIZATION', '') | ||
|
||
|
||
def is_hawk_request(request): | ||
auth_header = get_auth_header(request) | ||
return auth_header.startswith('Hawk ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.test import RequestFactory | ||
|
||
from tests.base import BaseTest | ||
from hawkrest.util import get_auth_header, is_hawk_request | ||
|
||
|
||
class TestGetAuthHeader(BaseTest): | ||
|
||
def test_func_gets_http_authorization_header(self): | ||
sender = self._sender() | ||
factory_obj = RequestFactory(HTTP_AUTHORIZATION=sender.request_header) | ||
request = factory_obj.request() | ||
auth_header = get_auth_header(request) | ||
self.assertEqual(auth_header, sender.request_header) | ||
|
||
def test_func_returns_empty_string_if_no_auth_header(self): | ||
factory_obj = RequestFactory() | ||
request = factory_obj.request() | ||
auth_header = get_auth_header(request) | ||
self.assertEqual(auth_header, '') | ||
|
||
|
||
class TestIsHawkRequest(BaseTest): | ||
|
||
def test_func_returns_true_when_auth_header_begins_with_hawk(self): | ||
factory_obj = RequestFactory(HTTP_AUTHORIZATION='Hawk ') | ||
request = factory_obj.request() | ||
self.assertTrue(is_hawk_request(request)) | ||
|
||
def test_func_returns_false_if_no_trailing_whitespace(self): | ||
factory_obj = RequestFactory(HTTP_AUTHORIZATION='Hawk') | ||
request = factory_obj.request() | ||
self.assertFalse(is_hawk_request(request)) | ||
|
||
def test_func_returns_false_if_auth_header_missing(self): | ||
factory_obj = RequestFactory() | ||
request = factory_obj.request() | ||
self.assertFalse(is_hawk_request(request)) |