Skip to content

Commit bed4873

Browse files
crgklbalmaceda
authored andcommitted
Always include Content-Type header in management requests (#158)
* always include Content-Type header in management requests * set authorization header in management constructor * string format in AuthenticationBase
1 parent 3c60f6e commit bed4873

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

auth0/v3/authentication/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, domain, telemetry=True):
4545
}).encode('utf-8')
4646

4747
self.base_headers.update({
48-
'User-Agent': 'Python/%s' % py_version,
48+
'User-Agent': 'Python/{}'.format(py_version),
4949
'Auth0-Client': base64.b64encode(auth0_client),
5050
})
5151

auth0/v3/management/rest.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class RestClient(object):
1616
def __init__(self, jwt, telemetry=True):
1717
self.jwt = jwt
1818

19+
self.base_headers = {
20+
'Authorization': 'Bearer {}'.format(self.jwt),
21+
'Content-Type': 'application/json',
22+
}
1923
if telemetry:
2024
py_version = platform.python_version()
2125
version = sys.modules['auth0'].__version__
@@ -36,68 +40,44 @@ def __init__(self, jwt, telemetry=True):
3640
]
3741
}).encode('utf-8')
3842

39-
self.base_headers = {
43+
self.base_headers.update({
4044
'User-Agent': 'Python/{}'.format(py_version),
4145
'Auth0-Client': base64.b64encode(auth0_client),
42-
'Content-Type': 'application/json'
43-
}
44-
else:
45-
self.base_headers = {}
46+
})
4647

4748
def get(self, url, params=None):
4849
headers = self.base_headers.copy()
49-
headers.update({
50-
'Authorization': 'Bearer {}'.format(self.jwt),
51-
})
5250

5351
response = requests.get(url, params=params, headers=headers)
5452
return self._process_response(response)
5553

5654
def post(self, url, data=None):
5755
headers = self.base_headers.copy()
58-
headers.update({
59-
'Authorization': 'Bearer {}'.format(self.jwt),
60-
'Content-Type': 'application/json'
61-
})
6256

6357
response = requests.post(url, data=json.dumps(data or {}), headers=headers)
6458
return self._process_response(response)
6559

6660
def file_post(self, url, data=None, files=None):
6761
headers = self.base_headers.copy()
6862
headers.pop('Content-Type', None)
69-
headers.update({
70-
'Authorization': 'Bearer {}'.format(self.jwt),
71-
})
7263

7364
response = requests.post(url, data=data, files=files, headers=headers)
7465
return self._process_response(response)
7566

7667
def patch(self, url, data=None):
7768
headers = self.base_headers.copy()
78-
headers.update({
79-
'Authorization': 'Bearer {}'.format(self.jwt),
80-
'Content-Type': 'application/json'
81-
})
8269

8370
response = requests.patch(url, data=json.dumps(data or {}), headers=headers)
8471
return self._process_response(response)
8572

8673
def put(self, url, data=None):
8774
headers = self.base_headers.copy()
88-
headers.update({
89-
'Authorization': 'Bearer {}'.format(self.jwt),
90-
'Content-Type': 'application/json'
91-
})
9275

9376
response = requests.put(url, data=json.dumps(data or {}), headers=headers)
9477
return self._process_response(response)
9578

9679
def delete(self, url, params=None):
9780
headers = self.base_headers.copy()
98-
headers.update({
99-
'Authorization': 'Bearer {}'.format(self.jwt),
100-
})
10181

10282
response = requests.delete(url, headers=headers, params=params or {})
10383
return self._process_response(response)

auth0/v3/test/management/test_rest.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class TestRest(unittest.TestCase):
1313
@mock.patch('requests.get')
1414
def test_get(self, mock_get):
1515
rc = RestClient(jwt='a-token', telemetry=False)
16-
headers = {'Authorization': 'Bearer a-token'}
16+
headers = {
17+
'Authorization': 'Bearer a-token',
18+
'Content-Type': 'application/json',
19+
}
1720

1821
mock_get.return_value.text = '["a", "b"]'
1922
mock_get.return_value.status_code = 200
@@ -198,6 +201,20 @@ def test_post_error_with_no_response_text(self, mock_post):
198201
self.assertEqual(context.exception.error_code, 'a0.sdk.internal.unknown')
199202
self.assertEqual(context.exception.message, '')
200203

204+
@mock.patch('requests.post')
205+
def test_file_post_content_type_is_none(self, mock_post):
206+
rc = RestClient(jwt='a-token', telemetry=False)
207+
headers = {'Authorization': 'Bearer a-token'}
208+
mock_post.return_value.status_code = 200
209+
mock_post.return_value.text = 'Success'
210+
211+
data = {'some': 'data'}
212+
files = [mock.Mock()]
213+
214+
rc.file_post('the-url', data=data, files=files)
215+
216+
mock_post.assert_called_once_with('the-url', data=data, files=files, headers=headers)
217+
201218
@mock.patch('requests.patch')
202219
def test_patch(self, mock_patch):
203220
rc = RestClient(jwt='a-token', telemetry=False)
@@ -234,7 +251,10 @@ def test_patch_errors(self, mock_patch):
234251
@mock.patch('requests.delete')
235252
def test_delete(self, mock_delete):
236253
rc = RestClient(jwt='a-token', telemetry=False)
237-
headers = {'Authorization': 'Bearer a-token'}
254+
headers = {
255+
'Authorization': 'Bearer a-token',
256+
'Content-Type': 'application/json',
257+
}
238258

239259
mock_delete.return_value.text = '["a", "b"]'
240260
mock_delete.return_value.status_code = 200
@@ -262,8 +282,12 @@ def test_delete_errors(self, mock_delete):
262282

263283
def test_disabled_telemetry(self):
264284
rc = RestClient(jwt='a-token', telemetry=False)
285+
expected_headers = {
286+
'Content-Type': 'application/json',
287+
'Authorization': 'Bearer a-token',
288+
}
265289

266-
self.assertEqual(rc.base_headers, {})
290+
self.assertEqual(rc.base_headers, expected_headers)
267291

268292
def test_enabled_telemetry(self):
269293
rc = RestClient(jwt='a-token', telemetry=True)

0 commit comments

Comments
 (0)