def test_streaming_response(self): """ Test a request to an endpoint that returns a streaming response """ s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port) r = s.get("/streaming/30") # verify that the time reported includes the download time of the whole streamed response self.assertGreater( self.runner.stats.get("/streaming/30", method="GET").avg_response_time, 250) self.runner.stats.clear_all() # verify that response time does NOT include whole download time, when using stream=True r = s.get("/streaming/30", stream=True) self.assertGreaterEqual( self.runner.stats.get("/streaming/30", method="GET").avg_response_time, 0) self.assertLess( self.runner.stats.get("/streaming/30", method="GET").avg_response_time, 250) # download the content of the streaming response (so we don't get an ugly exception in the log) _ = r.content
def test_slow_redirect(self): s = FastHttpSession("http://127.0.0.1:%i" % self.port) url = "/redirect?url=/redirect?delay=0.5" r = s.get(url) stats = global_stats.get(url, method="GET") self.assertEqual(1, stats.num_requests) self.assertGreater(stats.avg_response_time, 500)
def test_ssl_request_insecure(self): s = FastHttpSession(self.environment, "https://127.0.0.1:%i" % self.web_port, insecure=True) r = s.get("/") self.assertEqual(200, r.status_code) self.assertIn("<title>Locust</title>", r.content.decode("utf-8"))
def test_cookie(self): s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port) r = s.post("/set_cookie?name=testcookie&value=1337") self.assertEqual(200, r.status_code) r = s.get("/get_cookie?name=testcookie") self.assertEqual("1337", r.content.decode())
def test_slow_redirect(self): s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port, user=None) url = "/redirect?url=/redirect&delay=0.5" r = s.get(url) stats = self.runner.stats.get(url, method="GET") self.assertEqual(1, stats.num_requests) self.assertGreater(stats.avg_response_time, 500)
def test_404(self): global_stats.clear_all() s = FastHttpSession("http://127.0.0.1:%i" % self.port) r = s.get("/does_not_exist") self.assertEqual(404, r.status_code) self.assertEqual( 1, global_stats.get("/does_not_exist", "GET").num_failures)
def test_404(self): s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port) r = s.get("/does_not_exist") self.assertEqual(404, r.status_code) self.assertEqual( 1, self.runner.stats.get("/does_not_exist", "GET").num_failures)
def test_connection_error(self): s = FastHttpSession(self.environment, "http://localhost:1") r = s.get("/", timeout=0.1) self.assertEqual(r.status_code, 0) self.assertEqual(None, r.content) self.assertEqual(1, len(self.runner.stats.errors)) self.assertTrue(isinstance(r.error, ConnectionRefusedError)) self.assertTrue(isinstance(next(iter(self.runner.stats.errors.values())).error, ConnectionRefusedError))
def test_204(self): global_stats.clear_all() s = FastHttpSession("http://127.0.0.1:%i" % self.port) r = s.get("/status/204") self.assertEqual(204, r.status_code) self.assertEqual(1, global_stats.get("/status/204", "GET").num_requests) self.assertEqual(0, global_stats.get("/status/204", "GET").num_failures)
def test_ssl_request_insecure(self): s = FastHttpSession(self.environment, "https://127.0.0.1:%i" % self.web_port, insecure=True, user=None) r = s.get("/") self.assertEqual(200, r.status_code) self.assertIn("<title>Locust for None</title>", r.content.decode("utf-8")) self.assertIn("<p>Script: <span>None</span></p>", r.text)
def test_204(self): s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port) r = s.get("/status/204") self.assertEqual(204, r.status_code) self.assertEqual( 1, self.runner.stats.get("/status/204", "GET").num_requests) self.assertEqual( 0, self.runner.stats.get("/status/204", "GET").num_failures)
def test_connection_error(self): s = FastHttpSession(self.environment, "http://localhost:1", user=None) r = s.get("/", timeout=0.1, headers={"X-Test-Headers": "hello"}) self.assertEqual(r.status_code, 0) self.assertEqual(None, r.content) self.assertEqual(1, len(self.runner.stats.errors)) self.assertTrue(isinstance(r.error, ConnectionRefusedError)) self.assertTrue(isinstance(next(iter(self.runner.stats.errors.values())).error, ConnectionRefusedError)) self.assertEqual(r.url, "http://localhost:1/") self.assertEqual(r.request.url, r.url) self.assertEqual(r.request.headers.get("X-Test-Headers", ""), "hello")
def test_custom_ssl_context_fail_with_bad_context(self): """ Test FastHttpSession with a custom SSLContext factory that will fail as we can not set verify_mode to CERT_NONE when check_hostname is enabled """ def create_custom_context(): context = gevent.ssl.create_default_context() context.check_hostname = True context.verify_mode = gevent.ssl.CERT_NONE return context s = FastHttpSession( self.environment, "https://127.0.0.1:%i" % self.port, ssl_context_factory=create_custom_context, user=None, ) with self.assertRaises(ValueError) as e: s.get("/") self.assertEqual(e.exception.args, ("Cannot set verify_mode to CERT_NONE when check_hostname is enabled.",))
def test_connection_error(self): global_stats.clear_all() s = FastHttpSession("http://localhost:1") r = s.get("/", timeout=0.1) self.assertEqual(r.status_code, 0) self.assertEqual(None, r.content) self.assertEqual(1, len(global_stats.errors)) if six.PY2: self.assertTrue(isinstance(r.error, socket.error)) self.assertTrue( isinstance( six.next(six.itervalues(global_stats.errors)).error, socket.error)) else: self.assertTrue(isinstance(r.error, ConnectionRefusedError)) self.assertTrue( isinstance( six.next(six.itervalues(global_stats.errors)).error, ConnectionRefusedError))
def test_get(self): s = FastHttpSession("http://127.0.0.1:%i" % self.port) r = s.get("/ultra_fast") self.assertEqual(200, r.status_code)