Skip to content

Commit 10257a9

Browse files
committed
Prevent repeating the headers argument when setting custom headers.
1 parent b91a451 commit 10257a9

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

lib/pyld/documentloader/requests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self, secure=False, session=None, **kwargs):
2828
session = requests.Session()
2929
self.session = session
3030
self.secure = secure
31+
self.headers = kwargs.pop('headers', None)
3132
self.kwargs = kwargs
3233

3334
def __call__(self, url, options=None) -> RemoteDocument:
@@ -58,7 +59,7 @@ def __call__(self, url, options=None) -> RemoteDocument:
5859
'the URL\'s scheme is not "https".',
5960
'jsonld.InvalidUrl', {'url': url},
6061
code='loading document failed')
61-
headers = options.get('headers')
62+
headers = options.get('headers', self.headers)
6263
if headers is None:
6364
headers = {
6465
'Accept': 'application/ld+json, application/json'

tests/test_document_loader.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ def test_requests_document_loader_factory_returns_document_loader():
8888
assert callable(loader)
8989

9090

91+
def test_requests_document_loader_accepts_custom_headers():
92+
"""Requests factory accepts default headers without passing headers twice."""
93+
class Response:
94+
headers = {'content-type': 'application/ld+json'}
95+
url = 'http://example.com/context'
96+
97+
def json(self):
98+
return {'@context': {}}
99+
100+
class Session:
101+
def __init__(self):
102+
self.headers = None
103+
104+
def get(self, url, headers=None):
105+
self.headers = headers
106+
return Response()
107+
108+
session = Session()
109+
headers = {'Accept': 'application/json'}
110+
111+
loader = jsonld.requests_document_loader(session=session, headers=headers)
112+
loader('http://example.com/context')
113+
114+
assert session.headers == headers
115+
116+
91117
def test_aiohttp_document_loader_factory_returns_document_loader():
92118
"""Aiohttp factory returns a class-based callable document loader."""
93119
pytest.importorskip("aiohttp")

0 commit comments

Comments
 (0)