1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #11254: Introduce the X-Request-ID HTTP header to annotate the unique ID of each request for change logging

This commit is contained in:
jeremystretch
2023-01-13 11:41:57 -05:00
committed by jeremystretch
parent ef3ac25406
commit f74a2536f1
5 changed files with 43 additions and 4 deletions

View File

@@ -44,6 +44,9 @@ class CoreMiddleware:
with change_logging(request):
response = self.get_response(request)
# Attach the unique request ID as an HTTP header.
response['X-Request-ID'] = request.id
# If this is an API request, attach an HTTP header annotating the API version (e.g. '3.5').
if is_api_request(request):
response['API-Version'] = settings.REST_FRAMEWORK_VERSION

View File

@@ -1,3 +1,5 @@
import uuid
from django.urls import reverse
from utilities.testing import APITestCase
@@ -5,14 +7,22 @@ from utilities.testing import APITestCase
class AppTest(APITestCase):
def test_http_headers(self):
response = self.client.get(reverse('api-root'), **self.header)
# Check that all custom response headers are present and valid
self.assertEqual(response.status_code, 200)
request_id = response.headers['X-Request-ID']
uuid.UUID(request_id)
def test_root(self):
url = reverse('api-root')
response = self.client.get('{}?format=api'.format(url), **self.header)
response = self.client.get(f'{url}?format=api', **self.header)
self.assertEqual(response.status_code, 200)
def test_status(self):
url = reverse('api-status')
response = self.client.get('{}?format=api'.format(url), **self.header)
response = self.client.get(f'{url}?format=api', **self.header)
self.assertEqual(response.status_code, 200)