def test_request_connection_error(self): class MyLocust(HttpLocust): host = "http://localhost:1" locust = MyLocust() response = locust.client.get("/", timeout=0.1) self.assertEqual(response.status_code, 0) self.assertEqual(1, global_stats.get("/", "GET").num_failures) self.assertEqual(0, global_stats.get("/", "GET").num_requests)
def test_post_redirect(self): s = HttpSession("http://127.0.0.1:%i" % self.port) url = "/redirect" r = s.post(url) self.assertEqual(200, r.status_code) post_stats = global_stats.get(url, method="POST") get_stats = global_stats.get(url, method="GET") self.assertEqual(1, post_stats.num_requests) self.assertEqual(0, get_stats.num_requests)
def test_redirect_url_original_path_as_name(self): class MyLocust(HttpLocust): host = "http://127.0.0.1:%i" % self.port l = MyLocust() l.client.get("/redirect") from orange_locust.stats import global_stats self.assertEqual(1, len(global_stats.entries)) self.assertEqual(1, global_stats.get("/redirect", "GET").num_requests) self.assertEqual(0, global_stats.get("/ultra_fast", "GET").num_requests)
def test_request_stats_content_length(self): class MyLocust(HttpLocust): host = "http://127.0.0.1:%i" % self.port locust = MyLocust() locust.client.get("/ultra_fast") self.assertEqual( global_stats.get("/ultra_fast", "GET").avg_content_length, len("This is an ultra fast response")) locust.client.get("/ultra_fast") self.assertEqual( global_stats.get("/ultra_fast", "GET").avg_content_length, len("This is an ultra fast response"))
def test_request_stats_put(self): class MyLocust(HttpLocust): host = "http://127.0.0.1:%i" % self.port locust = MyLocust() locust.client.put("/put") self.assertEqual(1, global_stats.get("/put", "PUT").num_requests)
def test_slow_redirect(self): s = HttpSession("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_request_stats_no_content_length_streaming(self): class MyLocust(HttpLocust): host = "http://127.0.0.1:%i" % self.port l = MyLocust() path = "/no_content_length" r = l.client.get(path, stream=True) self.assertEqual(0, global_stats.get(path, "GET").avg_content_length)
def test_log_request_name_argument(self): from orange_locust.stats import global_stats self.response = "" class MyLocust(HttpLocust): tasks = [] host = "http://127.0.0.1:%i" % self.port @task() def t1(l): self.response = l.client.get("/ultra_fast", name="new name!") my_locust = MyLocust() my_locust.t1() self.assertEqual(1, global_stats.get("new name!", "GET").num_requests) self.assertEqual(0, global_stats.get("/ultra_fast", "GET").num_requests)
def test_request_stats_query_variables(self): class MyLocust(HttpLocust): host = "http://127.0.0.1:%i" % self.port locust = MyLocust() locust.client.get("/ultra_fast?query=1") self.assertEqual( 1, global_stats.get("/ultra_fast?query=1", "GET").num_requests)
def test_request_stats_named_endpoint(self): class MyLocust(HttpLocust): host = "http://127.0.0.1:%i" % self.port locust = MyLocust() locust.client.get("/ultra_fast", name="my_custom_name") self.assertEqual( 1, global_stats.get("my_custom_name", "GET").num_requests)
def test_request_stats_no_content_length(self): class MyLocust(HttpLocust): host = "http://127.0.0.1:%i" % self.port l = MyLocust() path = "/no_content_length" r = l.client.get(path) self.assertEqual( global_stats.get(path, "GET").avg_content_length, len("This response does not have content-length in the header"))
def test_streaming_response(self): """ Test a request to an endpoint that returns a streaming response """ s = HttpSession("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( global_stats.get("/streaming/30", method="GET").avg_response_time, 250) global_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.assertGreater( global_stats.get("/streaming/30", method="GET").avg_response_time, 0) self.assertLess( global_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