예제 #1
0
 def test_id_generation(self):
     request = self.factory.get('/')
     middleware = RequestIDMiddleware()
     middleware.process_request(request)
     self.assertTrue(hasattr(request, 'id'))
     test_view(request)
     self.assertTrue(request.id in self.handler.messages[0])
예제 #2
0
 def test_id_generation(self):
     request = self.factory.get('/')
     middleware = RequestIDMiddleware()
     middleware.process_request(request)
     self.assertTrue(hasattr(request, 'id'))
     test_view(request)
     self.assertTrue(request.id in self.handler.messages[0])
예제 #3
0
 def test_response_header_unset(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER'):
         request = self.factory.get('/')
         request.META['REQUEST_ID_HEADER'] = 'some_request_id'
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         response = test_view(request)
         self.assertFalse(response.has_header('REQUEST_ID'))
예제 #4
0
 def test_external_id_missing_in_http_header_should_fallback_to_generated_id(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER', GENERATE_REQUEST_ID_IF_NOT_IN_HEADER=True):
         request = self.factory.get('/')
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         self.assertTrue(hasattr(request, 'id'))
         test_view(request)
         self.assertTrue(request.id in self.handler.messages[0])
예제 #5
0
 def test_response_header_unset(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER'):
         request = self.factory.get('/')
         request.META['REQUEST_ID_HEADER'] = 'some_request_id'
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         response = test_view(request)
         self.assertFalse(response.has_header('REQUEST_ID'))
예제 #6
0
 def test_external_id_missing_in_http_header_should_fallback_to_generated_id(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER', GENERATE_REQUEST_ID_IF_NOT_IN_HEADER=True):
         request = self.factory.get('/')
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         self.assertTrue(hasattr(request, 'id'))
         test_view(request)
         self.assertTrue(request.id in self.handler.messages[0])
예제 #7
0
 def test_response_header_unset(self):
     with self.settings(LOG_REQUEST_ID_HEADER="REQUEST_ID_HEADER"):
         request = self.factory.get("/")
         request.META["REQUEST_ID_HEADER"] = "some_request_id"
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         response = test_view(request)
         self.assertFalse(response.has_header("REQUEST_ID"))
예제 #8
0
 def test_external_id_in_http_header(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER'):
         request = self.factory.get('/')
         request.META['REQUEST_ID_HEADER'] = 'some_request_id'
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         self.assertEqual(request.id, 'some_request_id')
         test_view(request)
         self.assertTrue('some_request_id' in self.handler.messages[0])
예제 #9
0
 def test_external_id_in_http_header(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER'):
         request = self.factory.get('/')
         request.META['REQUEST_ID_HEADER'] = 'some_request_id'
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         self.assertEqual(request.id, 'some_request_id')
         test_view(request)
         self.assertTrue('some_request_id' in self.handler.messages[0])
예제 #10
0
 def test_external_id_in_http_header(self):
     with self.settings(LOG_REQUEST_ID_HEADER="REQUEST_ID_HEADER"):
         request = self.factory.get("/")
         request.META["REQUEST_ID_HEADER"] = "some_request_id"
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         self.assertEqual(request.id, "some_request_id")
         test_view(request)
         self.assertTrue("some_request_id" in self.handler.messages[0])
예제 #11
0
 def test_request_id_passthrough(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER'):
         request = self.factory.get('/')
         request.META['REQUEST_ID_HEADER'] = 'some_request_id'
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         self.assertEqual(request.id, 'some_request_id')
         session = Session()
         outgoing = Request('get', 'http://nowhere')
         session.prepare_request(outgoing)
         self.assertEqual(outgoing.headers['REQUEST_ID_HEADER'],
                          'some_request_id')
예제 #12
0
    def test_request_id_passthrough(self):
        with self.settings(LOG_REQUEST_ID_HEADER="REQUEST_ID_HEADER"):
            from log_request_id.session import Session

            request = self.factory.get("/")
            request.META["REQUEST_ID_HEADER"] = "some_request_id"
            middleware = RequestIDMiddleware()
            middleware.process_request(request)
            self.assertEqual(request.id, "some_request_id")
            session = Session()
            outgoing = Request("get", "http://nowhere")
            session.prepare_request(outgoing)
            self.assertEqual(outgoing.headers["REQUEST_ID_HEADER"], "some_request_id")
예제 #13
0
    def test_log_requests(self):
        class DummyUser(object):
            pk = "fake_pk"

        with self.settings(LOG_REQUESTS=True):
            request = self.factory.get("/")
            request.user = DummyUser()
            middleware = RequestIDMiddleware()
            middleware.process_request(request)
            response = test_view(request)
            middleware.process_response(request, response)
            self.assertEqual(len(self.handler.messages), 2)
            self.assertTrue("fake_pk" in self.handler.messages[1])
예제 #14
0
    def process_request(self, request):
        local.start_time = time.time()
        RequestIDMiddleware.process_request(self, request)
        if 'HTTP_X_FORWARDED_FOR' in request.META:
            request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR']
        local.remote_addr = request.META['REMOTE_ADDR']

        logger.info(HR)
        logger.info('BEGIN TRANSACTION')
        logger.info(HR)
        logger.info('request.user: %s' % request.user)
        logger.info('request.method: %s' % request.method)
        logger.info('request.path: %s' % request.path)
        logger.debug('request.body: %s' % request.body)
예제 #15
0
 def test_request_id_passthrough(self):
     with self.settings(LOG_REQUEST_ID_HEADER='REQUEST_ID_HEADER'):
         from log_request_id.session import Session
         request = self.factory.get('/')
         request.META['REQUEST_ID_HEADER'] = 'some_request_id'
         middleware = RequestIDMiddleware()
         middleware.process_request(request)
         self.assertEqual(request.id, 'some_request_id')
         session = Session()
         outgoing = Request('get', 'http://nowhere')
         session.prepare_request(outgoing)
         self.assertEqual(
             outgoing.headers['REQUEST_ID_HEADER'],
             'some_request_id'
         )
예제 #16
0
    def test_log_requests(self):
        class DummyUser(object):
            pk = 'fake_pk'

        with self.settings(LOG_REQUESTS=True):
            request = self.factory.get('/')
            request.user = DummyUser()
            middleware = RequestIDMiddleware()
            middleware.process_request(request)
            response = test_view(request)
            middleware.process_response(request, response)
            self.assertEqual(len(self.handler.messages), 2)
            self.assertTrue('fake_pk' in self.handler.messages[1])