def test_non_json_response(self): """Ensure that the API client handles non-JSON responses gracefully""" imposter = self.mb.create_imposter('test_api_client/stubs/test_client_response.json') api_client = ApiClient(BASE_URL=self.mb.get_imposter_url(imposter), API_TOKEN='dummy') response, _ = api_client.get('/non-json') self.assertEqual('<html><body><h1>Testing!</h1></body></html>', response) response, _ = api_client.post('/non-json') self.assertEqual('<html><body><h1>Testing!</h1></body></html>', response)
def test_non_json_response(self): """Ensure that the API client handles non-JSON responses gracefully""" imposter = self.mb.create_imposter( 'test_api_client/stubs/test_client_response.json') api_client = ApiClient(BASE_URL=self.mb.get_imposter_url(imposter), API_TOKEN='dummy') response, _ = api_client.get('/non-json') self.assertEqual('<html><body><h1>Testing!</h1></body></html>', response) response, _ = api_client.post('/non-json') self.assertEqual('<html><body><h1>Testing!</h1></body></html>', response)
def test_no_response(self): """Ensure that API client handles no response from the server gracefully""" api_client = ApiClient(BASE_URL='http://example.com', API_TOKEN='dummy') # fake out the requests.get method requests.get = MagicMock(return_value=None) response, status_code = api_client.get('/') self.assertIsNone(response) self.assertIsNone(status_code) # fake out the requests.post method requests.post = MagicMock(return_value=None) response, status_code = api_client.post('/') self.assertIsNone(response) self.assertIsNone(status_code) # reload requests so that the methods are back to normal reload(requests)
class TestTimeouts(unittest.TestCase): # these tests will ONLY work on UNIX-based systems because of the signal package. So no Windows for us. def setUp(self): settings.set('BASE_URL', 'http://localhost:9000') settings.set('REQUEST_CONNECTION_TIMEOUT', 0.5) self.client = ApiClient() self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.bind(('', 9000)) def tearDown(self): self.socket.close() reload(theoktany.conf) @staticmethod def timeout_handler(signum, frame): raise AssertionError('Connection did not time out!') def test_get_timeout(self): # set a timeout in case something goes horribly wrong signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(int(3 * settings.get('REQUEST_CONNECTION_TIMEOUT'))) test_start = time.time() response = self.client.get('/') test_end = time.time() # stop listening for the signal signal.signal(signal.SIGALRM, lambda *args: None) # end-start is going to be a bit more than the timeout self.assertLess(test_end - test_start, 1.5 * settings.get('REQUEST_CONNECTION_TIMEOUT'), msg='Connection timeout did not obey setting.') self.assertEqual((None, None), response) def test_post_timeout(self): # set a timeout in case something goes horribly wrong signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(int(3 * settings.get('REQUEST_CONNECTION_TIMEOUT'))) test_start = time.time() response = self.client.post('/') test_end = time.time() # stop listening for the signal signal.signal(signal.SIGALRM, lambda *args: None) # end-start is going to be a bit more than the timeout self.assertLess(test_end - test_start, 1.5 * settings.get('REQUEST_CONNECTION_TIMEOUT'), msg='Connection timeout did not obey setting.') self.assertEqual((None, None), response) def test_delete_timeout(self): # set a timeout in case something goes horribly wrong signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(int(3 * settings.get('REQUEST_CONNECTION_TIMEOUT'))) test_start = time.time() response = self.client.delete('/') test_end = time.time() # stop listening for the signal signal.signal(signal.SIGALRM, lambda *args: None) # end-start is going to be a bit more than the timeout self.assertLess(test_end - test_start, 1.5 * settings.get('REQUEST_CONNECTION_TIMEOUT'), msg='Connection timeout did not obey setting.') self.assertEqual((None, None), response)
class TestTimeouts(unittest.TestCase): # these tests will ONLY work on UNIX-based systems because of the signal package. So no Windows for us. def setUp(self): settings.set('BASE_URL', 'http://localhost:9000') settings.set('REQUEST_CONNECTION_TIMEOUT', 0.5) self.client = ApiClient() self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.bind(('', 9000)) def tearDown(self): self.socket.close() reload(theoktany.conf) @staticmethod def timeout_handler(signum, frame): raise AssertionError('Connection did not time out!') def test_get_timeout(self): # set a timeout in case something goes horribly wrong signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(int(3*settings.get('REQUEST_CONNECTION_TIMEOUT'))) test_start = time.time() response = self.client.get('/') test_end = time.time() # stop listening for the signal signal.signal(signal.SIGALRM, lambda *args: None) # end-start is going to be a bit more than the timeout self.assertLess( test_end-test_start, 1.5*settings.get('REQUEST_CONNECTION_TIMEOUT'), msg='Connection timeout did not obey setting.') self.assertEqual((None, None), response) def test_post_timeout(self): # set a timeout in case something goes horribly wrong signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(int(3*settings.get('REQUEST_CONNECTION_TIMEOUT'))) test_start = time.time() response = self.client.post('/') test_end = time.time() # stop listening for the signal signal.signal(signal.SIGALRM, lambda *args: None) # end-start is going to be a bit more than the timeout self.assertLess( test_end-test_start, 1.5*settings.get('REQUEST_CONNECTION_TIMEOUT'), msg='Connection timeout did not obey setting.') self.assertEqual((None, None), response) def test_delete_timeout(self): # set a timeout in case something goes horribly wrong signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(int(3*settings.get('REQUEST_CONNECTION_TIMEOUT'))) test_start = time.time() response = self.client.delete('/') test_end = time.time() # stop listening for the signal signal.signal(signal.SIGALRM, lambda *args: None) # end-start is going to be a bit more than the timeout self.assertLess( test_end-test_start, 1.5*settings.get('REQUEST_CONNECTION_TIMEOUT'), msg='Connection timeout did not obey setting.') self.assertEqual((None, None), response)