def test_process_response(self, get_cid): get_cid.return_value = self.cid request = Mock() response = {} middleware = CidMiddleware() response = middleware.process_response(request, response) self.assertEqual(response['X_CORRELATION_ID'], self.cid)
def test_process_response_with_no_cid(self, get_cid): get_cid.return_value = None request = Mock() response = {} middleware = CidMiddleware() middleware.process_response(request, response) self.assertNotIn('X_CORRELATION_ID', response.keys())
def test_process_response(self, get_cid): get_cid.return_value = self.cid request = Mock() response = {} middleware = CidMiddleware() middleware.process_response(request, response) self.assertEqual(response['X_CORRELATION_ID'], self.cid)
def test_process_response_custom_header_name(self, get_cid): get_cid.return_value = self.cid request = Mock() response = {} middleware = CidMiddleware() response = middleware.process_response(request, response) self.assertEqual(response['X-Custom-Name'], self.cid)
def test_process_response_with_no_cid(self, get_cid): get_cid.return_value = None request = Mock() response = {} middleware = CidMiddleware() response = middleware.process_response(request, response) self.assertNotIn('X_CORRELATION_ID', response.keys())
def test_with_cid_from_upstream(self): request = make_request('cid-from-upstream') middleware = CidMiddleware(get_response=get_response) response = middleware(request) self.assertEqual(request.correlation_id, 'cid-from-upstream') self.assertEqual(cid.locals.get_cid(), 'cid-from-upstream') self.assertEqual(response['X_CORRELATION_ID'], 'cid-from-upstream')
def test_no_response_header(self): request = make_request('cid-from-upstream') middleware = CidMiddleware(get_response=get_response) response = middleware(request) self.assertEqual(request.correlation_id, 'cid-from-upstream') self.assertEqual(cid.locals.get_cid(), 'cid-from-upstream') self.assertEqual(response.headers, {})
def test_custom_response_header(self): request = make_request('cid-from-upstream') middleware = CidMiddleware(get_response=get_response) response = middleware(request) self.assertEqual(request.correlation_id, 'cid-from-upstream') self.assertEqual(cid.locals.get_cid(), 'cid-from-upstream') self.assertEqual(response['X_CUSTOM_HEADER'], 'cid-from-upstream')
def test_no_cid_from_upstream(self): request = make_request(cid=None) middleware = CidMiddleware(get_response=get_response) response = middleware(request) self.assertIsNone(request.correlation_id, None) self.assertIsNone(cid.locals.get_cid(), None) self.assertEqual(response.headers, {})
def test_generate_cid(self, uuid4): uuid4.return_value = 'generated-cid' request = make_request(cid=None) middleware = CidMiddleware(get_response=get_response) response = middleware(request) self.assertEqual(request.correlation_id, 'generated-cid') self.assertEqual(cid.locals.get_cid(), 'generated-cid') self.assertEqual(response['X_CORRELATION_ID'], 'generated-cid')
def test_concatenate_ids(self, uuid4): uuid4.return_value = 'local-cid' request = make_request(cid_='upstream-cid') middleware = CidMiddleware(get_response=get_response) response = middleware(request) self.assertEqual(request.correlation_id, 'upstream-cid, local-cid') self.assertEqual(cid.locals.get_cid(), 'upstream-cid, local-cid') self.assertEqual(response['X_CORRELATION_ID'], 'upstream-cid, local-cid')
def test_process_request_with_custom_header(self, set_cid): request = Mock() request.META = {'A_TEST_HEADER': 'different-cid'} middleware = CidMiddleware() middleware.process_request(request) set_cid.assert_called_with('different-cid')
def test_process_request_with_no_header(self, set_cid): request = self.get_mock_request() middleware = CidMiddleware() middleware.process_request(request) set_cid.assert_called_with(None)
def test_process_request(self, set_cid): request = self.get_mock_request(self.cid) middleware = CidMiddleware() middleware.process_request(request) set_cid.assert_called_with(self.cid)
def test_process_request_generates_uuid(self, set_cid, uuid4): uuid4.return_value = '6fa459ea-ee8a-3ca4-894e-db77e160355e' request = self.get_mock_request() middleware = CidMiddleware() middleware.process_request(request) set_cid.assert_called_with('6fa459ea-ee8a-3ca4-894e-db77e160355e')