示例#1
0
    def test_logging(self):
        port = find_free_port()
        expected = [
            (11, 'GET -> http://localhost:{}/test?a=1'.format(port)),
            (11, 'GET -> http://localhost:{}/test'.format(port)),
            (10, 'GET -> http://localhost:{}/test'.format(port)),
        ]
        # noinspection PyTypeChecker
        with self.assertLogs('requests_client.client',
                             level=logging.DEBUG) as captured:
            client = RequestsClient('localhost', port=port, log_lvl=11)
            with suppress(RequestException):
                client.get('test', params={'a': 1}, timeout=0.01)
            with suppress(RequestException):
                client.get('test',
                           params={'a': 1},
                           timeout=0.01,
                           log_params=False)
            with suppress(RequestException):
                client.get('test', params={'a': 1}, timeout=0.01, log=False)
            client = RequestsClient('localhost', port=port, log_params=False)
            with suppress(RequestException):
                client.get('test', params={'a': 1}, timeout=0.01)

        results = [(r.levelno, r.message) for r in captured.records]
        self.assertListEqual(results, expected)
示例#2
0
 def test_new_session_after_close(self):
     client = RequestsClient('http://localhost:1234/', session_fn=MagicMock)
     with client:
         client.get('/')
         # noinspection PyUnresolvedReferences
         session_1 = client._RequestsClient__session  # type: MagicMock
         self.assertTrue(session_1.request.called)
         self.assertFalse(session_1.close.called)
     self.assertTrue(session_1.close.called)
     client.get('/')
     # noinspection PyUnresolvedReferences
     session_2 = client._RequestsClient__session  # type: MagicMock
     self.assertNotEqual(session_1, session_2)
     self.assertTrue(session_2.request.called)
     self.assertFalse(session_2.close.called)