class TestHttpProxy(unittest.TestCase): def setUp(self): self._proxy_pid = start_proxy(options=_OPTIONS, log_level='error', log_output='/dev/null', protocol='http') self._web = start_simplehttp_server() time.sleep(.3) try: if self._web.poll(): raise ValueError("Could not start the proxy") self.client = Client() assert self.client.get_behavior() == 'dummy' except Exception: self.tearDown() raise def tearDown(self): stop_proxy(self._proxy_pid) self._web.terminate() def test_proxy(self): # let's do a few simple request first to make sure the proxy works self.assertEqual(self.client.get_behavior(), 'dummy') times = [] for i in range(10): start = time.time() try: res = requests.get(_PROXY) finally: times.append(time.time() - start) self.assertEqual(res.status_code, 200) fastest = min(times) # now let's try the various behaviors with self.client.with_behavior('blackout'): # oh look we broke it self.assertRaises(requests.ConnectionError, requests.get, _PROXY) self.assertEqual(self.client.get_behavior(), 'blackout') with self.client.with_behavior('delay'): # should work but be slower start = time.time() try: res = requests.get(_PROXY) finally: duration = time.time() - start self.assertEqual(res.status_code, 200) self.assertTrue(duration > fastest + 1) # we should be back to normal self.assertEqual(self.client.get_behavior(), 'dummy') res = requests.get(_PROXY) self.assertEqual(res.status_code, 200)
class TestHttpProxy(unittest.TestCase): def setUp(self): self._proxy_pid = start_proxy(options=_OPTIONS, log_level='error', log_output='/dev/null', protocol='http') self._web = start_simplehttp_server() time.sleep(.3) try: if self._web.poll(): raise ValueError("Could not start the proxy") self.client = Client() assert self.client.get_behavior() == 'dummy' except Exception: self.tearDown() raise def tearDown(self): stop_proxy(self._proxy_pid) self._web.terminate() def test_proxy(self): # let's do a few simple request first to make sure the proxy works self.assertEqual(self.client.get_behavior(), 'dummy') times = [] for i in range(10): start = time.time() try: res = requests.get(_PROXY) finally: times.append(time.time() - start) self.assertEqual(res.status_code, 200) fastest = min(times) # now let's try the various behaviors with self.client.with_behavior('blackout'): # oh look we broke it self.assertRaises(requests.ConnectionError, requests.get, _PROXY) self.assertEqual(self.client.get_behavior(), 'blackout') with self.client.with_behavior('delay'): # should work but be slower start = time.time() try: res = requests.get(_PROXY) finally: duration = time.time() - start self.assertEqual(res.status_code, 200) self.assertTrue(duration > fastest + 1) # we should be back to normal self.assertEqual(self.client.get_behavior(), 'dummy') res = requests.get(_PROXY) self.assertEqual(res.status_code, 200)
class TestSimpleProxy(unittest.TestCase): def setUp(self): self._proxy_pid = start_proxy(log_output='/dev/null', log_level='error') self._web = start_simplehttp_server() time.sleep(.2) try: if self._web.poll(): raise ValueError("Could not start the proxy") self.client = Client() assert self.client.get_behavior() == 'dummy' except Exception: self.tearDown() raise def tearDown(self): stop_proxy(self._proxy_pid) self._web.terminate() def test_existing_behaviors(self): wanted = [ 'blackout', 'delay', 'dummy', 'error', 'hang', 'transient', 'abort' ] self.assertEqual(set(self.client.list_behaviors()), set(wanted)) def test_proxy(self): # let's do a few simple request first to make sure the proxy works self.assertEqual(self.client.get_behavior(), 'dummy') for i in range(10): res = requests.get(_PROXY) self.assertEqual(res.status_code, 200) # now let's add a bit of havoc with self.client.with_behavior('blackout'): # oh look we broke it self.assertRaises(requests.ConnectionError, requests.get, _PROXY) self.assertEqual(self.client.get_behavior(), 'blackout') with self.client.with_behavior('abort'): # oh look we broke it self.assertRaises(requests.ConnectionError, requests.get, _PROXY) self.assertEqual(self.client.get_behavior(), 'abort') # we should be back to normal self.assertEqual(self.client.get_behavior(), 'dummy') res = requests.get(_PROXY) self.assertEqual(res.status_code, 200)
class TestSimpleProxy(unittest.TestCase): def setUp(self): self._proxy_pid = start_proxy(log_output='/dev/null', log_level='error') self._web = start_simplehttp_server() time.sleep(.2) try: if self._web.poll(): raise ValueError("Could not start the proxy") self.client = Client() assert self.client.get_behavior() == 'dummy' except Exception: self.tearDown() raise def tearDown(self): stop_proxy(self._proxy_pid) self._web.terminate() def test_existing_behaviors(self): wanted = ['blackout', 'delay', 'dummy', 'error', 'hang', 'transient', 'abort'] self.assertEqual(set(self.client.list_behaviors()), set(wanted)) def test_proxy(self): # let's do a few simple request first to make sure the proxy works self.assertEqual(self.client.get_behavior(), 'dummy') for i in range(10): res = requests.get(_PROXY) self.assertEqual(res.status_code, 200) # now let's add a bit of havoc with self.client.with_behavior('blackout'): # oh look we broke it self.assertRaises(requests.ConnectionError, requests.get, _PROXY) self.assertEqual(self.client.get_behavior(), 'blackout') with self.client.with_behavior('abort'): # oh look we broke it self.assertRaises(requests.ConnectionError, requests.get, _PROXY) self.assertEqual(self.client.get_behavior(), 'abort') # we should be back to normal self.assertEqual(self.client.get_behavior(), 'dummy') res = requests.get(_PROXY) self.assertEqual(res.status_code, 200)
class TestClient(TestCase): def setUp(self): port = 8009 cmd = '%s -m vaurien.tests.support %s' % (sys.executable, port) self.process = Popen(cmd.split(' ')) # wait for the server to start time.sleep(0.5) self.client = Client('localhost', port) def tearDown(self): self.process.terminate() self.process.wait() def test_set_valid_behavior(self): # check that we don't raise self.client.set_behavior('blackout') def test_set_invlid_behavior(self): self.assertRaises(ValueError, self.client.set_behavior, 'invalid_behavior') def test_get_default_behavior(self): # this should return the default behavior self.assertEquals(self.client.get_behavior(), 'default') def test_set_and_get_behavior(self): # after setting up the behavior, we should retrieve the informations # here self.client.set_behavior('blackout') self.assertEquals(self.client.get_behavior(), 'blackout') def test_set_behavior_with_options(self): self.client.set_behavior('blackout', foo='foo', bar='bar') self.assertEquals(self.client.get_behavior(), 'blackout') def test_list_behaviors(self): self.assertEquals(self.client.list_behaviors(), ['default', 'blackout'])
class TestClient(TestCase): def setUp(self): port = 8009 cmd = '%s -m vaurien.tests.support %s' % (sys.executable, port) self.process = Popen(cmd.split(' ')) # wait for the server to start time.sleep(1.) self.client = Client('localhost', port) def tearDown(self): self.process.terminate() self.process.wait() def test_set_valid_behavior(self): # check that we don't raise self.client.set_behavior('blackout') def test_set_invlid_behavior(self): self.assertRaises(ValueError, self.client.set_behavior, 'invalid_behavior') def test_get_default_behavior(self): # this should return the default behavior self.assertEquals(self.client.get_behavior(), 'default') def test_set_and_get_behavior(self): # after setting up the behavior, we should retrieve the informations # here self.client.set_behavior('blackout') self.assertEquals(self.client.get_behavior(), 'blackout') def test_set_behavior_with_options(self): self.client.set_behavior('blackout', foo='foo', bar='bar') self.assertEquals(self.client.get_behavior(), 'blackout') def test_list_behaviors(self): self.assertEquals(self.client.list_behaviors(), ['default', 'blackout'])