def test_redirect(self): @scenario(weight=10) async def _one(session): # redirected async with session.get('http://localhost:8888/redirect') as resp: redirect = resp.history assert redirect[0].status == 302 assert resp.status == 200 # not redirected async with session.get('http://localhost:8888/redirect', allow_redirects=False) as resp: redirect = resp.history assert len(redirect) == 0 assert resp.status == 302 content = await resp.text() assert content == '' _RES.append(1) args = self._get_args() args.verbose = 2 args.max_runs = 2 with coserver(): run(args) self.assertTrue(len(_RES) > 0)
def test_redirect(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None @scenario(weight=10) async def _one(session): # redirected async with session.get('http://localhost:8888/redirect') as resp: redirect = resp.history assert redirect[0].status == 302 assert resp.status == 200 # not redirected async with session.get('http://localhost:8888/redirect', allow_redirects=False) as resp: redirect = resp.history assert len(redirect) == 0 assert resp.status == 302 content = await resp.text() assert content == '' _RES.append(1) args = self._get_args() args.verbose = 2 with coserver(): run(args) self.assertTrue(len(_RES) > 0) test_loop._close()
def test_redirect(self): @scenario(weight=10) async def _one(session): # redirected async with session.get("http://localhost:8888/redirect") as resp: redirect = resp.history assert redirect[0].status == 302 assert resp.status == 200 # not redirected async with session.get("http://localhost:8888/redirect", allow_redirects=False) as resp: redirect = resp.history assert len(redirect) == 0 assert resp.status == 302 content = await resp.text() assert content == "" _RES.append(1) args = self._get_args() args.verbose = 2 args.max_runs = 2 with coserver(): run(args) self.assertTrue(len(_RES) > 0)
async def test_request(self, loop, console, results): with coserver(): async with self._get_session(loop, console, verbose=2) as session: async with session.get("http://localhost:8888") as resp: self.assertEqual(resp.status, 200) res = await serialize(console) self.assertTrue("Directory listing" in res, res)
async def test_request(self, loop, console, results): with coserver(): async with self._get_session(loop, console, verbose=2) as session: async with session.get('http://localhost:8888') as resp: self.assertEqual(resp.status, 200) res = await serialize(console) self.assertTrue('GET http://127.0.0.1:8888' in res)
async def test_request(self, loop): with coserver(): stream = asyncio.Queue() async with LoggedClientSession(loop, stream, verbose=2) as session: async with session.get('http://localhost:8888') as resp: self.assertEqual(resp.status, 200) res = await serialize(stream) self.assertTrue('GET http://127.0.0.1:8888' in res[1])
def test_runner(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None _RES = [] @scenario(10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(90) async def here_two(session): session.statsd.incr('yopla') _RES.append(2) args = namedtuple('args', 'verbose quiet duration exception') args.verbose = True args.quiet = False args.duration = 1 args.exception = True args.console = True args.processes = 1 args.workers = 1 args.debug = True args.statsd = True args.statsd_server = '127.0.0.1' args.statsd_port = 9999 args.scenario = 'molotov.tests.test_run' server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0) test_loop._close()
def test_bug_121(self): PASSED = [0] with catch_sleep(): @scenario() async def scenario_one(session): cookies = { "csrftoken": "sometoken", "dtk": "1234", "djdt": "hide", "sessionid": "5678", } boundary = "----WebKitFormBoundaryFTE" headers = { "X-CSRFToken": "sometoken", "Content-Type": "multipart/form-data; boundary={}".format(boundary), } data = json.dumps({"1": "xxx"}) with aiohttp.MultipartWriter("form-data", boundary=boundary) as mpwriter: mpwriter.append( data, { "Content-Disposition": 'form-data; name="json"; filename="blob"', "Content-Type": "application/json", }, ) async with session.post( "http://localhost:8888", data=mpwriter, headers=headers, cookies=cookies, ) as resp: res = await resp.text() assert data in res PASSED[0] += 1 args = self._get_args() args.verbose = 2 args.max_runs = 1 with coserver(): res = run(args) assert PASSED[0] == 1 assert res["OK"] == 1
async def test_request(self, loop): with coserver(): stream = asyncio.Queue() async with LoggedClientSession(loop, stream, verbose=True) as session: async with session.get('http://localhost:8888') as resp: self.assertEqual(resp.status, 200) res = [] while stream.qsize() > 0: line = await stream.get() res.append(line) self.assertTrue('GET http://127.0.0.1:8888' in res[1])
def test_use_extension_module_name_fail(self): ext = 'IDONTEXTSIST' @scenario(weight=10) async def simpletest(session): async with session.get('http://localhost:8888') as resp: assert resp.status == 200 with coserver(): stdout, stderr, rc = self._test_molotov('-cx', '--max-runs', '1', '--use-extension=' + ext, '-s', 'simpletest', 'molotov.tests.test_run') self.assertTrue("Cannot import" in stdout)
def test_use_extension(self): ext = os.path.join(_HERE, 'example5.py') @scenario(weight=10) async def simpletest(session): async with session.get('http://localhost:8888') as resp: assert resp.status == 200 with coserver(): stdout, stderr, rc = self._test_molotov('-cx', '--max-runs', '1', '--use-extension=' + ext, '-s', 'simpletest', 'molotov.tests.test_run') self.assertTrue("=>" in stdout) self.assertTrue("<=" in stdout)
def test_runner(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None @global_setup() def something_sync(args): grab = request('http://localhost:8888') self.assertEqual(grab['status'], 200) grab_json = json_request('http://localhost:8888/molotov.json') self.assertTrue('molotov' in grab_json['content']) @scenario(weight=10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(weight=90) async def here_two(session): if session.statsd is not None: session.statsd.incr('yopla') _RES.append(2) args = self._get_args() server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0) test_loop._close()
def test_runner(self): test_loop = asyncio.get_event_loop() @global_setup() def something_sync(args): grab = request("http://localhost:8888") self.assertEqual(grab["status"], 200) grab_json = json_request("http://localhost:8888/molotov.json") self.assertTrue("molotov" in grab_json["content"]) @scenario(weight=10) async def here_one(session): async with session.get("http://localhost:8888") as resp: await resp.text() _RES.append(1) @scenario(weight=90) async def here_two(session): if get_context(session).statsd is not None: get_context(session).statsd.increment("yopla") _RES.append(2) args = self._get_args() server = UDPServer("127.0.0.1", 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) args.max_runs = 3 args.duration = 9999 with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0)
def test_runner(self): test_loop = asyncio.get_event_loop() @global_setup() def something_sync(args): grab = request('http://localhost:8888') self.assertEqual(grab['status'], 200) grab_json = json_request('http://localhost:8888/molotov.json') self.assertTrue('molotov' in grab_json['content']) @scenario(weight=10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(weight=90) async def here_two(session): if session.statsd is not None: session.statsd.incr('yopla') _RES.append(2) args = self._get_args() server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) args.max_runs = 3 with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0)
def test_use_extension_module_name_fail(self): ext = "IDONTEXTSIST" @scenario(weight=10) async def simpletest(session): async with session.get("http://localhost:8888") as resp: assert resp.status == 200 with coserver(): stdout, stderr, rc = self._test_molotov( "-cx", "--max-runs", "1", "--use-extension=" + ext, "-s", "simpletest", "molotov.tests.test_run", ) self.assertTrue("Cannot import" in stdout)
def test_slow_server(self): @scenario(weight=10) async def _one(session): async with session.get('http://localhost:8888/slow') as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 2 args.verbose = 2 args.max_runs = 1 start = time.time() with coserver(): run(args) # makes sure the test is stopped even if the server # hangs a socket self.assertTrue(time.time() - start < 4) self.assertTrue(len(_RES) == 0)
def test_use_extension(self): ext = os.path.join(_HERE, "example5.py") @scenario(weight=10) async def simpletest(session): async with session.get("http://localhost:8888") as resp: assert resp.status == 200 with coserver(): stdout, stderr, rc = self._test_molotov( "-cx", "--max-runs", "1", "--use-extension=" + ext, "-s", "simpletest", "molotov.tests.test_run", ) self.assertTrue("=>" in stdout) self.assertTrue("<=" in stdout)
def test_slow_server_force_shutdown(self): @scenario(weight=10) async def _one(session): async with session.get('http://localhost:8888/slow') as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 2 args.verbose = 2 args.max_runs = 1 args.force_shutdown = True start = time.time() with coserver(): run(args) # makes sure the test is stopped even if the server # hangs a socket self.assertTrue(time.time() - start < 4) self.assertTrue(len(_RES) == 0)
def test_slow_server_graceful(self): @scenario(weight=10) async def _one(session): async with session.get("http://localhost:8888/slow") as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 0.1 args.verbose = 2 args.max_runs = 1 # graceful shutdown on the other hand will wait # for the worker completion args.graceful_shutdown = True start = time.time() with coserver(): run(args) # makes sure the test finishes self.assertTrue(time.time() - start > 5) self.assertTrue(len(_RES) == 1)
def test_slow_server_graceful(self): @scenario(weight=10) async def _one(session): async with session.get('http://localhost:8888/slow') as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 2 args.verbose = 2 args.max_runs = 1 # graceful shutdown on the other hand will wait # for the worker completion args.graceful_shutdown = True start = time.time() with coserver(): run(args) # makes sure the test finishes self.assertTrue(time.time() - start > 5) self.assertTrue(len(_RES) == 1)
def test_local_import(self): test = os.path.join(_HERE, "example9.py") with coserver(): stdout, stderr, rc = self._test_molotov("--max-runs", "1", test) self.assertTrue("SUCCESSES: 1" in stdout, stdout)
def test_runner(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None @global_setup() def something_sync(args): grab = request('http://localhost:8888') self.assertEqual(grab['status'], 200) grab_json = json_request('http://localhost:8888/molotov.json') self.assertTrue('molotov' in grab_json['content']) @scenario(10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(90) async def here_two(session): if session.statsd is not None: session.statsd.incr('yopla') _RES.append(2) args = namedtuple('args', 'verbose quiet duration exception') args.verbose = 1 args.quiet = False args.duration = 1 args.exception = True args.console = True args.processes = 1 args.workers = 1 args.debug = True args.statsd = True args.statsd_server = '127.0.0.1' args.statsd_port = 9999 args.scenario = 'molotov.tests.test_run' args.single_mode = None args.max_runs = None server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0) test_loop._close()
async def _setup_session(wid, session): with coserver(): html = str(request("http://localhost:8888")) content.append(html)
def test_runner_console(self): with coserver(): return self._runner(console=True)
def test_runner(self): with coserver(): return self._runner(console=False)