def test_api_request_query_params(self): # GIVEN tunnel_name = "tunnel (1)" current_process = ngrok.get_ngrok_process( pyngrok_config=self.pyngrok_config) public_url = ngrok.connect(urlparse(current_process.api_url).port, name=tunnel_name, bind_tls=True).public_url time.sleep(1) urlopen("{}/status".format(public_url)).read() time.sleep(3) # WHEN response1 = ngrok.api_request( "{}/api/requests/http".format(current_process.api_url), "GET") response2 = ngrok.api_request( "{}/api/requests/http".format(current_process.api_url), "GET", params={"tunnel_name": "{}".format(tunnel_name)}) response3 = ngrok.api_request( "{}/api/requests/http".format(current_process.api_url), "GET", params={"tunnel_name": "{} (http)".format(tunnel_name)}) # THEN self.assertGreater(len(response1["requests"]), 0) self.assertGreater(len(response2["requests"]), 0) self.assertEqual(0, len(response3["requests"]))
def test_api_request_query_params(self): # GIVEN tunnel_name = "tunnel (1)" current_process = ngrok.get_ngrok_process( pyngrok_config=self.pyngrok_config) public_url = ngrok.connect(name=tunnel_name).replace("http", "https") time.sleep(1) try: urlopen(public_url) except: pass # WHEN response1 = ngrok.api_request( "{}/api/requests/http".format(current_process.api_url), "GET") response2 = ngrok.api_request( "{}/api/requests/http".format(current_process.api_url), "GET", params={"tunnel_name": "{}".format(tunnel_name)}) response3 = ngrok.api_request( "{}/api/requests/http".format(current_process.api_url), "GET", params={"tunnel_name": "{} (http)".format(tunnel_name)}) # THEN self.assertEqual(1, len(response1["requests"])) self.assertEqual(1, len(response2["requests"])) self.assertEqual(0, len(response3["requests"]))
def test_api_request_timeout(self): # GIVEN current_process = ngrok.get_ngrok_process(pyngrok_config=self.pyngrok_config) ngrok_tunnel = ngrok.connect() time.sleep(1) # WHEN with self.assertRaises(PyngrokNgrokURLError) as cm: ngrok.api_request("{}{}".format(current_process.api_url, ngrok_tunnel.uri), "DELETE", timeout=0.0001) # THEN self.assertIn("timed out", cm.exception.reason)
def test_api_request_timeout(self): # GIVEN current_process = ngrok.get_ngrok_process(config_path=self.config_path) ngrok.connect(config_path=self.config_path) time.sleep(1) tunnels = ngrok.get_tunnels() # WHEN with self.assertRaises(PyngrokNgrokURLError) as cm: ngrok.api_request("{}{}".format(current_process.api_url, tunnels[0].uri.replace("+", "%20")), "DELETE", timeout=0.0001) # THEN self.assertIn("timed out", cm.exception.reason)
def test_api_request_fails(self): # GIVEN current_process = ngrok.get_ngrok_process(pyngrok_config=self.pyngrok_config) bad_options = { "name": str(uuid.uuid4()), "addr": "8080", "proto": "invalid-proto" } # WHEN with self.assertRaises(PyngrokNgrokHTTPError) as cm: ngrok.api_request("{}/api/{}".format(current_process.api_url, "tunnels"), "POST", data=bad_options) # THEN self.assertEqual(StatusCodes.BAD_REQUEST, cm.exception.status_code) self.assertIn("invalid tunnel configuration", str(cm.exception)) self.assertIn("protocol name", str(cm.exception))
def test_api_get_request_success(self): # GIVEN current_process = ngrok.get_ngrok_process(pyngrok_config=self.pyngrok_config) ngrok_tunnel = ngrok.connect() time.sleep(1) # WHEN response = ngrok.api_request("{}{}".format(current_process.api_url, ngrok_tunnel.uri), "GET") # THEN self.assertEqual(ngrok_tunnel.name, response["name"])
def test_api_get_request_success(self): # GIVEN current_process = ngrok.get_ngrok_process(config_path=self.config_path) ngrok.connect(config_path=self.config_path) time.sleep(1) tunnel = ngrok.get_tunnels()[0] # WHEN response = ngrok.api_request("{}{}".format(current_process.api_url, tunnel.uri.replace("+", "%20")), "GET") # THEN self.assertEqual(tunnel.name, response["name"])
def test_stop_monitor_thread(self): # GIVEN self.given_ngrok_installed(self.pyngrok_config.ngrok_path) public_url = ngrok.connect(pyngrok_config=self.pyngrok_config) ngrok_process = ngrok.get_ngrok_process() monitor_thread = ngrok_process._monitor_thread # WHEN time.sleep(1) self.assertTrue(monitor_thread.is_alive()) ngrok_process.stop_monitor_thread() # Make a request to the tunnel to force a log through, which will allow the thread to trigger its own teardown try: ngrok.api_request(public_url) except: pass time.sleep(1) # THEN self.assertFalse(monitor_thread.is_alive()) self.assertIsNone(ngrok_process._monitor_thread) self.assertFalse(ngrok_process.pyngrok_config.monitor_thread)
def test_api_request_delete_data_updated(self): # GIVEN current_process = ngrok.get_ngrok_process(pyngrok_config=self.pyngrok_config) ngrok.connect() time.sleep(1) tunnels = ngrok.get_tunnels() self.assertEqual(len(tunnels), 2) # WHEN response = ngrok.api_request("{}{}".format(current_process.api_url, tunnels[0].uri.replace("+", "%20")), "DELETE") # THEN self.assertIsNone(response) tunnels = ngrok.get_tunnels() self.assertEqual(len(tunnels), 1)
def test_get_tunnel_fileserver(self): if "NGROK_AUTHTOKEN" not in os.environ: self.skipTest("NGROK_AUTHTOKEN environment variable not set") # GIVEN self.assertEqual(len(process._current_processes.keys()), 0) pyngrok_config = self.copy_with_updates(self.pyngrok_config, auth_token=os.environ["NGROK_AUTHTOKEN"]) ngrok_tunnel = ngrok.connect("file:///", pyngrok_config=pyngrok_config) time.sleep(1) api_url = ngrok.get_ngrok_process(pyngrok_config).api_url # WHEN response = ngrok.api_request("{}{}".format(api_url, ngrok_tunnel.uri), "GET") # THEN self.assertEqual(ngrok_tunnel.name, response["name"]) self.assertTrue(ngrok_tunnel.name.startswith("http-file-"))
def test_get_tunnel_fileserver(self): if "NGROK_AUTHTOKEN" not in os.environ: self.skipTest("NGROK_AUTHTOKEN environment variable not set") # GIVEN self.assertEqual(len(process._current_processes.keys()), 0) pyngrok_config = PyngrokConfig( config_path=conf.DEFAULT_NGROK_CONFIG_PATH, auth_token=os.environ["NGROK_AUTHTOKEN"]) ngrok.connect("file:///", pyngrok_config=pyngrok_config) time.sleep(1) tunnel = ngrok.get_tunnels()[0] api_url = ngrok.get_ngrok_process(pyngrok_config).api_url # WHEN response = ngrok.api_request("{}{}".format(api_url, tunnel.uri), "GET") # THEN self.assertEqual(tunnel.name, response["name"]) self.assertIn("file", tunnel.name)
def test_api_request_security_error(self): # WHEN with self.assertRaises(PyngrokSecurityError): ngrok.api_request("file:{}".format(__file__))