def test_http_request_populated(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request expected_path = "http://testserver/123" expected_agent = "Mozilla/5.0" expected_referrer = "self" body_content = "test" django_request = RequestFactory().put( expected_path, data=body_content, HTTP_USER_AGENT=expected_agent, HTTP_REFERER=expected_referrer, ) middleware = request.RequestMiddleware(None) middleware.process_request(django_request) http_request, trace_id = self._call_fut() self.assertEqual(http_request["requestMethod"], "PUT") self.assertEqual(http_request["requestUrl"], expected_path) self.assertEqual(http_request["userAgent"], expected_agent) self.assertEqual(http_request["referer"], expected_referrer) self.assertEqual(http_request["remoteIp"], "127.0.0.1") self.assertEqual(http_request["requestSize"], len(body_content)) self.assertEqual(http_request["protocol"], "HTTP/1.1")
def test_http_request_populated(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request expected_path = "http://testserver/123" expected_agent = "Mozilla/5.0" expected_referrer = "self" body_content = "test" django_request = RequestFactory().put( expected_path, data=body_content, HTTP_USER_AGENT=expected_agent, HTTP_REFERER=expected_referrer, ) # ensure test passes even after request has been read # context: https://github.com/googleapis/python-logging/issues/159 django_request.read() middleware = request.RequestMiddleware(None) middleware.process_request(django_request) http_request, *_ = self._call_fut() self.assertEqual(http_request["requestMethod"], "PUT") self.assertEqual(http_request["requestUrl"], expected_path) self.assertEqual(http_request["userAgent"], expected_agent) self.assertEqual(http_request["protocol"], "HTTP/1.1")
def test_no_context_header(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request django_request = RequestFactory().get("/") middleware = request.RequestMiddleware() middleware.process_request(django_request) trace_id = self._call_fut() self.assertIsNone(trace_id)
def test_http_request_sparse(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request expected_path = "http://testserver/123" django_request = RequestFactory().put(expected_path) middleware = request.RequestMiddleware(None) middleware.process_request(django_request) http_request, *_ = self._call_fut() self.assertEqual(http_request["requestMethod"], "PUT") self.assertEqual(http_request["requestUrl"], expected_path) self.assertEqual(http_request["protocol"], "HTTP/1.1")
def test_no_context_header(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request django_request = RequestFactory().get("/") middleware = request.RequestMiddleware(None) middleware.process_request(django_request) http_request, trace_id, span_id = self._call_fut() self.assertEqual(http_request["requestMethod"], "GET") self.assertIsNone(trace_id) self.assertIsNone(span_id)
def test_valid_context_header(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request django_trace_header = "HTTP_X_CLOUD_TRACE_CONTEXT" expected_trace_id = "testtraceiddjango" django_trace_id = expected_trace_id + "/testspanid" django_request = RequestFactory().get( "/", **{django_trace_header: django_trace_id}) middleware = request.RequestMiddleware() middleware.process_request(django_request) trace_id = self._call_fut() self.assertEqual(trace_id, expected_trace_id)
def test_valid_context_header(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request django_trace_header = "HTTP_X_CLOUD_TRACE_CONTEXT" expected_span_id = _DJANGO_SPAN_ID expected_trace_id = _DJANGO_TRACE_ID django_trace_id = f"{expected_trace_id}/{expected_span_id}" django_request = RequestFactory().get( "/", **{django_trace_header: django_trace_id} ) middleware = request.RequestMiddleware(None) middleware.process_request(django_request) http_request, trace_id, span_id = self._call_fut() self.assertEqual(trace_id, expected_trace_id) self.assertEqual(span_id, expected_span_id) self.assertEqual(http_request["requestMethod"], "GET")
def test_traceparent_header(self): from django.test import RequestFactory from google.cloud.logging_v2.handlers.middleware import request django_trace_header = "HTTP_TRACEPARENT" expected_trace_id = "4bf92f3577b34da6a3ce929d0e0e4736" expected_span_id = "00f067aa0ba902b7" header = f"00-{expected_trace_id}-{expected_span_id}-01" django_request = RequestFactory().get("/", **{django_trace_header: header}) middleware = request.RequestMiddleware(None) middleware.process_request(django_request) http_request, trace_id, span_id, sampled = self._call_fut() self.assertEqual(trace_id, expected_trace_id) self.assertEqual(span_id, expected_span_id) self.assertEqual(sampled, True) self.assertEqual(http_request["requestMethod"], "GET")