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_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("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_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_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_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_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_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_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_options(self): s = FastHttpSession("http://127.0.0.1:%i" % self.port) r = s.options("/request_method") self.assertEqual(200, r.status_code) self.assertEqual("", r.content.decode()) self.assertEqual( set(["OPTIONS", "DELETE", "PUT", "GET", "POST", "HEAD", "PATCH"]), set(r.headers["allow"].split(", ")), )
def test_post_redirect(self): s = FastHttpSession("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_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_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_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_custom_ssl_context_passed_correct_to_client_pool(self): """ Test FastHttpSession with a custom SSLContext factory with a options.name that will be passed correctly to the ClientPool. It will also test a 2nd factory which is not the correct one. """ def custom_ssl_context(): context = gevent.ssl.create_default_context() context.check_hostname = False context.verify_mode = gevent.ssl.CERT_NONE context.options.name = "FAKEOPTION" return context def custom_context_with_wrong_option(): context = gevent.ssl.create_default_context() context.check_hostname = False context.verify_mode = gevent.ssl.CERT_NONE context.options.name = "OPTIONFAKED" return context s = FastHttpSession( self.environment, "https://127.0.0.1:%i" % self.port, ssl_context_factory=custom_ssl_context, user=None, ) self.assertEqual(s.client.clientpool.client_args["ssl_context_factory"], custom_ssl_context) self.assertNotEqual(s.client.clientpool.client_args["ssl_context_factory"], custom_context_with_wrong_option)
def __init__(self, *, user: FastHttpUser, size: Optional[int] = None): """ Connection pool for the FastHttpUser, the user will use the connections in round robin style for every HTTP request. Args: user (FastHttpUser): Instance of a FastHttpUSer, pass in self in most circumstances size (int, optional): [description]. Override for the pool size, value defaults to the input from the command line. """ if size: self.size = size else: self.size = user.environment.parsed_options.pool_size # type: ignore self._pool: List[FastHttpSession] = [] self.request_name = None for _ in range(self.size): self._pool.append( FastHttpSession( environment=user.environment, base_url=user.host, network_timeout=user.network_timeout, connection_timeout=user.connection_timeout, max_redirects=user.max_redirects, max_retries=user.max_retries, insecure=user.insecure, concurrency=user.concurrency, user=user, ) ) self.pool = cycle(self._pool)
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_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 get_client(self): return FastHttpSession(self.environment, base_url="http://127.0.0.1:%i" % self.port, user=None)
def get(self, path, name=None, **kwargs): """Sends a GET request""" request_name = name if name else self.request_name return FastHttpSession(**self.session_info).get(path=path, name=request_name, **kwargs)
def delete(self, path, name=None, **kwargs): """sends a DELETE request""" request_name = name if name else self.request_name return FastHttpSession(**self.session_info).delete(path=path, name=request_name, **kwargs)
def options(self, path, name=None, **kwargs): """Sends a OPTIONS request""" request_name = name if name else self.request_name return FastHttpSession(**self.session_info).options(path=path, name=request_name, **kwargs)
def head(self, path, name=None, **kwargs): """Sends a HEAD request""" request_name = name if name else self.request_name return FastHttpSession(**self.session_info).head(path=path, name=request_name, **kwargs)
def put(self, path, data=None, name=None, **kwargs): """Sends a PUT request""" request_name = name if name else self.request_name return FastHttpSession(**self.session_info).put(path=path, data=data, name=request_name, **kwargs)
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)
def test_patch(self): s = FastHttpSession("http://127.0.0.1:%i" % self.port) r = s.patch("/request_method") self.assertEqual(200, r.status_code) self.assertEqual("PATCH", r.content.decode())
def test_delete(self): s = FastHttpSession("http://127.0.0.1:%i" % self.port) r = s.delete("/request_method") self.assertEqual(200, r.status_code) self.assertEqual("DELETE", r.content.decode())
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.api_client = FastHttpSession(base_url=os.environ.get("API_HOST"))
def test_head(self): s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port) r = s.head("/request_method") self.assertEqual(200, r.status_code) self.assertEqual("", r.content.decode())
def test_json_payload(self): s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port) r = s.post("/request_method", json={"foo": "bar"}) self.assertEqual(200, r.status_code)